原则 2 未找到加入协会

发布于 2024-11-02 01:32:46 字数 1497 浏览 0 评论 0原文

我正在使用 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 技术交流群。

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

发布评论

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

评论(2

反话 2024-11-09 01:32:46

您应该将 User 实体上的 $accountId 属性更改为 $accounts,因为它不是整数,而是 Accounts 实体:

/**
  * @var Accounts $accounts
  *
  * @Column(name="accountId", type="integer", nullable=false)
  * @OneToOne(targetEntity="Accounts", inversedBy="id")
  * @JoinColumn(name="accounts_id", referencedColumnName="id")
  */
private $accounts;

您当前的查询不不起作用,因为没有 Accounts 属性。如果您进行上述更改,则以下操作应该有效:

$query = $em->createQueryBuilder()
    ->select('u')
    ->from('\Entities\Users', 'u')
    ->leftJoin('u.accounts', 'a')
    ->getQuery();
$info = $query->getResult();

You should change the $accountId property on the User entity to $accounts because it is not an integer it's an Accounts entity:

/**
  * @var Accounts $accounts
  *
  * @Column(name="accountId", type="integer", nullable=false)
  * @OneToOne(targetEntity="Accounts", inversedBy="id")
  * @JoinColumn(name="accounts_id", referencedColumnName="id")
  */
private $accounts;

Your current query doesn't work because there is no Accounts property. If you make the above changes the following should work:

$query = $em->createQueryBuilder()
    ->select('u')
    ->from('\Entities\Users', 'u')
    ->leftJoin('u.accounts', 'a')
    ->getQuery();
$info = $query->getResult();
橘味果▽酱 2024-11-09 01:32:46

您不能同时使用@Column@JoinColumn

您应该删除 @Column 行,它应该可以工作!:

/**
  * @var Accounts $accounts
  *
  * @OneToOne(targetEntity="Accounts", inversedBy="id")
  * @JoinColumn(name="accounts_id", referencedColumnName="id")
  */
private $accounts;

You can't use both @Column and @JoinColumn.

You should remove the @Column Line, and it should be works!:

/**
  * @var Accounts $accounts
  *
  * @OneToOne(targetEntity="Accounts", inversedBy="id")
  * @JoinColumn(name="accounts_id", referencedColumnName="id")
  */
private $accounts;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文