symfonydql

Symfony2 SQLSTATE[42S22]: Column not found: 1054 Unknown column


I'm trying to create a todo list kind of app. At the moment I am just trying to output all the tasks based on the user instanced user id. I'm having some trouble writing the query using $user->getId() as the parameter for user_id.

Here's what I have:

public function indexAction()
    {
        $user = $this->getUser();
        //var_dump($user);

        $em = $this->getDoctrine()->getManager();
        $query = $em->createQuery(
            'SELECT p
            FROM TaskBundle:Task p
            WHERE p.user_id = :user_id'
        )->setParameter('user_id', $user->getId());

        $tasks = $query->getResult();

        return $this->render('Default/index.html.twig', array(
                'tasks' => $tasks,
        ));
    }

I've tried QueryBuilder and DQL and I am getting the same error.

An exception occurred while executing 'SELECT t0_.id AS id0, t0_.name AS name1, t0_.done AS done2, t0_.created AS created3, t0_.updated AS updated4, t0_.user_id_id AS user_id_id5 FROM tasks t0_ WHERE t0_.user_id_id = ?' with params [1]:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0_.user_id_id' in 'field list'

Solution

  • The relationship between User and Task created a column in my database called user_id. Therefore I had to change the column name in the Task entity to user_id from user. However I didn't have to do this so I reverted the change and the query is now working.

    Entity/Task.php

    /**
         * @ORM\ManyToOne(targetEntity="Todo\UserBundle\Entity\User", inversedBy="")
         * @ORM\JoinColumn()
         */
        private $user;
    

    Entity/User.php

    /**
         * @ORM\Id
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue(strategy="AUTO")
         * @ORM\OneToMany(targetEntity="Todo\UserBundle\Entity\Task", mappedBy="user")
         */
        protected $id;
    

    Query:

    $em = $this->getDoctrine()->getManager();
                $query = $em->createQuery(
                    'SELECT p
                FROM TaskBundle:Task p
                WHERE p.user = :user'
                )->setParameter('user', $user->getId());
    
                $tasks = $query->getResult();
    
                return $this->render('Default/index.html.twig', array(
                    'tasks' => $tasks,
                ));