在Symfony2中创建关系实体

时间:2012-11-04 19:14:46

标签: symfony

我想在我的网站上保存用户搜索。这就是为什么我有一个类用户,我想创建搜索类。 我做到了:

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"}})

我不知道如何创建这个类Search ... 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

@ORM\Id删除$user,因为@ManyToOne映射引用不需要类型。有关详细信息,请参阅Doctrine's Annotation Reference。 Doctrine负责处理正确的列类型以保存对另一个实体的引用。

确保您的用户查询确实返回有效的$user。如果搜索可能没有$user,请使用@JoinColumn注释声明该列可以为空。请参阅另一个SO问题Doctrine 2 can't use nullable=false in manyToOne relation?