@@ -66,7 +66,7 @@ class OdooClient
66
66
// Can not be used on One2many.
67
67
// Can not be used in create().
68
68
// (5, _, _)
69
- const RELATION_REMOVE_ALL_LINKs = 5 ;
69
+ const RELATION_REMOVE_ALL_LINKS = 5 ;
70
70
//
71
71
// Replaces all existing records in the set by the ids list,
72
72
// equivalent to using the command 5 followed by a command 4
@@ -108,6 +108,11 @@ class OdooClient
108
108
protected $ username ;
109
109
protected $ password ;
110
110
111
+ /**
112
+ * The last response.
113
+ */
114
+ protected $ response ;
115
+
111
116
/**
112
117
* @param array $config the connection configuration details
113
118
*/
@@ -176,7 +181,7 @@ public function getUserId()
176
181
// Send the request.
177
182
178
183
try {
179
- $ response = $ xmlRpcClient ->send ($ msg );
184
+ $ this -> response = $ xmlRpcClient ->send ($ msg );
180
185
} catch (Exception $ e ) {
181
186
// Some connection problem.
182
187
@@ -188,7 +193,7 @@ public function getUserId()
188
193
189
194
// Grab the User ID.
190
195
191
- $ this ->userId = $ this ->valueToNative ($ response ->value ());
196
+ $ this ->userId = $ this ->valueToNative ($ this -> response ->value ());
192
197
193
198
// Get the server version for capabilities.
194
199
@@ -275,9 +280,9 @@ public function search(
275
280
$ msg ->addParam ($ this ->intValue ($ limit ));
276
281
$ msg ->addParam ($ this ->stringValue ($ order ));
277
282
278
- $ response = $ this ->getXmlRpcClient ('object ' )->send ($ msg );
283
+ $ this -> response = $ this ->getXmlRpcClient ('object ' )->send ($ msg );
279
284
280
- return $ response ;
285
+ return $ this -> response ;
281
286
}
282
287
283
288
/**
@@ -292,24 +297,24 @@ public function searchArray(
292
297
$ limit = self ::DEFAULT_LIMIT ,
293
298
$ order = ''
294
299
) {
295
- $ response = $ this ->search (
300
+ $ this -> response = $ this ->search (
296
301
$ modelName ,
297
302
$ criteria ,
298
303
$ offset ,
299
304
$ limit ,
300
305
$ order
301
306
);
302
307
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 ());
305
310
}
306
311
307
312
// An error in the criteria or model provided.
308
313
309
314
throw new Exception (sprintf (
310
315
'Failed to search model %s; response was "%s" ' ,
311
316
$ modelName ,
312
- $ response ->value ()
317
+ $ this -> response ->value ()
313
318
));
314
319
}
315
320
@@ -327,9 +332,9 @@ public function searchCount(
327
332
328
333
$ msg ->addParam ($ this ->nativeToValue ($ criteria ));
329
334
330
- $ response = $ this ->getXmlRpcClient ('object ' )->send ($ msg );
335
+ $ this -> response = $ this ->getXmlRpcClient ('object ' )->send ($ msg );
331
336
332
- return $ this ->valueToNative ($ response ->value ());
337
+ return $ this ->valueToNative ($ this -> response ->value ());
333
338
}
334
339
335
340
/**
@@ -365,10 +370,10 @@ public function searchRead(
365
370
$ msg ->addParam ($ this ->intValue ($ limit ));
366
371
$ msg ->addParam ($ this ->stringValue ($ order ));
367
372
368
- $ response = $ this ->getXmlRpcClient ('object ' )->send ($ msg );
373
+ $ this -> response = $ this ->getXmlRpcClient ('object ' )->send ($ msg );
369
374
}
370
375
371
- return $ response ;
376
+ return $ this -> response ;
372
377
}
373
378
374
379
/**
@@ -381,15 +386,15 @@ public function searchReadArray(
381
386
$ limit = self ::DEFAULT_LIMIT ,
382
387
$ order = ''
383
388
) {
384
- $ response = $ this ->searchRead (
389
+ $ this -> response = $ this ->searchRead (
385
390
$ modelName ,
386
391
$ criteria ,
387
392
$ offset ,
388
393
$ limit ,
389
394
$ order
390
395
);
391
396
392
- return $ this ->valueToNative ($ response ->value ());
397
+ return $ this ->valueToNative ($ this -> response ->value ());
393
398
}
394
399
395
400
/**
@@ -411,9 +416,9 @@ public function read(
411
416
$ msg ->addParam ($ this ->nativeToValue ($ options ));
412
417
}
413
418
414
- $ response = $ this ->getXmlRpcClient ('object ' )->send ($ msg );
419
+ $ this -> response = $ this ->getXmlRpcClient ('object ' )->send ($ msg );
415
420
416
- return $ response ;
421
+ return $ this -> response ;
417
422
}
418
423
419
424
/**
@@ -424,22 +429,22 @@ public function readArray(
424
429
array $ instanceIds = [],
425
430
array $ options = []
426
431
) {
427
- $ response = $ this ->read (
432
+ $ this -> response = $ this ->read (
428
433
$ modelName ,
429
434
$ instanceIds ,
430
435
$ options
431
436
);
432
437
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 ());
435
440
}
436
441
437
442
// An error in the instanceIds or model provided.
438
443
439
444
throw new Exception (sprintf (
440
445
'Failed to read model %s; response was "%s" ' ,
441
446
$ modelName ,
442
- $ response ->value ()
447
+ $ this -> response ->value ()
443
448
));
444
449
}
445
450
@@ -452,13 +457,58 @@ public function version()
452
457
{
453
458
$ msg = new Request ('version ' );
454
459
455
- $ response = $ this ->getXmlRpcClient ('common ' )->send ($ msg );
460
+ $ this -> response = $ this ->getXmlRpcClient ('common ' )->send ($ msg );
456
461
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 ());
458
508
}
459
509
460
510
//
461
- // TODO: actions to implement = create write unlink
511
+ // TODO: actions to implement = unlink
462
512
// Also: fields_get, version
463
513
//
464
514
@@ -676,4 +726,15 @@ public function valueToNative(Value $value)
676
726
677
727
return $ result ;
678
728
}
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
+ }
679
740
}
0 commit comments