0

The "in" property used in Extbase does not seem to be working for me.

$actor contains an array of Actor model objects. My Movie model and Actor are in m:n relation.

I tried something like this in my Movie Repository:

$query=$this->createQuery();
$query->matching($query->in('actors',$actors));
$result = $query->execute()->toArray();

$result is showing NULL

I tried passing array of actor uids too but that wont work as well:

$query->matching($query->in('actors',[$actor_1_uid,$actor_2_uid]));

There is of course contains but using in should be more convenient.

dora
  • 1,314
  • 2
  • 22
  • 38

2 Answers2

3

I don't see any problem in your statement. Just to be clear, a "in" statement must be placed somewhere inside a matching statement, which is correct in your case.

However, you should change your create query for

$query = $this->createQuery();

instead of

$query=$this->create->query();

If you have still no result, I suggest you check the exact the SQL statement executed by extbase, there's a tricky way to do it in TYPO3.

You have to locate the following file in the core:/typo3/sysext/extbase/Classes/Persistence/Generic/Storage/Typo3DbBackend.php

locate the replacePlaceHolders function and add the following codes at the end of the function:

if (strpos( $sqlString, "my_table_name" ) !== false) {
echo $sqlString;
}

I will echo every statement that is being made for the following "my_table_name" table. Of course, never do that in your production server.

I hope it will help! Cheers, Olivier

DaikonBoy
  • 158
  • 6
2

Sorry, but $query->in is the wrong approach. AFAIK it will not work for m:n reations, only for 1:n.

Try something like this, $actors being a query result from the model or the actors repository:

$constraints = array();
foreach ($actors as $actor) {
  $constraints[] = $query->contains('actors', $actor->getUid());
}

if (!empty($constraints)) {
  $result = $query->matching($query->logicalOr($constraints))->execute();
}

Of course you can use your own array of uids for the loop, then just drop the getUid() method

Mateng
  • 3,742
  • 5
  • 37
  • 64