Skip to content

Commit 3cf2a3a

Browse files
committed
Return collections of mapped models rather than arrays; remove "array" methods.
1 parent fbeff31 commit 3cf2a3a

File tree

8 files changed

+247
-84
lines changed

8 files changed

+247
-84
lines changed

README.md

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,26 @@ where `$type` will typically be 'db', 'common' or 'object'.
8484

8585
# Query methods
8686

87-
The following methods are supported and will return an XML-RPC response:
87+
The following methods are supported and will return a collection:
8888

89-
* search()
90-
* searchRead()
91-
* read()
89+
* search() - collection of integers
90+
* searchRead() - collection of models
91+
* read() - collection of models
92+
* getResourceIds - collection of integers
93+
* fieldsGet() - collection of arrays
9294

9395
The following helper functions return a native PHP type insead:
9496

95-
* searchArray - array
96-
* searchReadArray - array
97-
* readArray - array
9897
* searchCount - integer
9998
* getResourceId - integer
100-
* getResourceIds - array
99+
* unlink - boolean
100+
* create - boolean
101101

102-
(I'm torn between this approach and a more fluent approach such as
103-
`$client->firstOnly()->asArray()->read(...)` to set the context that
104-
will apply to the next command.)
102+
All `read()` and `searchRead()` methods will return a collection of models.
103+
The default model will be `Consilience\OdooApi\Model`, but other models can be specified.
104+
The `odoo-api.php` config provides an array setting to map OpenERP model names
105+
to model class names for instantiation. Further mappings can be added to the client
106+
using `$client->addMapping('odoo.model.name', \FQDN\Class\name::class)`.
105107

106108
Note that `searchRead` will emulate the server's `search_read` for
107109
Odoo versions less than 8.0 (OpenERP) but use the native `search_read`
@@ -123,6 +125,10 @@ formats are used:
123125
This makes finding help on the API difficult, since many articles
124126
fail to make the OpenERP/Odoo version number clear.
125127

128+
# Setting Relationships
129+
130+
TODO (it's fairly simple once explained)
131+
126132
# TODO
127133

128134
* The write functions are not written yet (~~create~~, ~~write~~ and unlink).

config/odoo-api.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@
1414
'database' => env('ODOO_API_DATABASE'),
1515
'username' => env('ODOO_API_USER'),
1616
'password' => env('ODOO_API_PASSWORD'),
17+
18+
'model_map' => [
19+
],
1720
],
1821
],
22+
23+
// Map OpenERP model names to local model classes.
24+
25+
'model_map' => [
26+
//'account.invoice' => Foo\Bar\Invoice::class,
27+
],
1928
];

src/HasModelDataTrait.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Consilience\OdooApi;
4+
5+
/**
6+
* Methods to initialise, and get data items.
7+
*/
8+
9+
trait HasModelDataTrait
10+
{
11+
/**
12+
* Data structure as returned by the API and converted to
13+
* a native PHP array.
14+
*/
15+
protected $data = [];
16+
17+
/**
18+
* Instantiate with the array data from the ERP model read.
19+
*/
20+
public function __construct(array $data = [])
21+
{
22+
// Store away the source data.
23+
$this->setData($data);
24+
}
25+
26+
protected function setData($data)
27+
{
28+
$this->data = $data;
29+
}
30+
31+
public function getData()
32+
{
33+
return $this->data;
34+
}
35+
36+
/**
37+
* Get a data field using a "dot notation" path.
38+
*/
39+
public function get($key, $default = null)
40+
{
41+
// Since we are running under laravel, use laravel's helper.
42+
43+
return data_get($this->data, $key, $default);
44+
}
45+
46+
public function jsonSerialize()
47+
{
48+
return $this->data;
49+
}
50+
}

src/Model.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Consilience\OdooApi;
4+
5+
/**
6+
* Default model for a module record instance.
7+
*/
8+
9+
class Model implements ModelInterface
10+
{
11+
use HasModelDataTrait;
12+
}

src/ModelInterface.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Consilience\OdooApi;
4+
5+
/**
6+
*
7+
*/
8+
9+
use JsonSerializable;
10+
11+
interface ModelInterface extends JsonSerializable
12+
{
13+
public function get($key, $default = null);
14+
}

0 commit comments

Comments
 (0)