2010-08-17 100 views
11

我在執行Doctrine DQL查詢時遇到問題。這是它給我的錯誤。Doctrine 2 PlainValue expected

Doctrine\Common\Annotations\AnnotationException: [Syntax Error] Expected PlainValue, 
got 'integer' at position 13 in property Base\Session::$lifetime. 

我的代碼如下所示:

$query = $em->createQuery("SELECT s FROM Base\Session s WHERE s.session = \"$id\""); 

其中$ id是當前的session_id。我的模型看起來像:

namespace Base; 

/** @Entity @Table(name="session") */ 
class Session extends Skeleton { 
/** 
* @Id @Column(type="integer") 
* @GeneratedValue(strategy="AUTO") 
*/ 
protected $id; 

/** @Column(length=32) */ 
protected $session; 

/** @Column(type=integer) */ 
protected $lifetime; 

/** @Column(type=integer) */ 
protected $modified; 

/** @Column(type="text") */ 
protected $data; 
} 

回答

21

您在這兩個錯誤:

  1. 您必須註明您的註解,即@Column(類型= 「整數」)不@Colunn(類型=整數) 。如果您的映射錯誤,則會引發Doctrine \ Common \ Annotations \ AnnotationException。這與查詢無關。
  2. 您的查詢應該使用準備的語句,即

    $查詢= $ EM->的createQuery( 「選擇期從基地\會話S其中s.session = 1?」); $ query-> setParameter(1,$ id);

+0

謝謝。第一個錯誤是解決方案。我已經知道你的第二點。剛剛嘗試了很多東西,結果我忘了使用準備好的語句。非常感謝! – 2010-08-18 09:22:22

+5

作爲對#1的補充評論,單引號也不起作用,必須使用雙引號 – Shawn 2013-09-05 15:24:32