Symfony2中插入错误PARAMS [NULL,NULL,NULL]

问题描述:

我试图在数据库中插入值,但它得到了这样 任何错误能告诉我为什么值是null这样的:Symfony2中插入错误PARAMS [NULL,NULL,NULL]

执行'INSERT INTO人名(姓名,年龄, footballteam)VALUES(?,?,?)'时发生异常并带参数[null,null,null]: SQLSTATE [23000]:完整性约束违规:1048列名' 不能为空

这里是实体文件

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


} 

,这里是控制文件

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'); 
    } 
+0

变化表定义的制定者和设置该栏不是女士''' null' –

+0

尚未,所有的值将为空,我的问题是为什么实体类中的值为NULL,即使在控制器类的值被添加 – Xuandao

只是你可以添加可空的真正的名字

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

根据您发布实体定义你的属性设置不正确

This

$this->$name = $name

应该

$this->name = $name;

并改变这所有的getter和你的实体如

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