0

我构建了我的实体的以下部分:

/**
* @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 Code 时:

$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) 
4

2 回答 2

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');
于 2013-02-18T15:20:00.943 回答
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

来源: http: //oldforum.symfony-project.org/index.php/m/69061/

Mysql 加入:http ://dev.mysql.com/doc/refman/5.0/en/join.html

于 2013-02-18T15:12:59.833 回答