Php 信条可以';在symfony中找不到方法
这个问题一直困扰着我。 我试图在symfony中使用DQL连接几个表,但它不断抛出一个异常,表示: 第37行@App/default/index.html.twig中不存在对象“AppBundle\Entity\Keyword”的方法“rankings” 这是我的DQL:Php 信条可以';在symfony中找不到方法,php,entity-framework,symfony,doctrine-orm,Php,Entity Framework,Symfony,Doctrine Orm,这个问题一直困扰着我。 我试图在symfony中使用DQL连接几个表,但它不断抛出一个异常,表示: 第37行@App/default/index.html.twig中不存在对象“AppBundle\Entity\Keyword”的方法“rankings” 这是我的DQL: $query = $em->createQuery( 'SELECT e,k,r FROM AppBundle:Keyword k JOIN k.company c
$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: { }
以下是排名实体:
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模板:
如果是多人关系,如何检索排名?
如果我喜欢这个:
{% for ranking in keyword.entry.rankings %}
<td>{{ ranking.id }}</td>
{% endfor %}
{%用于在keyword.entry.rankings%中排名]
{{ranking.id}
{%endfor%}
它为我提供了第37行@App/default/index.html.twig中不存在的对象“条令\ORM\PersistentCollection”的方法“排名”如果您想要排名,您应该通过
条目检索它
您不需要为此编写DQL,只需使用$em
将返回给您的对象即可:
$ranking = $em->find('AppBundle:Keyword', $keywordId)->getEntry()->getRanking();
您是否已经定义了所有实体类
编辑
如果您尝试访问集合的属性时出错,这意味着您不使用OneToOne
,而是使用OneToMany
关系。在访问排名之前,您应该循环该集合。每个条目
元素都有一个排名
,它不是具有排名的集合。修改您的小树枝,我将在这里向您展示
{% for entry in keyword.entry %}
{% for ranking in entry.rankings %}
<td>{{ ranking.id }}</td>
{% endfor %}
{% endfor %}
因此,即使您的查询将仅从您的关系中删除一条记录,结果也将是一个集合(某种类型,在本例中为PersistenCollection
)hi-Rvanlaak,感谢您的快速回复!棒极了,这看起来很简单,但我是在控制器中还是在模板中写的?对于细枝模板,您可以将关键字
传递给模板,然后您可以在那里执行以下操作:{{{keyword.entry.ranking}
这里我假设关系是OneToOne
。