Doctrine 2 : What is wrong with the association between these entities?

标签 doctrine doctrine-orm

我正在尝试通过简单的示例来查看 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/

相关文章:

php - Symfony 2 EntityAudit 根本不生成历史/版本表

php - EntityManager 已关闭 : do i have to check all before?

postgresql - Symfony2 - 如何限制用户可以存储的记录数?

php - 为什么我的 doctrine2 实体中的对象类型在调用 persist() 和 flush() 时没有得到更新?

mysql - [Doctrine\DBAL\Exception\DriverException]...使用 Doctrine 生成数据库表时出现 SQLSTATE[42000] 错误

symfony - Doctrine - "Missing value for primary key"

php - Doctrine 迁移不断地生成日期时间字段

caching - 在Symfony2/Doctrine中清除查询缓存

symfony - 在Doctrine2/Symfony2中创建索引会引发语义错误

php - Doctrine SoftDelete 一对一关系