Doctrine2: What is wrong with the association betw

2019-02-27 09:29发布

问题:

I am trying to work through simple examples to see all the changes in Doctrine2.

Please take a look at the following entity snippets:

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 is the Foreign Key column in table v_scat

This is the query I am trying to run:

$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();

And this is the error:

(string:130) [Semantical Error] line 0, col 69 near 'sub WHERE sub.active': Error: Class Application\Models\VCat has no association named VScat

It seems right, but I am obviously missing something.

UPDATE Now I get this error, which refers to the table column name in mysql. Is that incorrect? I thought I needed to tell Doctrine somehow that this property belongs to this field in the DB.

 Error: Class Application\Models\VScat has no field or association named vcatid

回答1:

It's the property on VCat you need to reference not the entity type.

JOIN c.VScat sub 

should be:

JOIN c.vScats sub