Symfony2: Doctrine does not load related objects in many ways to many

I have a many-to-many relationship, and when I load an object that is on the one hand, this relationship, I expect to see its ArrayCollection property of related objects on the other hand. However, this does not happen - there are no elements in the loaded ArrayCollection array, while in the database I see related records. What could be the reason?

Here is my code:
One side of the relationship, the ConsolidatedReport class:

/** * @var ArrayCollection * * @ORM\ManyToMany(targetEntity="P24\Response", inversedBy="consolidatedReports") * @ORM\JoinTable(name="con_rprt_responses") */ private $responses; 

The other side of the relationship, the response class:

 /** * @var ArrayCollection * * @ORM\ManyToMany(targetEntity="P24\ConsolidatedReport\ConsolidatedReport", mappedBy="responses") */ private $consolidatedReports; 

Here is the function that I run to get an instance of ConsolidatedReport. This function is inside the service called from the container:

 /** * Picks the consolidated report with given id. * * @param string $id * * @return ConsolidatedReport * * @throws NonExistentConsolidatedReportException if the survey doesn't exist */ public function pick($id) { $report = $this->repository->findOneBy(array('id' => $id)); if (!$report) { throw new NonExistentConsolidatedReportException($id); } return $report; }' 

The database has a table called "con_rprt_responses" with two columns, "united_reports_id" and "response_id". However, in the profiler, I do not see any queries on this table.

What could go wrong here?

UPDATE: Please see my answer to this question below, which worked for me.

+6
source share
4 answers

I added fetch="EAGER" to the $ response property of the ConsolidatedReport class and it worked.

Now the code is as follows:

 /** * @var ArrayCollection * * @ORM\ManyToMany(targetEntity="P24\Response", inversedBy="consolidatedReports", fetch="EAGER") * @ORM\JoinTable(name="con_rprt_responses") */ private $responses; 

More details here: http://doctrine-orm.readthedocs.org/en/latest/reference/working-with-objects.html#by-eager-loading

Nevertheless, if someone knows why the collection of the associated object will not be loaded without explicitly specifying the EAGER selection, please share your knowledge, we greatly appreciate it!

+5
source

If you specify joinColumns, will this solve your problem?

 /** * @ORM\ManyToMany(targetEntity="P24\Response", inversedBy="consolidatedReports") * @ORM\JoinTable(name="con_rprt_responses", * joinColumns={@ORM\JoinColumn(name="consolidated_reports_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="response_id", referencedColumnName="id")} * ) */ 
0
source

* ToMany properties must be initialized with an ArrayCollection.

 public function __construct() { $this->responses = new \Doctrine\Common\Collections\ArrayCollection(); $this-> consolidatedReports = new \Doctrine\Common\Collections\ArrayCollection(); } 
0
source

If you have more than one query to retrieve the same objects using Doctrine, try using:

 $entityManager->clear(); 

between them to fix the "missing" entities. This is not an β€œas is” solution, but it can give you an idea of ​​something wrong in your request chain.

0
source

All Articles