INNER JOIN查询+ WHERE不起作用 [英] INNER JOIN Query + WHERE doesn't work
问题描述
我有两个表格:联系人和 contactpersonlocale 。
表联系人
- contactpersonID(主键)
- 标签(VARCHAR)
表 contactpersonlocale :
- contactpersonlocaleID主键)
- contactpersonID(联系人表的外键)
- 函数(VARCHAR)
- name VARCHAR)
- locale(VARCHAR)
- contactpersonlocaleID (Primary Key)
- contactpersonID (Foreign Key to contactperson table)
- function (VARCHAR)
- name (VARCHAR)
- locale (VARCHAR)
在我的联络人实体中我有:
In my Contactperson Entity I have:
/**
* @var integer
*
* @ORM\Column(name="contactpersonID", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $contactpersonid;
/**
* @ORM\OneToMany(targetEntity="DX\MurisBundle\Entity\Contactpersonlocale", mappedBy="contactpersonid", cascade={"persist", "remove", "merge"}, orphanRemoval=true)
*/
protected $contactpersonlocale;
/**
* Set contactpersonlocale
*
* @param \DX\MurisBundle\Entity\Contactpersonlocale $contactpersonlocale
* @return Contactpersonlocale
*/
public function setContactpersonlocale(\DX\MurisBundle\Entity\Contactpersonlocale $contactpersonlocale = null)
{
$this->contactpersonlocale = $contactpersonlocale;
return $this;
}
/**
* Get contactpersonlocale
*
* @return \DX\MurisBundle\Entity\Contactpersonlocale
*/
public function getContactpersonlocale()
{
return $this->contactpersonlocale;
}
在我的Contactpersonlocale实体中我有:
In my Contactpersonlocale Entity I have:
/**
* @var \DX\MurisBundle\Entity\Contactperson
*
* @ORM\ManyToOne(targetEntity="DX\MurisBundle\Entity\Contactperson")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="contactpersonID", referencedColumnName="contactpersonID")
* })
*/
private $contactpersonid;
在我的联系人资料库中我有:
In my Contactperson Repository I have:
public function getContactpersonen($locale = 'nl')
{
$cp = $this->createQueryBuilder('cp')
->select('cp')
->innerJoin('cp.contactpersonlocale', 'cpl')
->where('cpl.locale = :locale')
->setParameter('locale', $locale);
return $cp->getQuery()
->getResult();
}
现在,当我循环使用它们:
Now when I loop through them like this:
$contactpersonen = $em->getRepository('MurisBundle:Contactperson')->getContactpersonen($locale);
foreach($contactpersonen as $cp)
{
dump($cp->getcontactpersonlocale()->toArray()); die;
}
然后我得到两个personlocale对象,他不采取帐户中的区域设置(可以看到我做了WHERE locale = ..)。而且我的地区肯定是在.. ..
Then I get two personlocale objects, he doesn't take the locale in account (as you can see I do WHERE locale = ..). And my locale is definitely filled in .. .
可能是这个问题?
推荐答案
您必须在查询中使用WITH语句:
You have to use the WITH statement in your query:
$cp = $this->createQueryBuilder('cp')
->select('cp')
->innerJoin('cp.contactpersonlocale', 'cpl', 'WITH', 'cpl.locale = :locale')
->setParameter('locale', $locale);
如果您只想加入与locale = $ locale的Contactpersonlocale。
If you want to join only Contactpersonlocale with locale = $locale.
这篇关于INNER JOIN查询+ WHERE不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!