Frage

I have an entity:

...

class UserRole extends Role
{
    /**
     * @ORM\Column(name="user_role_id", type="integer")
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    ...

But when I try to use user_role_id in a doctrine query I get this error:

[Semantical Error] line 0, col 42 near 'user_role_id': Error: Class MyApp\Model\UserRole has no field or association named user_role_id

The query I am using is this:

$query = $this->getEntityManager()
    ->createQuery('SELECT r FROM Model:UserRole AS r WHERE r.user_role_id IN (:roles)')
    ->setParameter('roles', array_values($roles));

I've definitely got a user_role_id field as I can see it in phpMyAdmin.

Does anyone have any idea why doctrine is not recognising it?

War es hilfreich?

Lösung

This is doctrine and you want to write a DQL not a SQL, and you need to use the field names as defined in entities not the column names.
So in your query just use r.id instead of r.user_role_id

Take a look at DQL in Doctrine

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top