2012-11-04 4 views
1

내 웹 사이트에서 내 사용자 검색을 저장하고 싶습니다. Thaht가 클래스 사용자가 있고 검색 클래스를 만들고 싶습니다. 그런 식으로 삽입Symfony 2에서 관련 정보 작성

class Search 
{ 

    public function __construct() 
    { 
     $this->searched_date = new \Datetime(); 
    } 

    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue 
    */ 
    private $id; 


    /** 
    * @ORM\Id 
    * @ORM\ManyToOne(targetEntity="test\UserBundle\Entity\User") 
    */ 
    private $user; 

    /** 
    * @ORM\Column(name="termsearch", type="string", length=255, nullable="true") 
    */ 
    private $termsearch; 

    /** 
    * @ORM\Column(name="goodtitle", type="string", length=255, nullable="true") 
    */ 
    private $goodtitle; 

    /** 
    * @ORM\Column(name="searched_date", type="datetime") 
    */ 
    private $searched_date; 



    /** 
    * Set termsearch 
    * 
    * @param text $termsearch 
    */ 
    public function setTermsearch($termsearch) 
    { 
     $this->termsearch = $termsearch; 
    } 

    /** 
    * Get termsearch 
    * 
    * @return text 
    */ 
    public function getTermsearch() 
    { 
     return $this->termsearch; 
    } 

    /** 
    * Set searched_date 
    * 
    * @param datetime $searchedDate 
    */ 
    public function setSearchedDate($searchedDate) 
    { 
     $this->searched_date = $searchedDate; 
    } 

    /** 
    * Get searched_date 
    * 
    * @return datetime 
    */ 
    public function getSearchedDate() 
    { 
     return $this->searched_date; 
    } 

    /** 
    * Set user 
    * 
    * @param test\UserBundle\Entity\User $user 
    */ 
    public function setUser(\test\UserBundle\Entity\User $user) 
    { 
     $this->user = $user; 
    } 

    /** 
    * Get user 
    * 
    * @return test\UserBundle\Entity\User 
    */ 
    public function getUser() 
    { 
     return $this->user; 
    } 

    /** 
    * Set goodtitle 
    * 
    * @param text $goodtitle 
    */ 
    public function setGoodtitle($goodtitle) 
    { 
     $this->goodtitle = $goodtitle; 
    } 

    /** 
    * Get goodtitle 
    * 
    * @return text 
    */ 
    public function getGoodtitle() 
    { 
     return $this->goodtitle; 
    } 

    /** 
    * Get id 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 
} 

그리고 내가 좋아하는 것 : 그 짓을

$em = $this->getDoctrine()->getEntityManager(); 
$user = $em->getRepository('TestUserBundle:User')->find($currentuser->getID());  
$search = new Search(); 
$search->setUser($user); 
$search->setTermsearch($termsearch); 
$search->setGoodtitle($goodtitle); 
$em->persist($search); 
$em->flush(); 

불행하게도 나는이 오류가 :

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens (500 Internal Server Error) 

을 그리고 스택에서 우리가 발견 할 수 :

INSERT INTO s_search (user_id, termsearch, goodtitle, searched_date) VALUES (?, ?, ?, ?) ({"1":"c2c","2":"C2C Down The Road","3":{"date":"2012-10-31 00:18:47","timezone_type":3,"timezone":"Europe\/Paris"}}) 

내가이 수업을 어떻게 만들 수 있는지 모르겠다. ... 도움을 주셔서 감사합니다!

+0

어떤 이유로 귀하의 코드 스 니펫이 올바르게 포맷되지 않았습니다. 그걸 고쳐 줄래? 코드를 찾아내어 질문에 도움을 줄 수 있습니다. –

+0

사실, 끝났습니다! – blackarcanis

답변

0

@ManyToOne 매핑 참조는 형식을 필요로하지 않으므로 $user에서 @ORM\Id을 제거하십시오. 자세한 내용은 Doctrine's Annotation Reference을 참조하십시오. Doctrine은 다른 엔티티에 대한 참조를 보유하기 위해 올바른 열 유형을 처리합니다.

사용자 쿼리가 실제로 $user을 반환하는지 확인하십시오. 검색에 $user이없는 경우 @JoinColumn 주석을 사용하여 열에 null을 지정할 수 있습니다. 다른 SO 질문보기 Doctrine 2 can't use nullable=false in manyToOne relation?

+0

완벽! 정말 고마워 ! – blackarcanis

관련 문제