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'
);
// 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\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();