原则 2 未找到加入协会
我正在使用 Doctrine-2.0.4 和 Zend 1.11。 Php 映射器和实体是从 MySQL 数据库生成的。现在我正在尝试一个连接两个表的查询,并且只得到
[语义错误]第0行,第48列'e'附近:错误:类实体\用户没有名为帐户的关联
查询:
$query = $em->createQueryBuilder()
->select('u')
->from('\Entities\Users', 'u')
->leftJoin('u.Accounts', 'a')
->getQuery();
$info = $query->getResult();
在我的DB 有两个表:Users、Accounts
- Users 有字段 id、accountId
- Accounts 有字段 id、info
Users.accountId 与 Accounts 有双向一对一关联。 ID
<?php
namespace Entities;
/**
* Users
*
* @Table(name="users")
* @Entity
*/
class Users
{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer $accountid
*
* @Column(name="accountId", type="integer", nullable=false)
* @OneToOne(targetEntity="accounts", inversedBy="id")
* @JoinColumn(name="accounts_id", referencedColumnName="id")
*/
private $accountid;
...
}
<?php
namespace Entities;
/**
* Accounts
*
* @Table(name="accounts")
* @Entity
*/
class Accounts
{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
* @OneToOne(targetEntity="users", mappedBy="accountid")
*/
private $id;
...
}
I am using Doctrine-2.0.4 with Zend 1.11. Php Mappers and Entities are generated from a MySQL DB. Now I am trying a query that joins two tables and only ever get
[Semantical Error] line 0, col 48 near 'e': Error: Class Entities\Users has no association named Accounts
Query:
$query = $em->createQueryBuilder()
->select('u')
->from('\Entities\Users', 'u')
->leftJoin('u.Accounts', 'a')
->getQuery();
$info = $query->getResult();
In my DB there are two tables: Users, Accounts
- Users has fields id, accountId
- Accounts has fields id, info
Users.accountId has a bidirectional one-to-one association to Accounts.id
<?php
namespace Entities;
/**
* Users
*
* @Table(name="users")
* @Entity
*/
class Users
{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var integer $accountid
*
* @Column(name="accountId", type="integer", nullable=false)
* @OneToOne(targetEntity="accounts", inversedBy="id")
* @JoinColumn(name="accounts_id", referencedColumnName="id")
*/
private $accountid;
...
}
<?php
namespace Entities;
/**
* Accounts
*
* @Table(name="accounts")
* @Entity
*/
class Accounts
{
/**
* @var integer $id
*
* @Column(name="id", type="integer", nullable=false)
* @Id
* @GeneratedValue(strategy="IDENTITY")
* @OneToOne(targetEntity="users", mappedBy="accountid")
*/
private $id;
...
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将 User 实体上的
$accountId
属性更改为$accounts
,因为它不是整数,而是Accounts
实体:您当前的查询不不起作用,因为没有 Accounts 属性。如果您进行上述更改,则以下操作应该有效:
You should change the
$accountId
property on the User entity to$accounts
because it is not an integer it's anAccounts
entity:Your current query doesn't work because there is no Accounts property. If you make the above changes the following should work:
您不能同时使用
@Column
和@JoinColumn
。您应该删除
@Column
行,它应该可以工作!:You can't use both
@Column
and@JoinColumn
.You should remove the
@Column
Line, and it should be works!: