Vasily802
Vasily802

Reputation: 1848

Symfony2: Doctrine does not load related entities in many-to-many relation

I have a many-to-many-relation, and when I load an entity that is on one side this relation, I expect to see as its property the ArrayCollection of related entities on another side. However, this does not happen - the ArrayCollection loaded has no elements in it, while in the database I can see the related entries. What could be the reason?

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

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

Another side of the relation, Response class:

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

Here is the function I run to get an instance of ConsolidatedReport. This function sits inside a service that is being called from 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;
}'

In the database, there is "con_rprt_responses" table with two columns "consolidated_reports_id" and "response_id". However, in profiler I do not see any queries to that table.

What could go wrong here?

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

Upvotes: 12

Views: 8080

Answers (4)

vl.lapikov
vl.lapikov

Reputation: 796

In case you have more then single query to fetch the same objects using Doctrine try to use:

$entityManager->clear();

in between, to fix "missing" entities. It isn't solution "as is", however can give you an idea something wrong in chain of your queries.

Upvotes: 0

Vasily802
Vasily802

Reputation: 1848

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

The code now looks like this:

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

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

Still if someone knows why the collection of related entity would not load without explicitly specifying EAGER fetching - please share your knowledge, it is highly appreciated!

Upvotes: 14

LorenzSchaef
LorenzSchaef

Reputation: 1543

The *toMany properties have to be initialized with an ArrayCollection.

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

Upvotes: 0

ggavrilut
ggavrilut

Reputation: 378

If you specify the joinColumns, does 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")}
 * )
 */

Upvotes: 0

Related Questions