PHP code example of alazzi-az / odoo-xmlrpc

1. Go to this page and download the library: Download alazzi-az/odoo-xmlrpc library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alazzi-az / odoo-xmlrpc example snippets


use AlazziAz\OdooXmlrpc\Odoo;

$client = Odoo::client('https://your-odoo-instance.com','xmlrpc/2', 'database', 'username', 'password');

use AlazziAz\OdooXmlrpc\Client;

// Call a method on the Odoo server
$result = $client->call('res.partner', 'search_read', [], ['limit' => 5]);

// Get records from a model
$records = $client->get('res.partner', [], ['name', 'id'], 5);

// Search for records in a model
$searchResult = $client->search('res.partner', []);

// Read records from a model
$records = $client->read('res.partner', $searchResult, ['name', 'id']);

// Create a new record in a model
$id = $client->create('res.partner', [
    'name' => 'John Doe',
    'email' => '[email protected]'
]);

// Update an existing record in a model
$result = $client->update('res.partner', [$id], [
    'name' => 'Jane Doe',
    'email' => '[email protected]'
]);

// Delete a record from a model
$result = $client->delete('res.partner', [$id]);

// Get the number of records in a model
$count = $client->count('res.partner', []);

// Get the current user's ID
$uid = $client->getUid();

// Get the version of the Odoo server
$version = $client->getVersion();


    // Create a new instance of the QueryBuilder using the model method
    $queryBuilder = $client->model('res.partner');
    
    // Or create a new instance of the QueryBuilder using the constructor
    $queryBuilder = new QueryBuilder('res.partner', $client);
    

// Query with difference conditions cluose
    $result = $queryBuilder
      ->where('id', '=', 5)
      ->orWhere('id', '=', 6)
      ->whereIn('id', [11, 10])
      ->whereNotIn('id', [100, 200])
      ->whereNull('id')
      ->whereNotNull('id')
      ->whereBetween('id', [10,99])
      ->whereNotBetween('id', [500, 600])
      ->whereNotBetween('id', [100, 200])
      ->get();

// You can provide multiple arguments to select multiple fields
   $result = $queryBuilder->select('id', 'name')->get();

// retrieve the first record that matches the query.
   $result = $queryBuilder->first();

//  limit the number of records returned by the query.
   $result = $queryBuilder->limit(5)->get();

//  sort the records returned by the query.
   $result = $queryBuilder->order('name')->get();

// retrieve the records that match the query. It returns an array of records
   $records = $queryBuilder->where('name', 'ilike', 'johndoe')
                    ->get();

// retrieve the number of records that match the query:
   $result = $queryBuilder->count();

// retrieve a record by its ID. You need to provide the ID as the first argument
   $result = $queryBuilder->find($createResult);

// create a new record. You need to provide an array of data to create the record
   $result = $queryBuilder->create([
        'name' => 'test',
        'email' => '[email protected]']
     );

//  update one or more records. You need to provide an array of data to update the records
   $result = $queryBuilder->where('id', '=', 4)->update([
        'name' => 'test2'
        ]);

// retrieve the IDs of the records that match the query
   $result = $queryBuilder->ids();

// delete the records that match the query
   $result = $queryBuilder->where('id', '=', 5)->delete();


namespace Your\Namespace;

use AlazziAz\OdooXmlrpc\OdooClient;
use AlazziAz\OdooXmlrpc\QueryBuilder;
use AlazziAz\OdooXmlrpc\Concern\Resourceable;

class OdooPartner implements \AlazziAz\OdooXmlrpc\Interfaces\OdooResource
{
    use Resourceable;
  
    public static function getModelName(): string
    {
        return 'res.partner';
    }

    public static function getModelFields(): array
    {
        return ['name', 'email'];
    }
}

// To use the class, you need to boot it:
OdooPartner::boot($odooClient);


// Now you can use the class to perform CRUD operations:
$partners = OdooPartner::query()->get();
foreach ($partners as $partner) {
    echo $partner['name'] . ': ' . $partner['email'] . "\n";
}

$newPartnerId = OdooPartner::create([
    'name' => 'John Doe',
    'email' => '[email protected]',
]);

OdooPartner::update($newPartnerId, [
    'name' => 'John Doe Jr.',
    'email' => '[email protected]',
]);

OdooPartner::delete($newPartnerId);

$partnerCount = OdooPartner::count();

$searchResult = OdooPartner::search([
    ['name', 'ilike', 'johndoe'],
    ['email', 'ilike', '[email protected]'],
]);

$partners = OdooPartner::read($searchResult, ['name', 'email']);