错误:类... \\ \\实体..没有协会命名(Error: Class …\\Entity\\..

2019-09-17 01:02发布

这个问题关系到我的另一个问题: Doctrine2给我相关的对象只有第一个实例

我想出了双向关联,试图解决我的老问题,但现在我还有一个问题。

模式[EDITED]:

  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 \捆绑\ CitiesBundle \实体\城市没有协会命名状态

产生DQL:

选择C,S FROM CitiesBundle:城市℃的内部JOIN c.state的订单BY c.population DESC

耻辱:(

可能有人帮我解决我的问题?

[编辑]

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

注意:未定义指数:名称/home/marek/devel/sf2/cities/vendor/doctrine/lib/Doctrine/ORM/Mapping/Driver/YamlDriver.php线473

Answer 1:

我敢肯定, joinColumns:注意复数形式),仅在使用Many-To-Many关系。 在其他关系,你应该使用joinColumn

在城市XML,可以尝试在:

joinColumn:
    name: state_id
    referencedColumnName: id

除此之外,尝试重命名“多对一”在你的XML“多对一”,但我怀疑这种情况下,是这里的问题...

希望这可以帮助...



Answer 2:

请注意,使用Symfony的时候,它就会查看地图YAML文件第一,而忽略你的实体的注释。 我花了年龄改变我的注释而所有它加载YAML映射文件的时间。



文章来源: Error: Class …\\Entity\\.. has no association named