Php Symfony2插入错误参数[null,null,null]
我试图在数据库中插入值,但它得到了如下错误 任何人都可以告诉我为什么该值为null,如下所示: 执行“插入到人(姓名、年龄、, 足球队)值(?,?)'和参数[null,null,null]: SQLSTATE[23000]:完整性约束冲突:1048列“名称” 不能为空 这是实体文件Php Symfony2插入错误参数[null,null,null],php,mysql,symfony,Php,Mysql,Symfony,我试图在数据库中插入值,但它得到了如下错误 任何人都可以告诉我为什么该值为null,如下所示: 执行“插入到人(姓名、年龄、, 足球队)值(?,?)'和参数[null,null,null]: SQLSTATE[23000]:完整性约束冲突:1048列“名称” 不能为空 这是实体文件 class Person { /** * @ORM\Column(type="integer") * @ORM\Id * @ORM\GeneratedValue(strategy
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');
}
只需为name添加可为null的true即可
/**
* @ORM\Column(type="string", length=100,nullable=true)
*/
protected $name;
根据您发布的实体定义,您的属性设置不正确 这个
$this->$name=$name
应该是
$this->name=$name代码>
并在实体的所有getter和setter中更改此设置,例如
public function setAge($age){
$this->age = $age;
return $this;
}
public function getAge(){
return $this->age;
}
更改表定义并将列not null
设置为null
尚未,所有值都将为null,我的问题是,为什么即使在添加了控制器类值的情况下,实体类中的值也将为null