我正在尝试通过简单的示例来查看 Doctrine2 中的所有更改。
请查看以下实体片段:
VCat.php
namespace Application\Models;
/**
* @Entity
* @Table(name="v_cat")
*/
class VCat
{
/**
* @Id @Column(type="bigint")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @OneToMany(targetEntity="VScat", mappedBy="vCat") )
*/
private $vScats;
namespace Application\Models;
VScat.php
namespace Application\Models;
/**
* @Entity
* @Table(name="v_scat",indexes={@index(name="FK_v_scat",columns={"vcatid"})})
*/
class VScat
{
/**
* @Id @Column(type="bigint")
* @GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ManyToOne(targetEntity="VCat", inversedBy="vScats")
* @JoinColumn(name="vcatid", referencedColumnName="id")
*/
private $vCat;
vcatid
是表 v_scat 中的外键列
这是我尝试运行的查询:
$categories = Zend_Registry::get('em')
-> createQuery('select c.name, sub.name as sub_name from \Application\Models\VCat c JOIN c.VScat sub WHERE sub.active = 1 and c.active = 1 and c.id = sub.vcatid')
-> getResult();
这是错误:
(string:130) [Semantical Error] line 0, col 69 near 'sub WHERE sub.active': Error: Class Application\Models\VCat has no association named VScat
看起来是对的,但我显然错过了一些东西。
UPDATE 现在我得到这个错误,它引用了mysql中的表列名。这是不正确的吗?我想我需要以某种方式告诉 Doctrine 这个属性属于数据库中的这个字段。
Error: Class Application\Models\VScat has no field or association named vcatid
最佳答案
您需要引用的是 VCat 上的属性,而不是实体类型。
JOIN c.VScat sub
应该是:
JOIN c.vScats sub
关于 Doctrine 2 : What is wrong with the association between these entities?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5568394/