2017-05-06 30 views
0

我在Symfony 3上有這個錯誤,我正在使用一個實體插入一個FK行到另一個表中,在這種情況下,$ video包含$ user USER_ID):在執行「INSERT INTO視頻(標題, 描述,狀態,圖像,video_path,created_at,的updated_at發生學說沖洗錯誤 - > null var實體

例外, 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]:

SQ LSTATE [23000]:完整性約束違規:1048列「user_ID的」 不能爲空

我有很多從$用戶數據空到了var $視頻......看看轉儲在的結束髮布

  $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 $視頻:

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 
} 

看$視頻[ 「用戶」] it's滿,爲什麼我有這麼齊平的空? 任何人都知道爲什麼?

影片實體SETUSER方法

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

    return $this; 
    } 

相關視頻用戶DB

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

你能告訴列有關係的定義是什麼?來自用戶和視頻實體。 – miikes

+0

用更多代碼更新,ty! – Sk8eR

回答

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

+0

用戶: targetEntity:用戶>>>我更改爲該工作而無需運行cmd ty! N1 – Sk8eR

0

你可能需要在您的用戶實體addVideo(視頻$視頻)下{...}添加$視頻 - > SETUSER($這個);.

如您所見,var_dump的最後一行爲空。

+0

但是那是「用戶」,它是實體,它填充的變量「用戶」... – Sk8eR