我试图将值插入数据库,但是它得到了这样的错误 任何人都可以这样告诉我为什么值为null:
执行' INSERT INTO person(姓名,年龄, footballteam)VALUES(?,?,?)'使用params [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 :(得分:2)
根据您发布的实体定义,您的属性设置不正确
这个
$this->$name = $name
应该是
$this->name = $name;
并在您实体的所有getter和setter中更改此内容,例如
public function setAge($age){
$this->age = $age;
return $this;
}
public function getAge(){
return $this->age;
}
答案 1 :(得分:0)
只需为名称
添加nullable true即可/**
* @ORM\Column(type="string", length=100,nullable=true)
*/
protected $name;