Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doctrine 2 join associations not found

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;
...
}
like image 256
timt Avatar asked Dec 08 '25 23:12

timt


2 Answers

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();
like image 191
rojoca Avatar answered Dec 11 '25 12:12

rojoca


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;
like image 30
Almog Baku Avatar answered Dec 11 '25 11:12

Almog Baku



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!