@@ -474,47 +474,96 @@ public function version()
474
474
*/
475
475
public function getResourceId (string $ externalId , string $ model = null )
476
476
{
477
+ $ resourceIds = $ this ->getResourceIds ([$ externalId ], $ model );
478
+
479
+ return collect ($ resourceIds )->first ();
480
+ }
481
+
482
+ /**
483
+ * Get multiple resource IDs at once.
484
+ *
485
+ * @param array $externalIds each either "name" or "module.name"
486
+ * @param string $module optional, but recommended
487
+ * @return int|null
488
+ *
489
+ * FIXME: all external IDs must have the same "module" at the moment.
490
+ */
491
+ public function getResourceIds (
492
+ array $ externalIds ,
493
+ string $ model = null ,
494
+ $ offset = 0 ,
495
+ $ limit = self ::DEFAULT_LIMIT ,
496
+ $ order = ''
497
+ ) {
477
498
$ criteria = [];
478
499
479
500
if ($ model !== null ) {
480
501
$ criteria [] = ['model ' , '= ' , 'res.partner ' ];
481
502
}
482
503
483
- if (strpos ($ externalId , '. ' ) !== false ) {
484
- list ($ module , $ name ) = explode ('. ' , $ externalId , 2 );
504
+ $ moduleList = [];
485
505
486
- $ criteria [] = ['module ' , '= ' , $ module ];
487
- } else {
488
- $ name = $ externalId ;
506
+ foreach ($ externalIds as $ externalId ) {
507
+ if (strpos ($ externalId , '. ' ) !== false ) {
508
+ list ($ module , $ name ) = explode ('. ' , $ externalId , 2 );
509
+ } else {
510
+ $ name = $ externalId ;
511
+ $ module = '{none} ' ;
512
+ }
513
+
514
+ if (! array_key_exists ($ module , $ moduleList )) {
515
+ $ moduleList [$ module ] = [];
516
+ }
517
+
518
+ $ moduleList [$ module ][] = $ name ;
489
519
}
490
520
491
- $ criteria [] = ['name ' , '= ' , $ name ];
521
+ // TODO: work out how to represent the boolean OR operator
522
+ // for multiple modules fetched at once.
523
+ // Each set of conditions in this loop should be ORed with
524
+ // every other set of conditions in this loop.
525
+ // So we should be able to search for "foo.bar_123" and "fing.bing_456"
526
+ // in one query, giving us conceptually:
527
+ // ((module = foo and name = bar_123) or (module = fing and name = bing_456))
528
+
529
+ foreach ($ moduleList as $ module => $ externalIds ) {
530
+ if ($ module !== '{none} ' ) {
531
+ $ criteria [] = ['module ' , '= ' , $ module ];
532
+ }
533
+
534
+ $ criteria [] = ['name ' , 'in ' , $ externalIds ];
535
+ }
492
536
493
- $ irModelDataIds = $ this ->searchArray ('ir.model.data ' , $ criteria );
494
- $ irModelDataId = collect ($ irModelDataIds )->first ();
537
+ $ irModelDataIds = $ this ->searchArray (
538
+ 'ir.model.data ' ,
539
+ $ criteria ,
540
+ $ offset ,
541
+ $ limit ,
542
+ $ order
543
+ );
495
544
496
- if ($ irModelDataId === null ) {
545
+ if (empty ( $ irModelDataIds ) ) {
497
546
// No matches found, so give up now.
498
547
return ;
499
548
}
500
549
501
- // Now read the full record to get the resource ID .
550
+ // Now read the full records to get the resource IDs .
502
551
503
552
$ irModelDataArray = $ this ->readArray (
504
553
'ir.model.data ' ,
505
- [ $ irModelDataId ]
554
+ $ irModelDataIds
506
555
);
507
- $ irModelData = collect ($ irModelDataArray )-> first () ;
556
+ $ irModelData = collect ($ irModelDataArray );
508
557
509
558
if ($ irModelData === null ) {
510
559
// We could not find the record.
511
560
// (We really should have, since we just looked it up)
512
561
return ;
513
562
}
514
563
515
- // Return the resource ID .
564
+ // Return the resource IDs .
516
565
517
- return $ irModelData[ 'res_id ' ] ;
566
+ return $ irModelData-> pluck ( 'res_id ' )-> toArray () ;
518
567
}
519
568
520
569
/**
0 commit comments