我有一个问题一直困扰着我。 我试图使用Symfony中的DQL连接几个表,但它一直抛出一个异常,显示:
"在@App/default/index.html.twig的第37行,对象“AppBundle\Entity\Keyword”的方法“rankings”不存在"
这是我的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"对象的方法"rankings"在@App/default/index.html.twig的第37行不存在。
keyword
传递给模板,然后在那里执行以下操作:`{{ keyword.entry.ranking }}`
这里我假设关系是OneToOne
。 - Rvanlaak