3
votes

I'm attempting to return data from a simple query but I get the following error:

Uncaught PHP Exception Doctrine\ORM\Query\QueryException: "[Semantical Error] line 0, col 63 near 'user_id = :u': Error: Class \Bundle\MyBundle\Entity\User\Users has no field or association named user_id" at …../doctrine/orm/lib/Doctrine/ORM/Query/QueryException.php line 49

The Entity:

    /**
* Users
*
* @ORM\Table(name="company.users")
* @ORM\Entity
*/
class Users
{
    /**
     * @var integer
     *
     * @ORM\Column(name="user_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\SequenceGenerator(sequenceName="company.user_id_seq")
     */
    private $userId;

The Call in the Controller:

$query = $em->createQuery(
    'SELECT u
    FROM MyBundle:User\Users u
    WHERE u.user_id = :user_id'
)->setParameter('user_id', 48201);

$users = $query->getResult();
1

1 Answers

2
votes

While using DQL you need to forget about SQL. You should use column names defined in Entity not in database - use userId instead of user_id:

$query = $em->createQuery(
    'SELECT u
    FROM MyBundle:Users u
    WHERE u.userId = :user_id'
)->setParameter('user_id', 48201);

I'm also not sure about MyBundle:User\Users syntax. Why don't you use MyBundle:Users ?