1

I would like to save my users search on my website. Thaht's why i have a Class User and i would like to create Search Class. I have done that :

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;
    }
}

And i would like to insert like that :

$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();

Unfortunately i have this error :

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

And in the stack we can found that :

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

I don't know how i can create this class Search... Thank for for you help !

blackarcanis
  • 542
  • 1
  • 4
  • 23

1 Answers1

0

Remove @ORM\Id from the $user as @ManyToOne mapping reference does not require a type. See Doctrine's Annotation Reference for details. Doctrine takes care of the correct column type to hold the reference to another entity.

Make also sure that your User query really returns a valid $user. If it's possible that Search does not have $user, use @JoinColumn annotation to claim the column nullable. See another SO question Doctrine 2 can't use nullable=false in manyToOne relation?

Community
  • 1
  • 1
Ville Mattila
  • 1,343
  • 3
  • 15
  • 28