alazzi-az/odoo-xmlrpc

PHP 包,提供了一个简单易用的接口来与 Odoo XML-RPC API 交互

v1.0.7 2023-07-22 16:45 UTC

This package is auto-updated.

Last update: 2024-09-22 19:21:42 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Odoo XML-RPC 客户端 是一个 PHP 包,它提供了一个简单易用的接口来与 Odoo XML-RPC API 交互。与需要扩展或其他依赖的其他 Odoo 客户端不同,此客户端使用 laminas/laminas-xmlrpc 包,这是 XML-RPC 协议的纯 PHP 实现。

要求

  • PHP 8.1 或更高版本
  • laminas/laminas-xmlrpc 包

安装

您可以通过 composer 安装此包

composer require alazzi-az/odoo-xmlrpc

用法

要使用 Odoo XML-RPC 客户端,首先创建 OdooClient 类的实例

use AlazziAz\OdooXmlrpc\Odoo;

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

https://your-odoo-instance.com、数据库、用户名和密码替换为您的 Odoo 实例的相应值。

然后您可以使用 Odoo 客户端

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' => 'johndoe@example.com'
]);

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

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

使用查询构建器

  • 要创建 QueryBuilder 类的新实例,您需要提供要查询的模型名称和 OdooClient 类的实例或使用客户端对象中的模型方法
    // 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();

// 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' => 't@t.t']
     );

//  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' => 'johndoe@example.com',
]);

OdooPartner::update($newPartnerId, [
    'name' => 'John Doe Jr.',
    'email' => 'john.doe@example.com',
]);

OdooPartner::delete($newPartnerId);

$partnerCount = OdooPartner::count();

$searchResult = OdooPartner::search([
    ['name', 'ilike', 'johndoe'],
    ['email', 'ilike', 'john.doe@example.com'],
]);

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

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 CHANGELOG

贡献

有关详细信息,请参阅 CONTRIBUTING

安全漏洞

请查阅 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅 许可文件