Associations between 2 tables with Doctrine 2 not found
I'm trying to workout a basic join in a query for MySQL-tables. After reading the documentation and some questions on Stackoverflow, I still can't get it to work ...
The error: Message: [Semantical Error] line 0, col 74 near 'o': Error: Class Client_Model_Client has no association named Contact_Model_Contact
The code here, are my models and the query I'm working on:
Model client
<?php
/**
* @Table(name="client_clients")
* @Entity(repositoryClass="Client_Model_ClientRepository")
*/
class Client_Model_Client {
public function __construct()
{
$this->contacts = new \Doctrine\Common\开发者_StackOverflowCollections\ArrayCollection();
}
/**
* @var integer $id
* @Id @Column(type="integer")
* @GeneratedValue
*/
private $id;
/**
* @var string $name
* @Column(type="string")
*/
private $name;
/**
* @var string $address
* @Column(type="string")
*/
private $address;
/**
* @var string $comment
* @Column(type="string")
*/
private $comment;
/**
* @var integer $account_id
* @Column(type="integer")
*/
private $account_id;
/**
* @var integer $contact_id
* @Column(type="integer")
*/
private $contact_id;
/**
* @var string $created_at
* @Column(type="string")
*/
private $created_at;
/**
* @var contact_data $contact_data
* @Column(name="contact_data", type="integer", nullable=false)
* @OneToMany(targetEntity="Contact_Model_Contact", mappedBy="contact_data")
* @JoinColumn(name="contact_id", referencedColumnName="id")
*/
private $contact_data;
... (getters and setters)
Model Contact
<?php
/**
* @Table(name="contact_contacts")
* @Entity(repositoryClass="Contact_Model_ContactRepository")
*/
class Contact_Model_Contact {
/**
* @var integer $id
* @Id @Column(type="integer")
* @GeneratedValue
* @ManyToOne(targetEntity="Client_Model_Client", mappedBy="contact_data")
*/
private $id;
/**
* @var string $name
* @Column(type="string")
*/
private $name;
/**
* @var string $email
* @Column(type="string")
*/
private $email;
/**
* @var string $phone
* @Column(type="string")
*/
private $phone;
/**
* @var integer $address
* @Column(type="string")
*/
private $address;
/**
* @var string $comment
* @Column(type="string")
*/
private $comment;
/**
* @var string $created_at
* @Column(type="string")
*/
private $created_at;
/**
* @var integer $contact_id
* @Column(type="string")
*/
private $contact_id;
The query
$qb = $this->_em->createQueryBuilder()
->select('i, o')
->from('Client_Model_Client', 'i')
->join('i.Contact_Model_Contact', 'o');
$query = $qb->getQuery();
$roles = $query->getResult();
Zend_Debug::dump($roles); die;
What am I doing wrong? How should it work?
You should be doing this:
$qb = $this->_em->createQueryBuilder()
->select('i, o')
->from('Client_Model_Client', 'i')
->join('i.contact_data', 'o');
精彩评论