0
votes

I have this error on Symfony 3, I´m using an entity into an entity for insert a row with FK into another table, in this case, $video contain $user(user_id):

An exception occurred while executing 'INSERT INTO videos (title, description, status, image, video_path, created_at, updated_at, user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)' with params ["titulo del video1", null, null, null, null, "2017-05-06 08:10:16", "2017-05-06 08:10:16", null]:

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'user_id' cannot be null

I have a lot of null from $user data into the var $video... look the dump at the end of the post

            $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();

var_dump of $video:

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) "[email protected]"
    ["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
}

Look at $video["user"] it´s full, why i have the nulls on flush so? anyone knows why?

Videos Entity setUser method

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

    return $this;
  }

Relationship Videos to user DB

   manyToOne:
    User:
        targetEntity: User
        cascade: {  }
        fetch: LAZY
        mappedBy: null
        inversedBy: null
        joinColumns:
            user_id:
                referencedColumnName: id
        orphanRemoval: false
2
Can you show the definition of columns with relationships? From User and Video entities.miikes
updated with more code, ty!Sk8eR

2 Answers

2
votes

Change the field name User to user.

user: 
   targetEntity: User

Whole settings:

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

You should also run command for check if all relationships are set valid:

php bin/console doctrine:schema:validate

0
votes

You probably need in your User entity under addVideo(Video $video) {...} add a $video->setUser($this);.

As you see, the last line of your var_dump is null.