【问题标题】:Doctrine can't find method in symfony教义在 symfony 中找不到方法
【发布时间】:2015-07-01 15:39:18
【问题描述】:

这个问题一直困扰着我。 我正在尝试在 symfony 中使用 DQL 加入几个表,但它不断抛出异常:

“对象“AppBundle\Entity\Keyword”的方法“排名”在第 37 行的@App/default/index.html.twig 中不存在”

这是我的 DQL:

$query = $em->createQuery(
        'SELECT e,k,r
         FROM AppBundle:Keyword k
         JOIN k.company c
         LEFT JOIN k.entry e
         LEFT JOIN e.rankings r
         WHERE c.user = :id
         ORDER BY k.name ASC'
    )->setParameter('id',$user_id);


$keywords = $query->getResult();

我想我知道问题所在 - 但我不知道如何解决它。 我的关键字实体如下所示:

AppBundle\Entity\Keyword:
type: entity
table: null
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    name:
        type: string
        length: 255

manyToOne:
    company:
        targetEntity: AppBundle\Entity\Company

oneToMany:
     entry:
        targetEntity: AppBundle\Entity\Entry
        mappedBy: keyword


lifecycleCallbacks: {  }

请注意,排名实体和关键字实体之间没有直接连接关系 - 该关系在条目实体中,即关键字实体和排名实体之间的链接:

AppBundle\Entity\Entry:
type: entity
table: null
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    path:
        type: string
        length: 255

manyToOne:
     keyword:
        targetEntity: AppBundle\Entity\Keyword

oneToMany:
     rankings:
        targetEntity: AppBundle\Entity\Ranking
        mappedBy: entry

lifecycleCallbacks: {  }

这是排名实体:

AppBundle\Entity\Ranking:
type: entity
table: null
id:
    id:
        type: integer
        id: true
        generator:
            strategy: AUTO
fields:
    position:
        type: integer
    visits:
        type: integer
        nullable: TRUE
    bounces:
        type: integer
        nullable: TRUE
    date:
        type: datetime


manyToOne:
    entry:
        targetEntity: AppBundle\Entity\Entry

lifecycleCallbacks: {  }

感谢各种帮助! :)

编辑

好的,如果我将上面编写的 DQL 传递给 twig 模板: 如果它是 manyToOne 关系,我如何检索排名? 如果我这样做:

   {% for ranking in keyword.entry.rankings %}
      <td>{{ ranking.id }}</td>
   {% endfor %}

它给了我对象“Doctrine\ORM\PersistentCollection”的方法“排名”在第 37 行的 @App/default/index.html.twig 中不存在

【问题讨论】:

    标签: php entity-framework symfony doctrine-orm


    【解决方案1】:

    修改你的树枝,我会在这里给你看

    {% for entry in keyword.entry %}
      {% for ranking in entry.rankings %}
        <td>{{ ranking.id }}</td>
      {% endfor %}
    {% endfor %}
    

    因为条目不是单个实体,而是实体的集合

    您还写入了实体映射文件

    AppBundle\Entity\Keyword:
    [...]
    oneToMany:
         entry:
            targetEntity: AppBundle\Entity\Entry
            mappedBy: keyword
    

    出于这个原因,即使您的查询仅包含您的关系中的一条记录,结果也将是一个集合(某种类型,在本例中为 PersistenCollection

    【讨论】:

      【解决方案2】:

      如果你想要排名,你应该通过Entry检索它。

      您不需要为此编写 DQL,只需使用 $em 将返回给您的对象即可:

      $ranking = $em->find('AppBundle:Keyword', $keywordId)->getEntry()->getRanking();
      

      您是否已经定义了所有实体类?

      http://symfony.com/doc/current/book/doctrine.html

      编辑

      如果您尝试访问集合的属性时遇到错误,这意味着您没有使用OneToOne,而是使用OneToMany 关系。在访问Ranking 之前,您应该遍历该集合。每个Entry 元素都有一个Ranking,它不是具有排名的集合。

      【讨论】:

      • 嗨 Rvanlaak,感谢您的快速回复!太棒了,这看起来很简单,但是我是在控制器中还是在模板中编写呢?
      • 对于 Twig 模板,您可以将 keyword 传递给模板,然后您可以在那里执行以下操作:{{ keyword.entry.ranking }} 这里我假设关系是 OneToOne
      猜你喜欢
      • 2020-07-10
      • 2020-01-18
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-17
      • 2017-04-24
      相关资源
      最近更新 更多