PHP code example of oscar-team / odoo-json2

1. Go to this page and download the library: Download oscar-team/odoo-json2 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/ */

    

oscar-team / odoo-json2 example snippets


use OdooJson2\Odoo;

class YourController extends Controller
{
    public function __construct(
        private Odoo $odoo
    ) {}

    public function index()
    {
        $partners = $this->odoo->search('res.partner', []);
        return response()->json($partners);
    }
}

use OdooJson2\Odoo;

$odoo = app(Odoo::class);
$partners = $odoo->search('res.partner', []);

use App\Models\Partner; // Your Odoo model

class PartnerController extends Controller
{
    public function index()
    {
        $partners = Partner::query()
            ->where('is_company', '=', true)
            ->get();
        
        return response()->json($partners);
    }
}

use OdooJson2\OdooClient;

$client = new OdooClient(
    baseUrl: 'https://your-odoo-instance.com',
    apiKey: 'your-api-key-here',
    database: 'your-database-name'
);

// Search with domain
$partners = $client->search('res.partner', [
    ['name', '=', 'John Doe']
]);

// Search with options
$partners = $client->search('res.partner', [
    ['is_company', '=', true]
], [
    'limit' => 10,
    'offset' => 0,
    'order' => 'name asc'
]);

// Read specific records
$partners = $client->read('res.partner', [1, 2, 3]);

// Read with specific fields
$partners = $client->read('res.partner', [1, 2, 3], [
    'name',
    'email',
    'phone'
]);

// Search and read in one call
$partners = $client->searchRead('res.partner', [
    ['is_company', '=', true]
], [
    'fields' => ['name', 'email', 'phone'],
    'limit' => 10
]);

// Create a single record
$partnerId = $client->create('res.partner', [
    'name' => 'John Doe',
    'email' => '[email protected]',
    'phone' => '+1234567890',
    'is_company' => false
]);

// Create multiple records
$partnerIds = $client->create('res.partner', [
    [
        'name' => 'Company A',
        'is_company' => true,
    ],
    [
        'name' => 'Company B',
        'is_company' => true,
    ],
]);

// Update records
$success = $client->write('res.partner', [1, 2], [
    'email' => '[email protected]',
    'phone' => '+9876543210'
]);

// Delete records
$success = $client->unlink('res.partner', [1, 2, 3]);

// Count records matching a domain
$count = $client->count('res.partner', [
    ['is_company', '=', true]
]);

// Get all fields of a model
$fields = $client->fieldsGet('res.partner');

