Reputation: 485
I am trying to get all records from "Cours" table where the value of $speciality
which is submitted by a form is IN the arrayColloction()
returned by each one single object of class Cours
. I am using the next lines to have that result (but unfortunately it doesn't work):
public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality )
{
$qb
->andWhere($qb->expr()->in(':speciality','a.specialities'))
->setParameter('speciality', $speciality) ;
return $qb;
}
The class Cours
has a ManyToMany
relation like in the following code:
/**
* @ORM\ManyToMany(targetEntity="BacUp\GeneralBundle\Entity\Speciality", cascade={"persist"})
* @ORM\JoinColumn(nullable=false)
* @Assert\Count(min = 1, minMessage = "You have to choose at least one speciality")
*/
private $specialities;
The execution returns the following error:
CRITICAL - Uncaught PHP Exception Symfony\Component\Debug\Exception\ContextErrorException: "Catchable Fatal Error: Object of class BacUp\GeneralBundle\Entity\Speciality could not be converted to string in C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Expr.php line 452" at C:\wamp\www\Symfony\vendor\doctrine\orm\lib\Doctrine\ORM\Query\Expr.php line 452
Upvotes: 2
Views: 1984
Reputation: 485
I resolve the issue, I post the solution here, it can maybe help somebody else... You have to use the "MEMBER OF" clause to treat a manytomany relation, and you maybe need to inject the following code directley in the Expr.php file:
/**
* Creates an instance of MEMBER OF function, with the given arguments.
*
* @param string $x Value to be checked
* @param string $y Value to be checked against
*
* @return string
*/
public function isMemberOf($x, $y)
{
return $x . ' MEMBER OF ' . $y;
}
For more information, go on here (Maybe you need to use INSTANCE OF also? same way as above) Now, in my code I used the "isMemberOf" function and that worked fine.
public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality )
{
$qb
->andWhere($qb->expr()->isMemberOf(':speciality','a.specialities'))
->setParameter('speciality', $speciality) ;
return $qb;
}
Hope that can help.
Upvotes: 0
Reputation: 1181
i didn't test this but using Member of
should resolve the issue
public function andWhereSpeciality(QueryBuilder $qb, Speciality $speciality )
{
$qb
->andWhere($qb->expr()->isMemberOf(':speciality','a.specialities'))
->setParameter('speciality', $speciality) ;
return $qb;
}
Upvotes: 1