Skip to content

Commit fbeff31

Browse files
committed
Support write() and create()
1 parent 47c48b1 commit fbeff31

File tree

2 files changed

+92
-28
lines changed

2 files changed

+92
-28
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,11 @@ The following helper functions return a native PHP type insead:
9797
* readArray - array
9898
* searchCount - integer
9999
* getResourceId - integer
100-
* getResourceId2 - array
100+
* getResourceIds - array
101101

102102
(I'm torn between this approach and a more fluent approach such as
103-
`$client->firstOnly()->asArray()->read(...)`)
103+
`$client->firstOnly()->asArray()->read(...)` to set the context that
104+
will apply to the next command.)
104105

105106
Note that `searchRead` will emulate the server's `search_read` for
106107
Odoo versions less than 8.0 (OpenERP) but use the native `search_read`
@@ -124,6 +125,8 @@ fail to make the OpenERP/Odoo version number clear.
124125

125126
# TODO
126127

127-
* The write functions are not written yet (create, write and unlink).
128+
* The write functions are not written yet (~~create~~, ~~write~~ and unlink).
128129
* Examples on how relationships are managed are needed, since they are
129-
one of the areas that cause the most confusion.
130+
one of the areas that cause the most confusion. It's actuall pretty
131+
easy once you see the technique, though a helper may be useful to
132+
put together the data structure needed.

src/OdooClient.php

Lines changed: 85 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class OdooClient
6666
// Can not be used on One2many.
6767
// Can not be used in create().
6868
// (5, _, _)
69-
const RELATION_REMOVE_ALL_LINKs = 5;
69+
const RELATION_REMOVE_ALL_LINKS = 5;
7070
//
7171
// Replaces all existing records in the set by the ids list,
7272
// equivalent to using the command 5 followed by a command 4
@@ -108,6 +108,11 @@ class OdooClient
108108
protected $username;
109109
protected $password;
110110

111+
/**
112+
* The last response.
113+
*/
114+
protected $response;
115+
111116
/**
112117
* @param array $config the connection configuration details
113118
*/
@@ -176,7 +181,7 @@ public function getUserId()
176181
// Send the request.
177182

