我正在尝试过滤掉父项由复合主键组成的子项,父项称为主题,子项是课程:
学科:
class Subject
{
/**
* @var integer
*
* @Column(type="integer")
* @Id
* @GeneratedValue(strategy="NONE")
*/
protected $id;
/**
* @var integer
*
* @Column(type="integer")
* @Id
*/
protected $customer_id;
/**
* @var ArrayCollection
*
* @OneToMany(targetEntity="Course", mappedBy="subject")
*/
private $courses;
...
}
课程:
class Course
{
...
/**
* @var Subject
*
* @ManyToOne(targetEntity="Subject", inversedBy="courses")
* @JoinColumns({
* @JoinColumn(name="subject_id", referencedColumnName="id"),
* @JoinColumn(name="subject_customer_id", referencedColumnName="customer_id")
* })
*/
private $subject;
}
我有“subject_id”和“subject_customer_id”,我的问题是我不能在没有加入主题的情况下过滤掉课程,当我写这个时:
$this->em->createQuery("SELECT c FROM Course c WHERE c.subject = :subject")
->setParameters( array('subject' => array( 'subject_id' => $subject_id, 'subject_customer_id' => $subject_customer_id ) ) )
->getResult();
我收到以下错误:
Doctrine\ORM\Query\QueryException [ 0 ]:不支持到具有复合主键的实体的单值关联路径表达式。在查询中显式命名复合主键的组件。
我能得到这个工作的唯一原因是通过内部加入主题,例如:
$this->em->createQuery("SELECT c FROM Course c INNER JOIN c.subject s WITH s.id = :subject AND s.customer_id = :customer")
->setParameters( array( 'subject' => $subject_id, 'customer' => $customer_id ) )
->getResult();
有没有什么方法可以在不加入学科的情况下根据学科主键检索课程?