Symfony2 SQLSTATE [42S22]:找不到列:1054未知列

时间:2015-04-02 09:43:15

标签: symfony dql

我试图创建一个todo列表类型的应用程序。目前我只是尝试根据用户实例化用户ID输出所有任务。我在使用$user->getId()作为user_id的参数编写查询时遇到了一些麻烦。

以下是我所拥有的:

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,
        ));
    }

我已经尝试过QueryBuilder和DQL,但我收到同样的错误。

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'

3 个答案:

答案 0 :(得分:5)

User和Task之间的关系在我的数据库中创建了一个名为user_id的列。因此,我必须将任务实体中的列名更改为user_id user。但是我不必这样做,所以我还原了更改,查询现在正在运行。

实体/ Task.php

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

实体/ user.php的

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

查询:

$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,
            ));

答案 1 :(得分:0)

您的实体应该是:

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

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

您可以尝试:

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

答案 2 :(得分:0)

当我忘记[FunctionName("queryById")] public static async Task<IActionResult> Run( [HttpTrigger(AuthorizationLevel.Function, "get", Route = "{tableName}")] HttpRequest req, Binder binder, ILogger log, string tableName)

时,经常会遇到此错误