// Get specific field attributes
$fields = $client->fieldsGet('res.partner', ['type', '

// Call any custom method on a model
$result = $client->call('sale.order', 'action_confirm', [
    'ids' => [123],
    'context' => []
]);

use OdooJson2\Exceptions\OdooException;
use OdooJson2\Exceptions\AuthenticationException;
use OdooJson2\Exceptions\ConnectionException;

try {
    $partners = $client->search('res.partner', []);
} catch (AuthenticationException $e) {
    // Handle authentication errors
    echo "Authentication failed: " . $e->getMessage();
} catch (ConnectionException $e) {
    // Handle connection errors
    echo "Connection failed: " . $e->getMessage();
} catch (OdooException $e) {
    // Handle other Odoo errors
    echo "Odoo error: " . $e->getMessage();
}

// Custom HTTP client options
$client = new OdooClient(
    baseUrl: 'https://your-odoo-instance.com',
    apiKey: 'your-api-key-here',
    database: 'your-database-name',
    options: [
        'timeout' => 60,
        'verify' => false, // Only for development
        'proxy' => 'http://proxy.example.com:8080',
    ]
);

// Use custom HTTP client
$customClient = new \GuzzleHttp\Client([
    'timeout' => 120,
]);
$client->setHttpClient($customClient);

// Simple condition
[['field', '=', 'value']]

// Multiple conditions (AND)
[
    ['field1', '=', 'value1'],
    ['field2', '!=', 'value2']
]

// OR conditions
['|', ['field1', '=', 'value1'], ['field2', '=', 'value2']]

// Operators: =, !=, <, >, <=, >=, like, ilike, in, not in, child_of, parent_of

// Create a sales order
$orderId = $client->create('sale.order', [
    'partner_id' => 1,
    'order_line' => [
        [
            'product_id' => 5,
            'product_uom_qty' => 10,
            'price_unit' => 100.00,
        ],
    ],
]);

// Confirm the order
$client->call('sale.order', 'action_confirm', [
    'ids' => [$orderId]
]);

// Search for confirmed orders
$orders = $client->searchRead('sale.order', [
    ['state', '=', 'sale']
], [
    'fields' => ['name', 'partner_id', 'amount_total'],
    'limit' => 20
]);

// Create a product
$productId = $client->create('product.product', [
    'name' => 'New Product',
    'type' => 'product',
    'categ_id' => 1,
    'list_price' => 99.99,
    'standard_price' => 50.00,
]);

// Update product price
$client->write('product.product', [$productId], [
    'list_price' => 89.99
]);

// Search products by category
$products = $client->searchRead('product.product', [
    ['categ_id', '=', 1],
    ['sale_ok', '=', true]
], [
    'fields' => ['name', 'list_price', 'qty_available'],
    'order' => 'list_price desc'
]);

use OdooJson2\Attributes\Field;
use OdooJson2\Attributes\Model;
use OdooJson2\Odoo\OdooModel;

#[Model('res.partner')]
class Partner extends OdooModel
{
    #[Field]
    public string $name;

    #[Field('email')]
    public ?string $email;

    #[Field('phone')]
    public ?string $phone;

    #[Field('is_company')]
    public bool $isCompany = false;
}

#[Model('product.product')]
class Product extends OdooModel
{
    // Field name matches property name
    #[Field]
    public string $name;

    // Custom field name mapping
    #[Field('default_code')]
    public ?string $defaultCode;

    #[Field('list_price')]
    public ?float $listPrice;

    #[Field('standard_price')]
    public ?float $standardPrice;
}

#[Model('product.product')]
class Product extends OdooModel
{
    #[Field]
    public string $name;

    // Extract the ID from the many-to-one field
    #[Field('categ_id'), Key]
    public ?int $categoryId;

    // Extract the name from the many-to-one field
    #[Field('categ_id'), KeyName]
    public ?string $categoryName;

    // Define the relationship
    #[Field('categ_id'), BelongsTo(name: 'categ_id', class: ProductCategory::class)]
    public ?ProductCategory $category;
}

#[Model('account.move')]
class AccountMove extends OdooModel
{
    #[Field('name')]
    public string $name;

    #[Field('partner_id'), Key]
    public ?int $partnerId;

    // One-to-many relationship
    #[Field('invoice_line_ids'), HasMany(class: AccountMoveLine::class, name: 'invoice_line_ids')]
    public ?array $invoiceLines;
}

use OdooJson2\Attributes\BelongsTo;
use OdooJson2\Attributes\Field;
use OdooJson2\Attributes\HasMany;
use OdooJson2\Attributes\Key;
use OdooJson2\Attributes\KeyName;
use OdooJson2\Attributes\Model;
use OdooJson2\Odoo\OdooModel;

#[Model('res.partner')]
class Partner extends OdooModel
{
    #[Field]
    public string $name;

    #[Field('display_name')]
    public ?string $displayName;

    #[Field('email')]
    public ?string $email;

    #[Field('phone')]
    public ?string $phone;

    #[Field('street')]
    public ?string $street;

    #[Field('city')]
    public ?string $city;

    #[Field('zip')]
    public ?string $zip;

    #[Field('country_id'), Key]
    public ?int $countryId;

    #[Field('country_id'), KeyName]
    public ?string $countryName;

    #[Field('is_company')]
    public bool $isCompany = false;

    #[Field('active')]
    public bool $active = true;

    // Self-referential relationship
    #[Field('parent_id'), Key]
    public ?int $parentId;

    #[Field('parent_id'), BelongsTo(name: 'parent_id', class: Partner::class)]
    public ?Partner $parent;

    #[Field('child_ids'), HasMany(class: Partner::class, name: 'child_ids')]
    public ?array $children;
}

use OdooJson2\Odoo;
use OdooJson2\Odoo\Config;
use OdooJson2\Odoo\Context;
use OdooJson2\Odoo\OdooModel;

// Create Odoo instance
$config = new Config(
    database: 'your-database',
    host: 'https://your-odoo-instance.com',
    apiKey: 'your-api-key'
);
$context = new Context();
$odoo = new Odoo($config, $context);

// Boot models
OdooModel::boot($odoo);

// Find a record by ID
$partner = Partner::find(1);

// Get all records
$partners = Partner::all();

// Query with conditions
$partners = Partner::query()
    ->where('is_company', '=', true)
    ->where('active', '=', true)
    ->limit(10)
    ->orderBy('name')
    ->get();

// Get first matching record
$partner = Partner::query()
    ->where('email', '=', '[email protected]')
    ->first();

// Count records
$count = Partner::query()
    ->where('is_company', '=', true)
    ->count();

// Get only IDs
$ids = Partner::query()
    ->where('active', '=', true)
    ->ids();

// Create a new partner
$partner = new Partner();
$partner->name = 'John Doe';
$partner->email = '[email protected]';
$partner->phone = '+1234567890';
$partner->isCompany = false;
$partner->save();

// Or use fill method
$partner = new Partner();
$partner->fill([
    'name' => 'John Doe',
    'email' => '[email protected]',
    'phone' => '+1234567890',
]);
$partner->save();

// Update an existing record
$partner = Partner::find(1);
$partner->email = '[email protected]';
$partner->phone = '+9876543210';
$partner->save();

// Or update multiple records via query
Partner::query()
    ->where('is_company', '=', true)
    ->update(['active' => false]);

// Delete a single record
$partner = Partner::find(1);
// Note: OdooModel doesn't have a delete() method by default
// Use the Odoo client directly:
$odoo->unlink('res.partner', [$partner->id]);

// Or delete via query
Partner::query()
    ->where('active', '=', false)
    ->delete();

// Access belongs-to relationship
$product = Product::find(1);
$category = $product->category; // Automatically loaded

// Access has-many relationship (lazy loaded)
$invoice = AccountMove::find(1);
$lines = $invoice->invoiceLines; // Lazy loaded when accessed

// Working with self-referential relationships
$partner = Partner::find(1);
$parent = $partner->parent; // Parent partner
$children = $partner->children; // Child partners

// Search with multiple conditions
$partners = Partner::query()
    ->where('is_company', '=', true)
    ->orWhere('email', '!=', null)
    ->limit(50)
    ->offset(10)
    ->orderBy('name', 'desc')
    ->get();

// Read specific records
$partners = Partner::read([1, 2, 3, 4, 5]);

// Get model fields
$fields = Partner::listFields();

$partner = new Partner();
$partner->name = 'Acme Corporation';
$partner->email = '[email protected]';
$partner->phone = '+1234567890';
$partner->isCompany = true;
$partner->street = '123 Main St';
$partner->city = 'New York';
$partner->zip = '10001';
$partner->countryId = 233; // USA
$partner->save();

echo "Created partner with ID: {$partner->id}\n";

// Find products by category
$products = Product::query()
    ->where('categ_id', '=', 1)
    ->where('sale_ok', '=', true)
    ->where('active', '=', true)
    ->get();

foreach ($products as $product) {
    echo "Product: {$product->name}, Price: {$product->listPrice}\n";
    
    // Update price
    $product->listPrice = $product->listPrice * 1.1; // 10% increase
    $product->save();
}

// Find invoices for a partner
$invoices = AccountMove::query()
    ->where('partner_id', '=', 1)
    ->where('move_type', '=', 'out_invoice')
    ->where('state', '=', 'posted')
    ->get();

foreach ($invoices as $invoice) {
    echo "Invoice: {$invoice->name}, Total: {$invoice->amountTotal}\n";
    
    // Access invoice lines (lazy loaded)
    if ($invoice->invoiceLines) {
        foreach ($invoice->invoiceLines as $line) {
            echo "  - Line: {$line->name}, Amount: {$line->priceTotal}\n";
        }
    }
}

// Find company partners
$companies = Partner::query()
    ->where('is_company', '=', true)
    ->get();

foreach ($companies as $company) {
    echo "Company: {$company->name}\n";
    
    // Access children (lazy loaded)
    if ($company->children) {
        foreach ($company->children as $child) {
            echo "  - Contact: {$child->name} ({$child->email})\n";
        }
    }
}
bash
php artisan vendor:publish --provider="OdooJson2\OdooServiceProvider" --tag="config"