2015-07-01 47 views
3

我有這個問題讓我煩惱。 我想加入一對夫婦在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

回答

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

0

如果你想排名,你應該通過Entry檢索它。

你不需要寫DQL的是,僅僅使用對象的$em將返回到你:

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

你已經定義的所有實體類?

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

編輯

如果收到您嘗試訪問集合的性質,這意味着你不使用OneToOneOneToMany關係的錯誤。在訪問Ranking之前,您應該循環訪問該集合。每個Entry元素都有一個Ranking,它不是具有排名的集合。

+0

嗨Rvanlaak,感謝您的快速回復!太棒了,這看起來很簡單,但是我可以在控制器中還是在模板中寫入? –

+0

對於枝條模板,你可以在'keyword'傳遞給模板,然後你可以在那裏做以下幾點: '{{keyword.entry.ranking}}' 在這裏,我假設關係'OneToOne'。 – Rvanlaak

+0

嗨,看編輯:) –