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

3

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'
Disjunct answered 2/4, 2015 at 9:43 Comment(4)
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.Seigneur
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'tDisjunct
If problem is solved you should close your question as it is based on your typo.Sound
It wasn't a typo, rather a misunderstanding of Doctrine.Disjunct
D
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,
            ));
Disjunct answered 2/4, 2015 at 10:57 Comment(0)
T
8

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

Taylor answered 14/2, 2019 at 10:23 Comment(0)
D
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,
            ));
Disjunct answered 2/4, 2015 at 10:57 Comment(0)
S
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);
Starflower answered 2/4, 2015 at 10:37 Comment(4)
check my answer for the fixDisjunct
Dear Tom, what is the problem of my answer?Starflower
$user and $user->getId() works either way, this was a problem within the entityDisjunct
I have just given example for entity and I assume everybody knows it. You have written mapping as annotation. It also will be yml format etc.Starflower

© 2022 - 2024 — McMap. All rights reserved.