Skip to content

Commit cd1b98f

Browse files
committed
Added options parameter to read() method.
1 parent 183d2bc commit cd1b98f

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

README.md

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ Publish `config\odoo-api.php` using the Laravel artisan command:
3030

3131
A sample set of entries for `.env` can be found in `.env.example`.
3232

33+
You can add multiple sets of configuration to `config\odoo-api.php` and
34+
use them all at once in your application.
35+
The configuration set name is passed in to `OdooApi::getClient('config-name')`.
36+
3337
# Example
3438

3539
A very simple example:
@@ -71,6 +75,13 @@ $client->searchCount('res.partner', $criteria);
7175
$client->read('res.partner', [17858, 17852])->value()->me['array']
7276
```
7377

78+
If you have specific requirements for the XML-RPC client, such as an SSL
79+
certificate to add, then yiu can get the client instance using:
80+
81+
$xmlRpcClient = $client->getXmlRpcClient($type);
82+
83+
where `$type` will typically be 'db', 'common' or 'object'.
84+
7485
# Query methods
7586

7687
The following methods are supported and will return an XML-RPC response:
@@ -87,17 +98,31 @@ The following helper functions return a native PHp type insead:
8798
* searchCount - integer
8899
* getResourceId - integer
89100

101+
(I'm torn between this approach and a more fluent approach such as
102+
`$client->firstOnly()->asArray()->read(...)`)
103+
90104
Note that `searchRead` will emulate the server's `search_read` for
91105
Odoo versions less than 8.0 (OpenERP) but use the native `search_read`
92106
for Odoo 8.0 upwards.
93107

108+
# Read Options
109+
110+
The `read()` method takes an options array that varies significantly
111+
between OpenERP/Odoo versions.
112+
This package does not attempt to deal with that at this time.
113+
114+
Fpor example, to restrict the read to named attributes, the following
115+
formats are used:
116+
117+
* OpenERP 7: $client->read('account.invoice', [123], ['type', 'partner_id']);
118+
* OpenERP 8: $client->read('account.invoice', [123], ['fields' => ['type', 'partner_id']]);
119+
* OpenERP 10: $client->read('account.invoice', [123], ['attributes' => ['type', 'partner_id']]);
120+
121+
This makes finding help on the API difficult, since many articles
122+
fail to make the OpenERP/Odoo version number clear.
123+
94124
# TODO
95125

96-
* An elegant way to parse the results, as the `Value` objects can be
97-
a little cumbersome.
98-
For example, we know the `search()` result will be an array of
99-
integer model instance IDs, so a simple array or collection of IDs can
100-
be returned, rather than a Value array containing Value integer objects.
101126
* Docs on config (this package supports multiple clients, so can connect
102127
to multiple Odoo instances or as multiple users at the same time).
103128
* Docs on installation (has a auto discovered provider and facade).

src/OdooClient.php

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class OdooClient
2929
const DEFAULT_LIMIT = 100;
3030

3131
/**
32-
*
32+
* Later versions of the API include a version number e.g. /xmlrpc/2/
3333
*/
3434
protected $endpointTemplate = '{uri}/xmlrpc/{type}';
3535

@@ -75,16 +75,19 @@ public function __construct(array $config)
7575
}
7676

7777
/**
78-
* Get an XML ROC client singleton of a particular type.
78+
* Get an XML RRC client singleton of a particular type.
79+
*
80+
* @param string $type One of: 'common', 'object', 'db'
81+
* @return Client
7982
*/
8083
public function getXmlRpcClient(string $type)
8184
{
85+
$type = strtolower($type);
86+
8287
if (array_key_exists($type, $this->xmlRpcClients)) {
8388
return $this->xmlRpcClients[$type];
8489
}
8590

86-
$type = strtolower($type);
87-
8891
$endpoint = str_replace(
8992
['{uri}', '{type}'],
9093
[$this->url, $type],
@@ -343,17 +346,24 @@ public function searchReadArray(
343346
}
344347

345348
/**
349+
* @param string $modelName example res.partner
346350
* @param array $instanceIds list of model instance IDs to read and return
351+
* @param array $options varies with API versions see documentation
347352
* @return Response
348353
*/
349354
public function read(
350355
string $modelName,
351-
array $instanceIds = []
356+
array $instanceIds = [],
357+
array $options = []
352358
) {
353359
$msg = $this->getBaseObjectRequest($modelName, 'read');
354360

355361
$msg->addParam($this->nativeToValue($instanceIds));
356362

363+
if (! empty($options)) {
364+
$msg->addParam($this->nativeToValue($options));
365+
}
366+
357367
$response = $this->getXmlRpcClient('object')->send($msg);
358368

359369
return $response;
@@ -364,11 +374,13 @@ public function read(
364374
*/
365375
public function readArray(
366376
string $modelName,
367-
array $instanceIds = []
377+
array $instanceIds = [],
378+
array $options = []
368379
) {
369380
$response = $this->read(
370381
$modelName,
371-
$instanceIds
382+
$instanceIds,
383+
$options
372384
);
373385

374386
if ($response->value() instanceof Value) {
@@ -405,6 +417,9 @@ public function version()
405417

406418
/**
407419
* Get the ERP internal resource ID for a given external ID.
420+
* This in thoery kind of belongs in a wrapper to the client,
421+
* but is used so often in data syncs that it makes sense having
422+
* it here.
408423
*
409424
* @param string $externalId either "name" or "module.name"
410425
* @param string $module optional, but recommended

0 commit comments

Comments
 (0)