我构建了我的实体的以下部分:
/**
* @var Application_Model_Ticket
*
* @OneToMany(targetEntity="Application_Model_Ticket",mappedBy="contacts_id")
* @JoinColumn(name="id", referencedColumnName="contacts_id")
*/
private $tickets;
public function getTickets() {
return $this->tickets;
}
/**
* @var Application_Model_Contact
*
* @ManyToOne(targetEntity="Application_Model_Contact",cascade={"persist"})
* @JoinColumn(name="contacts_id", referencedColumnName="id")
*/
private $contact;
public function getContact() {
return $this->contact;
}
当我现在尝试以下Doctrine代码时:
$this->oQueryBuilder->select('contact','ticket')
->from('Application_Model_Contact', 'contact')
->leftJoin('contact.tickets', 'ticket')
->orderBy('contact.id', 'DESC');
我收到MYSQL错误。 发生以下情况:
FROM contact c0_ LEFT JOIN ORDER BY c0_.id DESC
似乎Doctrine没有正确填写LEFT JOIN。 有没有人知道,为什么会这样?
更新:
->leftJoin('contact.tickets', 'ticket', 'WITH','contact.id = ticket.contactsId')
给我
LEFT JOIN AND (c0_.id = t1_.contacts_id)
答案 0 :(得分:0)
您错过了查询中的ON
$this->oQueryBuilder
->select('contact','ticket')
->from('Application_Model_Contact', 'contact')
->leftJoin('contact', 'on contact.ticket=Application_Model_Contact.field')
->orderBy('contact.id', 'DESC');
其中Application_Model_Contact.field
替换匹配字段以应用JOIN
所以你的查询应该是:
....FROM contact c0_ LEFT JOIN ON table1.field=table2.field ORDER BY c0_.id DESC
答案 1 :(得分:0)
试试这个:
$this->oQueryBuilder->select('contact','ticket')
->from('Application_Model_Contact', 'contact')
->leftJoin('contact.tickets', 'ticket', 'WITH', 'contact.id = ticket.contacts_id')
->orderBy('contact.id', 'DESC');