Skip to content

Commit a7b48bb

Browse files
committed
Added method to look up internal resource ID from external ID.
1 parent 8dd8942 commit a7b48bb

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

src/OdooClient.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,58 @@ public function read(
285285
// TODO: actions to implement = create write unlink
286286
//
287287

288+
/**
289+
* Get the ERP internal resource ID for a given external ID.
290+
*
291+
* @param string $externalId either "name" or "module.name"
292+
* @param string $module optional, but recommended
293+
* @return int|null
294+
*/
295+
public function getResourceId(string $externalId, string $model = null)
296+
{
297+
$criteria = [];
298+
299+
if ($model !== null) {
300+
$criteria[] = ['model', '=', 'res.partner'];
301+
}
302+
303+
if (strpos($externalId, '.') !== false) {
304+
list ($module, $name) = explode('.', $externalId, 2);
305+
306+
$criteria[] = ['module', '=', $module];
307+
} else {
308+
$name = $externalId;
309+
}
310+
311+
$criteria[] = ['name', '=', $name];
312+
313+
$result = $this->search('ir.model.data', $criteria);
314+
$irModelDataIds = $this->valueToNative($result->value());
315+
$irModelDataId = collect($irModelDataIds)->first();
316+
317+
if ($irModelDataId === null) {
318+
// No matches found, so give up now.
319+
return;
320+
}
321+
322+
// Now read the full record to get the resource ID.
323+
324+
$irModelDataArray = $this->valueToNative(
325+
$this->read('ir.model.data', [$irModelDataId])->value()
326+
);
327+
$irModelData = collect($irModelDataArray)->first();
328+
329+
if ($irModelData === null) {
330+
// We could not find the record.
331+
// (We really should have, since we just looked it up)
332+
return;
333+
}
334+
335+
// Return the resource ID.
336+
337+
return $irModelData['res_id'];
338+
}
339+
288340
/**
289341
* Return a message with the base parameters for any object call.
290342
* Identified the login credentials, model and action.

0 commit comments

Comments
 (0)