Xuandao
Xuandao

Reputation: 33

Symfony2 Insert error params[null,null,null]

I'm trying to insert value in to database but it got the error like this Anyone can tell me why the value be null like this :

An exception occurred while executing 'INSERT INTO person (name, age, footballteam) VALUES (?, ?, ?)' with params [null, null, null]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null

here is the entity file

class Person
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $name;
        /**
     * @ORM\Column(type="integer")
     */
    protected $age;
        /**
     * @ORM\Column(type="string", length=100)
     */
    protected $footballteam;



    /**
     * @return mixed
     * */
    public function getID(){
        return $this->$id;
    }
    /**
     * @param mixed $name
     * */
    public function setName($name){
        $this->$name = $name;
        return $this;
    }
    /**
     * @return mixed
     * */
    public function getName(){
        return $this->$name;
    }
    /**
     * @param mixed $age
     * */
    public function setAge($age){
        $this->$age = $age;
        return $this;
    }
    /**
     * @return mixed
     * */
    public function getAge(){
        return $this->$age;
    }
    /**
     * @param mixed $setFootballteam
     * */
    public function setFootballteam($footballteam){
        $this->$footballteam = $footballteam;
        return $this;
    }
    /**
     * @return mixed
     * */
    public function getFootballteam(){
        return $this->$footballteam;
    }


}

and here is the controller file

public function contactAction()
    {

        $person = new Person();
        $person->setName('xuandao');
        $person->setAge(20);
        $person->setFootballteam("Manchester");

        $em = $this->getDoctrine()->getManager();

        $em->persist($person);
        $em->flush();
        return $this->render('rikidaolxBundle:Page:contact.html.twig');
    }

Upvotes: 0

Views: 1102

Answers (2)

M Khalid Junaid
M Khalid Junaid

Reputation: 64486

According to your posted entity definition your properties are not set properly

This

$this->$name = $name

should be

$this->name = $name;

and change this in all your getters and setters of your entity e.g

public function setAge($age){
    $this->age = $age;
    return $this;
}
public function getAge(){
    return $this->age;
} 

Upvotes: 2

imran
imran

Reputation: 157

just you can add nullable true for name

/**
 * @ORM\Column(type="string", length=100,nullable=true)
 */
 protected $name;

Upvotes: 0

Related Questions