我有這個問題讓我煩惱。 我想加入一對夫婦在symfony中有DQL表,但它不斷拋出一個異常說:主義在symfony中找不到方法
「方法‘排名’爲對象‘的appbundle \實體\關鍵字’不中不存在@應用程序/默認/index.html.twig在第37" 行
這是我的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: 我怎麼檢索排名,如果它是一個多對一的關係? 如果我這樣做:
{% for ranking in keyword.entry.rankings %}
<td>{{ ranking.id }}</td>
{% endfor %}
它給了我方法「排名」爲對象的「學說\ ORM \ PersistentCollection」在@應用程序/默認/ index.html.twig在行不存在37
嗨Rvanlaak,感謝您的快速回復!太棒了,這看起來很簡單,但是我可以在控制器中還是在模板中寫入? –
對於枝條模板,你可以在'keyword'傳遞給模板,然後你可以在那裏做以下幾點: '{{keyword.entry.ranking}}' 在這裏,我假設關係'OneToOne'。 – Rvanlaak
嗨,看編輯:) –