6

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'
Tom
  • 258
  • 1
  • 3
  • 12
  • 2
    Do you have column user_id_id in table task? I guess not. You have to change mapping in entity TaskBundle:Task. Probably you have bad column name in @ORM\Column annotation. – kba Apr 02 '15 at 09:55
  • 1
    You are correct, thanks. Basically I thought I had to change the column name from user to user_id like in the DB but apparently you don't – Tom Apr 02 '15 at 10:01
  • If problem is solved you should close your question as it is based on your typo. – Michael Sivolobov Apr 02 '15 at 10:48
  • 1
    It wasn't a typo, rather a misunderstanding of Doctrine. – Tom Apr 02 '15 at 10:51

3 Answers3

7

I often get this error when I forgot about doctrine:schema:update --force

Aerogirl
  • 369
  • 4
  • 17
6

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,
            ));
Tom
  • 258
  • 1
  • 3
  • 12
0

Your entity should be something like:

Task{
 private $user;
 .....
}

User{
 ....`enter code here`
}

You can try:

$query = $em->createQuery(
            'SELECT p
            FROM TaskBundle:Task p
            WHERE p.user = :user'
        )->setParameter('user', $user);
imran
  • 155
  • 1
  • 9