我正嘗試創建待辦事項列表類應用程序。目前,我只是試圖根據用戶實例化用戶標識輸出所有任務。我在使用$user->getId()
作爲user_id的參數編寫查詢時遇到了一些麻煩。Symfony2 SQLSTATE [42S22]:未找到列:1054未知列
這是我有:
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'
你有表任務欄user_id_id?我猜不會。您必須更改實體TaskBundle中的映射:任務。可能在@ORM \ Column註釋中列名錯誤。 – kba 2015-04-02 09:55:42
你是對的,謝謝。基本上我以爲我不得不像用數據庫一樣將用戶的列名改爲user_id,但顯然你不需要 – Tom 2015-04-02 10:01:27
如果問題解決了,你應該關閉你的問題,因爲它是基於你的錯字。 – 2015-04-02 10:48:25