1

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

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

我不知道如何创建此类搜索...感谢您的帮助!

4

1 回答 1

0

@ORM\Id从@ManyToOne 映射引用中删除$user不需要类型。有关详细信息,请参阅Doctrine 的注释参考。Doctrine 负责正确的列类型以保存对另一个实体的引用。

还要确保您的 User 查询确实返回了有效的$user. 如果 Search 可能没有$user,请使用@JoinColumn注释来声明该列可为空。查看另一个 SO 问题Doctrine 2 can't use nullable=false in manyToOne 关系?

于 2012-11-04T19:42:47.847 回答