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="accou开发者_如何学CntId", 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;
...
}
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();
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;
精彩评论