Doctrine Flush错误 - >实体

时间:2017-05-06 06:30:09

标签: php entity-framework symfony doctrine-orm

我在Symfony 3上有这个错误,我使用一个实体进入一个实体,用FK将一行插入另一个表,在这种情况下,$ video包含$ user(user_id):

  

执行'INSERT INTO视频时发生异常(标题,   description,status,image,video_path,created_at,updated_at,   user_id)VALUES(?,?,?,?,?,?,?,?)'与params [“titulo del   video1“,null,null,null,null,”2017-05-06 08:10:16“,”2017-05-06   08:10:16“,null]:

     

SQLSTATE [23000]:完整性约束违规:1048列'user_id'   不能为空

我从$ user数据到var $ video有很多空...在帖子的末尾查看转储

            $user_id = ($identity->sub != null) ? $identity->sub : null;

            $title = (isset($params->title)) ? $params->title : null;
            $description = (isset($params->description)) ? $params->description : null;
            $status = (isset($params->status)) ? $params->status : null;
            if ($user_id != null && $title != null) {
                $em = $this->getDoctrine()->getManager();
                $user = $em->getRepository("BackendBundle:User")->findOneBy(
                        array(
                            "id" => $user_id
                ));

                $video = new Video();
                $video->setUser($user);
                $video->setTitle($title);
                $video->setDescription($description);
                $video->setStatus($status);
                $video->setCreatedAt($createdAt);
                $video->setUpdatedAt($updatedAt);
                var_dump($video);
                $em->persist($video);
                $em->flush();

$ video的var_dump:

object(BackendBundle\Entity\Video)#313 (10) {
  ["id":"BackendBundle\Entity\Video":private]=>
  NULL
  ["title":"BackendBundle\Entity\Video":private]=>
  string(17) "titulo del video1"
  ["description":"BackendBundle\Entity\Video":private]=>
  NULL
  ["status":"BackendBundle\Entity\Video":private]=>
  NULL
  ["image":"BackendBundle\Entity\Video":private]=>
  NULL
  ["videoPath":"BackendBundle\Entity\Video":private]=>
  NULL
  ["createdAt":"BackendBundle\Entity\Video":private]=>
  object(DateTime)#281 (3) {
    ["date"]=>
    string(26) "2017-05-06 08:10:16.255330"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(12) "Europe/Paris"
  }
  ["updatedAt":"BackendBundle\Entity\Video":private]=>
  object(DateTime)#282 (3) {
    ["date"]=>
    string(26) "2017-05-06 08:10:16.255350"
    ["timezone_type"]=>
    int(3)
    ["timezone"]=>
    string(12) "Europe/Paris"
  }
  ["user":"BackendBundle\Entity\Video":private]=>
  object(BackendBundle\Entity\User)#317 (8) {
    ["id":"BackendBundle\Entity\User":private]=>
    int(8)
    ["role":"BackendBundle\Entity\User":private]=>
    string(4) "user"
    ["name":"BackendBundle\Entity\User":private]=>
    string(7) "Pruebas"
    ["surname":"BackendBundle\Entity\User":private]=>
    string(7) "Pruebas"
    ["email":"BackendBundle\Entity\User":private]=>
    string(19) "pruebas@pruebas.com"
    ["password":"BackendBundle\Entity\User":private]=>
    string(64) "718e3978516d387924d91980a7e21af2f434de445731951a6585bda2eacef046"
    ["image":"BackendBundle\Entity\User":private]=>
    string(14) "1494043934.png"
    ["createdAt":"BackendBundle\Entity\User":private]=>
    object(DateTime)#314 (3) {
      ["date"]=>
      string(26) "2017-05-05 10:05:36.000000"
      ["timezone_type"]=>
      int(3)
      ["timezone"]=>
      string(12) "Europe/Paris"
    }
  }
  ["User":"BackendBundle\Entity\Video":private]=>
  NULL
}

看看$ video [“user”]已满,为什么我在flush上有空值? 有谁知道为什么?

视频实体setUser方法

  public function setUser(\BackendBundle\Entity\User $user = null)
  {
    $this->user = $user;

    return $this;
  }

与用户数据库的关系视频

   manyToOne:
    User:
        targetEntity: User
        cascade: {  }
        fetch: LAZY
        mappedBy: null
        inversedBy: null
        joinColumns:
            user_id:
                referencedColumnName: id
        orphanRemoval: false

2 个答案:

答案 0 :(得分:2)

将字段名称User更改为user

user: 
   targetEntity: User

整个设置:

manyToOne:
    user:
        targetEntity: User
        cascade: {  }
        fetch: LAZY
        mappedBy: null
        inversedBy: null
        joinColumns:
            user_id:
                referencedColumnName: id
        orphanRemoval: false

您还应该运行命令以检查所有关系是否设置为有效:

php bin/console doctrine:schema:validate

答案 1 :(得分:0)

您可能需要在您的用户实体下添加视频(视频$视频){...}添加$ video-> setUser($ this);.

如您所见,var_dump的最后一行为空。