未找到 2 个表与原则 2 之间的关联

发布于 2024-11-17 06:19:58 字数 3062 浏览 0 评论 0原文

我正在尝试练习 MySQL 表查询中的基本连接。阅读文档和< a href="http://groups.google.com/group/doctrine-user/browse_thread/thread/a9b63a7d57018da8?pli=1" rel="nofollow noreferrer">一些 问题 在 Stackoverflow 上,我仍然无法让它工作...

错误: 消息:[语义错误]第 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;

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

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\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)

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

陪我终i 2024-11-24 06:19:58

你应该这样做:

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

You should be doing this:

$qb = $this->_em->createQueryBuilder()
    ->select('i, o')
    ->from('Client_Model_Client', 'i')
    ->join('i.contact_data', 'o');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文