错误:类... \ Entity \ ..没有命名的关联

时间:2012-05-04 21:43:13

标签: symfony doctrine-orm yaml one-to-many

此问题与我的另一个问题有关:Doctrine2 gives me only first instance of related objects

我想出了双向关联以尝试解决我的旧问题,但现在我有另一个问题。

架构 [已编辑]

  XYZ\Bundle\CitiesBundle\Entity\Cities:
  type: entity
  table: cities
  fields:
    id:
      id: true
      type: integer
      unsigned: false
      nullable: false
      generator:
        strategy: IDENTITY
    name:
      type: string
      length: 50
      fixed: false
      nullable: false
    landarea:
      type: decimal
      nullable: false
    density:
      type: integer
      unsigned: false
      nullable: false
    population:
      type: integer
      unsigned: false
      nullable: false
  manyToOne:
    state:
      targetEntity: States
      cascade: {  }
      inversedBy: cities
      joinColumn:
        state_id:
          referencedColumnName: id
  lifecycleCallbacks: {  }

实体:

class Cities
{
    //other vars

    /**
     * @var XYZ\Bundle\CitiesBundle\Entity\States
     */
    private $state;

    //other get/set    

    /**
     * Set state
     *
     * @param XYZ\Bundle\CitiesBundle\Entity\States $state
     */
    public function setState(\XYZ\Bundle\CitiesBundle\Entity\States $state)
    {
        $this->state = $state;
    }

    /**
     * Get state
     *
     * @return XYZ\Bundle\CitiesBundle\Entity\States 
     */
    public function getState()
    {
        return $this->state;
    }
}

class States
{
    //other vars and get/set

    private $cities;

    public function __construct()
    {
        $this->cities = new \Doctrine\Common\Collections\ArrayCollection();
    }

    /**
     * Add cities
     *
     * @param XYZ\Bundle\CitiesBundle\Entity\Cities $cities
     */
    public function addCities(\XYZ\Bundle\CitiesBundle\Entity\Cities $cities)
    {
        $this->cities[] = $cities;
    }

    /**
     * Get cities
     *
     * @return Doctrine\Common\Collections\Collection 
     */
    public function getCities()
    {
        return $this->cities;
    }
}

QueryBuilder用法:

    $query = $em->createQueryBuilder()
                ->select('c','s')
                ->from('CitiesBundle:Cities', 'c')
                ->innerJoin('c.state', 's')
                ->orderBy('c.population', 'DESC')
                ->setMaxResults(10)
                ->getQuery();  

,错误是:

  

[语义错误]第0行,第58行靠近'ORDER BY c.population':   错误:类XYZ \ Bundle \ CitiesBundle \ Entity \ Cities没有关联   命名状态

生成DQL:

  

SELECT c,s FROM CitiesBundle:Cities c INNER JOIN c.state s ORDER BY   c.population DESC

羞耻:(

有人可以帮我解决我的问题吗?

[编辑]

我编辑了城市架构,现在错误是:

  

注意:未定义的索引:名称在   /home/marek/devel/sf2/cities/vendor/doctrine/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php   第473行

2 个答案:

答案 0 :(得分:2)

我确信joinColumns:(注意复数)仅用于Many-To-Many关系。在其他关系中,您应该使用joinColumn

在Cities XML中,尝试使用:

joinColumn:
    name: state_id
    referencedColumnName: id

除此之外,尝试在XML中将'ManyToOne'重命名为'manyToOne',但我怀疑这个问题是否存在......

希望这会有所帮助......

答案 1 :(得分:2)

请注意,在使用Symfony时,它将首先查看YAML文件的映射并忽略您的实体注释。我花了很多年时间来改变我的注释,同时它一直在加载YAML映射文件。