gpt4 book ai didi

php - Symfony2 插入错误参数[null,null,null]

转载 作者:行者123 更新时间:2023-11-29 07:39:08 27 4
gpt4 key购买 nike

我正在尝试将值插入数据库,但出现这样的错误任何人都可以告诉我为什么该值为空,如下所示:

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

这是实体文件

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


}

这是 Controller 文件

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

最佳答案

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

这个

$this->$name = $name

应该是

$this->name = $name;

并在实体的所有 getter 和 setter 中更改此设置,例如

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

关于php - Symfony2 插入错误参数[null,null,null],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29592751/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com