Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine entity manager find by empty array collection

I have an entity called Cycle, with a OneToMany association to CycleActeur (see code below).

I'd like to be able to fetch all Cycle objets in database with no CycleActeur objects associated, using a simple doctrine findBy* method from my controller.

That is to say something like this :

$manager = $this->getContainer()->get('doctrine.orm.entity_manager');
$cycleManager =  $manager->getRepository('ESI67Zen2Bundle:Cycle');
$cyclesWithNoCycleActeur = $cycleManager->findBy('acteurs', null);

Is there a way to do this without having to write a specific method in the CycleRepository ?

Extract from the Cycle class code

class Cycle {
  /**
   * @ORM\OneToMany(
   *      targetEntity="CycleActeur", 
   *      mappedBy="cycle", 
   *      orphanRemoval=true)
   */
  private $acteurs;
}

Extract from the Cycle class code

class CycleActeur {
 /**
  * @var Cycle Le cycle concerné
  * 
  * @ORM\ManyToOne(targetEntity="Cycle", inversedBy="acteurs")
  * @ORM\JoinColumn(name="cycle_id", referencedColumnName="id")
  * 
  */
  private $cycle;
}
like image 470
Turquoise Avatar asked Mar 08 '23 14:03

Turquoise


2 Answers

Your Cycle entity is the inverse side of relationship and it's table in database has no 'acteurs' column, so you cannot use findBy(['acteurs'=>null]) or findByActeurs(null). But you can do something anyway:

$manager = $this->getContainer()->get('doctrine.orm.entity_manager');
$cycleManager =  $manager->getRepository('ESI67Zen2Bundle:Cycle');
$allCycles = $cycleManager->findAll();

$cyclesWithNoCycleActeur = [];
foreach($allCycles as $cycle)
{
    if($cycle->getActeurs()->isEmpty())
    {
        $cyclesWithNoCycleActeur[] = $cycle;
    }
}
like image 97
Snegirekk Avatar answered Mar 16 '23 20:03

Snegirekk


In this case (to my mind) the best way is to use DQL's condition IS EMPTY:

  $manager
    ->createQueryBuilder()
    ->from(Cycle::class, 'cycle')
    ->select('cycle')
    ->andWhere('cycle.acteurs IS EMPTY')
    ->getQuery()
    ->getResult()
    ;

You can use this code in the EntityRepository or anywhere you have access to the EntityManager.

Source: Doctrine documentation.

like image 25
Alexander Yakutskiy Avatar answered Mar 16 '23 18:03

Alexander Yakutskiy