Doctrine cannot find method in symfony
I have this problem eavesdropping on me. I'm trying to join multiple tables with DQL in symfony, but it keeps throwing an exception saying:
"The 'ranking' method for the 'AppBundle \ Entity \ Keyword' does not exist in @ App / default / index.html.twig on line 37"
This is my 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();
I think I know what the problem is, but I don't know how to solve it. My keyword object looks like this:
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: { }
Note that there is no direct relationship to the connection between the ranking items and the keyword object - this relationship is in the input object, which is the link between the keyword object and the ranking object:
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: { }
Here is the rating:
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: { }
I appreciate all kinds of help! :)
EDIT
Ok, so if I pass the DQL written above to the branch template: how to get the ranking if it's a manyToOne relation? If I like this:
{% for ranking in keyword.entry.rankings %}
<td>{{ ranking.id }}</td>
{% endfor %}
it gives me a "ranking" method for object "Doctrine \ ORM \ PersistentCollection" does not exist in @ App / default / index.html.twig on line 37
source to share
Modify your branch as I show you here
{% for entry in keyword.entry %}
{% for ranking in entry.rankings %}
<td>{{ ranking.id }}</td>
{% endfor %}
{% endfor %}
since a record is not a single entity but a collection of objects
You also wrote to the entity mapping file
AppBundle\Entity\Keyword:
[...]
oneToMany:
entry:
targetEntity: AppBundle\Entity\Entry
mappedBy: keyword
for this reason, even if your query only moistens one entry from your relationship, the result will be a compilation (in this case, PersistenCollection
)
source to share
If you want a rating, you must get it through Entry
.
You don't need to write DQL for this, just use the object that $em
comes back to you:
$ranking = $em->find('AppBundle:Keyword', $keywordId)->getEntry()->getRanking();
Have you already defined all the Entity classes?
http://symfony.com/doc/current/book/doctrine.html
EDIT
If you get an error trying to access a property of a collection, it means that you are not using a relationship OneToOne
, but OneToMany
. Before referring to Ranking
, you must iterate over this collection. Each item Entry
has Ranking
, it is not a collection that is rated.
source to share