Doctrine & Symfony2 连接多个表
Doctrine & Symfony2 join multiple tables
我一直在努力在 DQL 中进行多重连接。
这是我的代码:
$query = $em->createQuery(
'SELECT k
FROM AppBundle:Keyword k
JOIN k.company c
JOIN k.entry e
WHERE c.user = :id
ORDER BY k.name ASC'
)->setParameter('id',$user_id);
但它在执行时给了我 "Notice: Undefined index: entry"。
这是我的关键字实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Keyword
*/
class Keyword
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Keyword
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $user;
/**
* Constructor
*/
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add user
*
* @param \AppBundle\Entity\User $user
* @return Keyword
*/
public function addUser(\AppBundle\Entity\User $user)
{
$this->user[] = $user;
return $this;
}
/**
* Remove user
*
* @param \AppBundle\Entity\User $user
*/
public function removeUser(\AppBundle\Entity\User $user)
{
$this->user->removeElement($user);
}
/**
* Get user
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
/**
* Set user
*
* @param \AppBundle\Entity\User $user
* @return Keyword
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $company;
/**
* Add company
*
* @param \AppBundle\Entity\Company $company
* @return Keyword
*/
public function addCompany(\AppBundle\Entity\Company $company)
{
$this->company[] = $company;
return $this;
}
/**
* Remove company
*
* @param \AppBundle\Entity\Company $company
*/
public function removeCompany(\AppBundle\Entity\Company $company)
{
$this->company->removeElement($company);
}
/**
* Get company
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCompany()
{
return $this->company;
}
/**
* Set company
*
* @param \AppBundle\Entity\Company $company
* @return Keyword
*/
public function setCompany(\AppBundle\Entity\Company $company = null)
{
$this->company = $company;
return $this;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $entry;
/**
* Add entry
*
* @param \AppBundle\Entity\Entry $entry
* @return Keyword
*/
public function addEntry(\AppBundle\Entity\Entry $entry)
{
$this->entry[] = $entry;
return $this;
}
/**
* Remove entry
*
* @param \AppBundle\Entity\Entry $entry
*/
public function removeEntry(\AppBundle\Entity\Entry $entry)
{
$this->entry->removeElement($entry);
}
/**
* Get entry
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getEntry()
{
return $this->entry;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $ranking;
/**
* Add ranking
*
* @param \AppBundle\Entity\Ranking $ranking
* @return Keyword
*/
public function addRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking[] = $ranking;
return $this;
}
/**
* Remove ranking
*
* @param \AppBundle\Entity\Ranking $ranking
*/
public function removeRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking->removeElement($ranking);
}
/**
* Get ranking
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRanking()
{
return $this->ranking;
}
}
还有我的参赛实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Entry
*/
class Entry
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $path;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* @param string $path
* @return Entry
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @var \AppBundle\Entity\Keyword
*/
private $keyword;
/**
* Set keyword
*
* @param \AppBundle\Entity\Keyword $keyword
* @return Entry
*/
public function setKeyword(\AppBundle\Entity\Keyword $keyword = null)
{
$this->keyword = $keyword;
return $this;
}
/**
* Get keyword
*
* @return \AppBundle\Entity\Keyword
*/
public function getKeyword()
{
return $this->keyword;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $ranking;
/**
* Constructor
*/
public function __construct()
{
$this->ranking = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add ranking
*
* @param \AppBundle\Entity\Ranking $ranking
* @return Entry
*/
public function addRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking[] = $ranking;
return $this;
}
/**
* Remove ranking
*
* @param \AppBundle\Entity\Ranking $ranking
*/
public function removeRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking->removeElement($ranking);
}
/**
* Get ranking
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRanking()
{
return $this->ranking;
}
}
顺便说一句,我对 symfony 和 doctrine 还很陌生。
我感谢各种帮助!
您需要为模型 class 中的每个属性提供映射信息,并为 class 提供实体映射。看看http://doctrine-common.readthedocs.org/en/latest/reference/annotations.html
您的每个模型 classes 都需要 @ORM\Entity 注释来告诉学说它是一个映射实体。因此,对于您的情况,您将拥有:
/**
* Entry
* @ORM\Entity
*/
class Entry
{
...
那么每个你想映射到数据库的属性都需要一个@ORM\Column注解。例如:
/**
* @var integer
* @ORM\Id @ORM\Column @ORM\GeneratedValue
*/
private $id;
/**
* @var string
* @ORM\Column(type="string")
*/
private $path;
然后您需要为模型之间的任何关系(关键字 -> 公司、关键字 -> 条目等)创建关系映射注释,使用此处的映射之一 http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html
获得所有正确的映射后,使用命令行工具 app/console doctrine:schema:update 确保您的模型与数据库同步。
您的 DQL 看起来不错,所以一旦您拥有正确的映射,您的运气可能会更好。
我一直在努力在 DQL 中进行多重连接。 这是我的代码:
$query = $em->createQuery(
'SELECT k
FROM AppBundle:Keyword k
JOIN k.company c
JOIN k.entry e
WHERE c.user = :id
ORDER BY k.name ASC'
)->setParameter('id',$user_id);
但它在执行时给了我 "Notice: Undefined index: entry"。
这是我的关键字实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Keyword
*/
class Keyword
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $name;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Keyword
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $user;
/**
* Constructor
*/
public function __construct()
{
$this->user = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add user
*
* @param \AppBundle\Entity\User $user
* @return Keyword
*/
public function addUser(\AppBundle\Entity\User $user)
{
$this->user[] = $user;
return $this;
}
/**
* Remove user
*
* @param \AppBundle\Entity\User $user
*/
public function removeUser(\AppBundle\Entity\User $user)
{
$this->user->removeElement($user);
}
/**
* Get user
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getUser()
{
return $this->user;
}
/**
* Set user
*
* @param \AppBundle\Entity\User $user
* @return Keyword
*/
public function setUser(\AppBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $company;
/**
* Add company
*
* @param \AppBundle\Entity\Company $company
* @return Keyword
*/
public function addCompany(\AppBundle\Entity\Company $company)
{
$this->company[] = $company;
return $this;
}
/**
* Remove company
*
* @param \AppBundle\Entity\Company $company
*/
public function removeCompany(\AppBundle\Entity\Company $company)
{
$this->company->removeElement($company);
}
/**
* Get company
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCompany()
{
return $this->company;
}
/**
* Set company
*
* @param \AppBundle\Entity\Company $company
* @return Keyword
*/
public function setCompany(\AppBundle\Entity\Company $company = null)
{
$this->company = $company;
return $this;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $entry;
/**
* Add entry
*
* @param \AppBundle\Entity\Entry $entry
* @return Keyword
*/
public function addEntry(\AppBundle\Entity\Entry $entry)
{
$this->entry[] = $entry;
return $this;
}
/**
* Remove entry
*
* @param \AppBundle\Entity\Entry $entry
*/
public function removeEntry(\AppBundle\Entity\Entry $entry)
{
$this->entry->removeElement($entry);
}
/**
* Get entry
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getEntry()
{
return $this->entry;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $ranking;
/**
* Add ranking
*
* @param \AppBundle\Entity\Ranking $ranking
* @return Keyword
*/
public function addRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking[] = $ranking;
return $this;
}
/**
* Remove ranking
*
* @param \AppBundle\Entity\Ranking $ranking
*/
public function removeRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking->removeElement($ranking);
}
/**
* Get ranking
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRanking()
{
return $this->ranking;
}
}
还有我的参赛实体:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Entry
*/
class Entry
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $path;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set path
*
* @param string $path
* @return Entry
*/
public function setPath($path)
{
$this->path = $path;
return $this;
}
/**
* Get path
*
* @return string
*/
public function getPath()
{
return $this->path;
}
/**
* @var \AppBundle\Entity\Keyword
*/
private $keyword;
/**
* Set keyword
*
* @param \AppBundle\Entity\Keyword $keyword
* @return Entry
*/
public function setKeyword(\AppBundle\Entity\Keyword $keyword = null)
{
$this->keyword = $keyword;
return $this;
}
/**
* Get keyword
*
* @return \AppBundle\Entity\Keyword
*/
public function getKeyword()
{
return $this->keyword;
}
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $ranking;
/**
* Constructor
*/
public function __construct()
{
$this->ranking = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add ranking
*
* @param \AppBundle\Entity\Ranking $ranking
* @return Entry
*/
public function addRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking[] = $ranking;
return $this;
}
/**
* Remove ranking
*
* @param \AppBundle\Entity\Ranking $ranking
*/
public function removeRanking(\AppBundle\Entity\Ranking $ranking)
{
$this->ranking->removeElement($ranking);
}
/**
* Get ranking
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getRanking()
{
return $this->ranking;
}
}
顺便说一句,我对 symfony 和 doctrine 还很陌生。 我感谢各种帮助!
您需要为模型 class 中的每个属性提供映射信息,并为 class 提供实体映射。看看http://doctrine-common.readthedocs.org/en/latest/reference/annotations.html
您的每个模型 classes 都需要 @ORM\Entity 注释来告诉学说它是一个映射实体。因此,对于您的情况,您将拥有:
/**
* Entry
* @ORM\Entity
*/
class Entry
{
...
那么每个你想映射到数据库的属性都需要一个@ORM\Column注解。例如:
/**
* @var integer
* @ORM\Id @ORM\Column @ORM\GeneratedValue
*/
private $id;
/**
* @var string
* @ORM\Column(type="string")
*/
private $path;
然后您需要为模型之间的任何关系(关键字 -> 公司、关键字 -> 条目等)创建关系映射注释,使用此处的映射之一 http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html
获得所有正确的映射后,使用命令行工具 app/console doctrine:schema:update 确保您的模型与数据库同步。
您的 DQL 看起来不错,所以一旦您拥有正确的映射,您的运气可能会更好。