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.
You don't want your OneToMany
association on your primary key. What you want is another variable in your Auctions
entity that relates to Catalogs
:
/**
* @var \Doctrine\Common\Collections\ArrayCollection
*
* @ORM\OneToMany(targetEntity="Catalogs", mappedBy="auction")
*/
private $catalogs;
Then update your Catalogs
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 the id
mappings inside your entity so that when you write DQL
you can join based on your association rather than the actual column names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.