TYPO3 Extbase repository constraint with MM relation

1.3k Views Asked by At

I build an extension with a table for items, these can be either projects or objects, a project being a container for multple objects.

To distinguish, a checkbox is used to label an item as project, and when this checkbox is ticked an optional field is displayed. This field is a relation (m:n) from the project to the objects it contains (same table). The Multiple side by side select displayes only non projects and objects not yet assigned to a project through foreign_table_where.

This field has following TCA:

'objects' => [
  'displayCond' => 'FIELD:isproject:=:1',
  'exclude' => 0,
  'label' => $ll . 'tx_myext_domain_model_item.objects',
  'config' => [
    'type' => 'select',
    'renderType' => 'selectMultipleSideBySide',
    'foreign_table' => 'tx_myext_domain_model_item',
    'foreign_table_where' => 'AND isproject = 0 AND tx_myext_domain_model_item.uid NOT IN (SELECT uid_foreign FROM tx_myext_item_object_mm WHERE uid_local != ###THIS_UID###)',
    'MM' => 'tx_myext_item_object_mm',
    'size' => 10,
    'autoSizeMax' => 30,
    'maxitems' => 9999,
    'multiple' => 0
  ],
],

with my plugin I give the option (trough a flexform) to select to display only objects, only projects or both, done with following code in the repository:

public function findList($entryInclude = 'objects_only') {

 /** @var \TYPO3\CMS\Extbase\Persistence\Generic\Query $query */
 $query = $this->createQuery();

 switch ($entryInclude) {
   case 'projects_objects':
     $foreign_uids = $this->createQuery()
       ->statement('SELECT uid_foreign FROM tx_myext_item_object_mm')
       ->execute();
     $constraints = [
       $query->equals('isproject', 1),
       $query->logicalNot($query->in('uid', $foreign_uids))
     ];
     break;
   case 'projects_only':
     $constraints = $query->equals('isproject', 1);
     break;
   default:
     $constraints = $query->equals('isproject', 0);
     break;
 }

 $query->matching($query->logicalAnd($constraints));

 return $query->execute();
}

the effort to build an array of all uid_foreign found in the tx_myext_item_object_mm table causes an error ...

1

There are 1 best solutions below

0
On

This is, in my oppinion, a case for a custom query. The following should give you the objects not referenced to from within your mm-table:

$query = $this->createQuery();
$query->statement('
    SELECT * 
    FROM tx_myext_domain_model_item 
    WHERE uid NOT IN(
        SELECT foreign_uid FROM tx_myext_item_object_mm
    )
');
$query->execute();