php - Symfony2 + Doctrine OneToMany JOIN failing

604

Trying to use the following code to join my 2 tables:

    $query = $em->createQuery('SELECT a, c FROM AppBundle:Auctions a JOIN a.catalog c');        

    $auctions = $query->getResult();

And getting the error: [Semantical Error] line 0, col 53 near 'c': Error: Class AppBundle\Entity\Auctions has no association named catalog

My tables are defined like this:

Auctions.php:

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Auctions
 *
 * @ORM\Table(name="auctions")
 * @ORM\Entity
 */
class Auctions
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var \Doctrine\Common\Collections\ArrayCollection
 * 
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Catalogs", mappedBy="auction")
 */
private $catalog;
}

And Catalogs.php:

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * Catalogs
 *
 * @ORM\Table(name="catalogs", indexes={@ORM\Index(name="catalogs_FI_1", columns={"treasurer_info_id"}), @ORM\Index(name="catalogs_FI_2", columns={"auction_id"}), @ORM\Index(name="unique_stripped_name", columns={"stripped_name"})})
 * @ORM\Entity
 */
class Catalogs
{
    /**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var \AppBundle\Entity\Auctions
 * 
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Auctions", inversedBy="catalog")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="auction_id", referencedColumnName="id")
 * })
 */
private $auction;             

Not really sure what I'm doing wrong here, it seems like all of the doctrine examples for OneToMany work like this? Any help is appreciated.

UPDATE: updated the schema files and query being used.

623

Answer

Solution:

You don't want yourOneToMany association on your primary key. What you want is another variable in yourAuctions entity that relates toCatalogs:

/**
 * @var \Doctrine\Common\Collections\ArrayCollection
 *
 * @ORM\OneToMany(targetEntity="Catalogs", mappedBy="auction")
 */
private $catalogs;

Then update yourCatalogs entity:

/**
 * @var \AppBundle\Entity\Auctions
 * 
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Auctions", inversedBy="catalogs")
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="auction_id", referencedColumnName="id")
 * })
 */
private $auction;

Then your join would look like this:

$query = $em->createQuery('SELECT a, c FROM AppBundle:Auctions a JOIN a.catalogs c');
$auctions = $query->getResult();

What you want to remember when dealing with Doctrine is that you are thinking of your entities in terms of how they relate to each other, not how they would relate in a SQL query. You're defining theid mappings inside your entity so that when you writeDQL you can join based on your association rather than the actual column names.

People are also looking for solutions to the problem: php - functions not showing tables

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.