【问题标题】:Symfony2 Insert error params[null,null,null]Symfony2 插入错误参数 [null,null,null]
【发布时间】:2015-04-12 18:07:53
【问题描述】:

我正在尝试向数据库中插入值,但出现了这样的错误 任何人都可以告诉我为什么这个值为 null :

执行'INSERT INTO person (name, age, Footballteam) VALUES (?, ?, ?)' with 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');
    }

【问题讨论】:

  • 更改表定义并将列not null设置为null
  • 还没有,所有的值都将是 null ,我的问题是为什么实体类中的值是 NULL 即使在控制器类中添加了值

标签: php mysql symfony


【解决方案1】:

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

这个

$this->$name = $name

应该是

$this->name = $name;

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

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

【讨论】:

    【解决方案2】:

    只是你可以为 name 添加 nullable true

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

    【讨论】:

      猜你喜欢
      • 2012-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-14
      • 2013-07-02
      相关资源
      最近更新 更多