178183
try {
179-
$response = $xmlRpcClient->send($msg);
184+
$this->response = $xmlRpcClient->send($msg);
180185
} catch (Exception $e) {
181186
// Some connection problem.
182187

@@ -188,7 +193,7 @@ public function getUserId()
188193

189194
// Grab the User ID.
190195

191-
$this->userId = $this->valueToNative($response->value());
196+
$this->userId = $this->valueToNative($this->response->value());
192197

193198
// Get the server version for capabilities.
194199

@@ -275,9 +280,9 @@ public function search(
275280
$msg->addParam($this->intValue($limit));
276281
$msg->addParam($this->stringValue($order));
277282

278-
$response = $this->getXmlRpcClient('object')->send($msg);
283+
$this->response = $this->getXmlRpcClient('object')->send($msg);
279284

280-
return $response;
285+
return $this->response;
281286
}
282287

283288
/**
@@ -292,24 +297,24 @@ public function searchArray(
292297
$limit = self::DEFAULT_LIMIT,
293298
$order = ''
294299
) {
295-
$response = $this->search(
300+
$this->response = $this->search(
296301
$modelName,
297302
$criteria,
298303
$offset,
299304
$limit,
300305
$order
301306
);
302307

303-
if ($response->value() instanceof Value) {
304-
return $this->valueToNative($response->value());
308+
if ($this->response->value() instanceof Value) {
309+
return $this->valueToNative($this->response->value());
305310
}
306311

307312
// An error in the criteria or model provided.
308313

309314
throw new Exception(sprintf(
310315
'Failed to search model %s; response was "%s"',
311316
$modelName,
312-
$response->value()
317+
$this->response->value()
313318
));
314319
}
315320

@@ -327,9 +332,9 @@ public function searchCount(
327332

328333
$msg->addParam($this->nativeToValue($criteria));
329334

330-
$response = $this->getXmlRpcClient('object')->send($msg);
335+
$this->response = $this->getXmlRpcClient('object')->send($msg);
331336

332-
return $this->valueToNative($response->value());
337+
return $this->valueToNative($this->response->value());
333338
}
334339

335340
/**
@@ -365,10 +370,10 @@ public function searchRead(
365370
$msg->addParam($this->intValue($limit));
366371
$msg->addParam($this->stringValue($order));
367372

368-
$response = $this->getXmlRpcClient('object')->send($msg);
373+
$this->response = $this->getXmlRpcClient('object')->send($msg);
369374
}
370375

371-
return $response;
376+
return $this->response;
372377
}
373378

374379
/**
@@ -381,15 +386,15 @@ public function searchReadArray(
381386
$limit = self::DEFAULT_LIMIT,
382387
$order = ''
383388
) {
384-
$response = $this->searchRead(
389+
$this->response = $this->searchRead(
385390
$modelName,
386391
$criteria,
387392
$offset,
388393
$limit,
389394
$order
390395
);
391396

392-
return $this->valueToNative($response->value());
397+
return $this->valueToNative($this->response->value());
393398
}
394399

395400
/**
@@ -411,9 +416,9 @@ public function read(
411416
$msg->addParam($this->nativeToValue($options));
412417
}
413418

414-
$response = $this->getXmlRpcClient('object')->send($msg);
419+
$this->response = $this->getXmlRpcClient('object')->send($msg);
415420

416-
return $response;
421+
return $this->response;
417422
}
418423

419424
/**
@@ -424,22 +429,22 @@ public function readArray(
424429
array $instanceIds = [],
425430
array $options = []
426431
) {
427-
$response = $this->read(
432+
$this->response = $this->read(
428433
$modelName,
429434
$instanceIds,
430435
$options
431436
);
432437

433-
if ($response->value() instanceof Value) {
434-
return $this->valueToNative($response->value());
438+
if ($this->response->value() instanceof Value) {
439+
return $this->valueToNative($this->response->value());
435440
}
436441

437442
// An error in the instanceIds or model provided.
438443

439444
throw new Exception(sprintf(
440445
'Failed to read model %s; response was "%s"',
441446
$modelName,
442-
$response->value()
447+
$this->response->value()
443448
));
444449
}
445450

@@ -452,13 +457,58 @@ public function version()
452457
{
453458
$msg = new Request('version');
454459

455-
$response = $this->getXmlRpcClient('common')->send($msg);
460+
$this->response = $this->getXmlRpcClient('common')->send($msg);
456461

457-
return $this->valueToNative($response->value());
462+
return $this->valueToNative($this->response->value());
463+
}
464+
465+
/**
466+
* Create a new resource.
467+
*
468+
* @param array $fields
469+
*/
470+
public function create(string $modelName, array $fields)
471+
{
472+
$msg = $this->getBaseObjectRequest($modelName, 'create');
473+
474+
$msg->addParam($this->nativeToValue($fields));
475+
476+
$this->response = $this->getXmlRpcClient('object')->send($msg);
477+
478+
// If there was an error, then an integer will be returned.
479+
480+
if (! $this->response->value() instanceof Value) {
481+
return $this->response->value();
482+
}
483+
484+
return $this->valueToNative($this->response->value());
485+
}
486+
487+
/**
488+
* Update a resource.
489+
*
490+
* @return bool true if the update was successful.
491+
*/
492+
public function write(string $modelName, int $resourceId, array $fields)
493+
{
494+
$msg = $this->getBaseObjectRequest($modelName, 'write');
495+
496+
$msg->addParam($this->nativeToValue([$resourceId]));
497+
$msg->addParam($this->nativeToValue($fields));
498+
499+
$this->response = $this->getXmlRpcClient('object')->send($msg);
500+
501+
// If there was an error, then an integer will be returned.
502+
503+
if (! $this->response->value() instanceof Value) {
504+
return $this->response->value();
505+
}
506+
507+
return $this->valueToNative($this->response->value());
458508
}
459509

460510
//
461-
// TODO: actions to implement = create write unlink
511+
// TODO: actions to implement = unlink
462512
// Also: fields_get, version
463513
//
464514

@@ -676,4 +726,15 @@ public function valueToNative(Value $value)
676726

677727
return $result;
678728
}
729+
730+
/**
731+
* The last response, in case it needs to be inspected for
732+
* error reasons.
733+
*
734+
* @return Response|null
735+
*/
736+
public function getLastResponse()
737+
{
738+
return $this->response;
739+
}
679740
}

0 commit comments

Comments
 (0)