问题描述:

I built the following Parts of my Entities :

/**

* @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;

}

When I now try the following Doctrine Code :

$this->oQueryBuilder->select('contact','ticket')

->from('Application_Model_Contact', 'contact')

->leftJoin('contact.tickets', 'ticket')

->orderBy('contact.id', 'DESC');

I get a MYSQL-Error.

The following happens :

FROM contact c0_ LEFT JOIN ORDER BY c0_.id DESC

It seems that Doctrine doesn't fill in the LEFT JOIN properly.

Does anybody have an idea, why this happens ?

UPDATE:

->leftJoin('contact.tickets', 'ticket', 'WITH','contact.id = ticket.contactsId')

Gives me

LEFT JOIN AND (c0_.id = t1_.contacts_id)

网友答案:

you miss the ON in your query

$this->oQueryBuilder
     ->select('contact','ticket')
     ->from('Application_Model_Contact', 'contact')
     ->leftJoin('contact', 'on contact.ticket=Application_Model_Contact.field')
     ->orderBy('contact.id', 'DESC');

where Application_Model_Contact.field replace it for the match field to apply the JOIN

so your query should be like:

....FROM contact c0_ LEFT JOIN ON table1.field=table2.field ORDER BY c0_.id DESC

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

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

网友答案:

Try this:

$this->oQueryBuilder->select('contact','ticket')
                    ->from('Application_Model_Contact', 'contact')
                    ->leftJoin('contact.tickets', 'ticket', 'WITH', 'contact.id = ticket.contacts_id')
                    ->orderBy('contact.id', 'DESC');
相关阅读:
Top