找不到具有Doctrine 2的2个表之间的关联

时间:2011-06-27 13:41:02

标签: zend-framework join doctrine-orm

我正在尝试在MySQL表的查询中进行基本连接。在Stackoverflow上阅读documentationsome questions之后,我仍然无法让它工作......

错误: 消息:[语义错误]第0行,第74列附近'o':错误:类Client_Model_Client没有名为Contact_Model_Contact的关联

这里的代码是我的模型和我正在处理的查询:

模型客户

<?php
/**
 * @Table(name="client_clients")
 * @Entity(repositoryClass="Client_Model_ClientRepository")
 */
class Client_Model_Client {

public function __construct()
{
    $this->contacts = new \Doctrine\Common\Collections\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)

模型联系

<?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;

查询

    $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;

我做错了什么? 它应该如何运作?

1 个答案:

答案 0 :(得分:1)

你应该这样做:

$qb = $this->_em->createQueryBuilder()
    ->select('i, o')
    ->from('Client_Model_Client', 'i')
    ->join('i.contact_data', 'o');