2017-05-06 11 views
0

このエラーはSymfony 3で発生しました。エンティティにエンティティを使用して別のテーブルにFKの行を挿入します。この場合、$ videoには$ user ?????user_idの):、(、、、、動画(タイトル、 説明、ステータス、画像、video_path、のcreated_at、updated_atの、 のuser_id)VALUES。INSERT INTO」を実行しながら、Doctrine Flushでエラーが発生しました - > null var on Entity

例外が発生しましたか?ヌル、ヌル、ヌル、 "2017-05-06 08:10:16"、 "2017-05-06" video1 "、" 2017-05-08:10:16 "のように、 、null]:

SQ LSTATE [23000]:整合性制約違反:1048列「user_idの」 私は変数$ビデオに$のユーザデータからヌルがたくさんある

nullにすることはできませんが...の最後にダンプを見て私はフラッシュにヌルを持っている理由$ビデオ[ "ユーザー"]を見には、フルit's

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 
} 

:$ビデオの

  $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を投稿? 誰もがなぜ知っていますか?

ビデオエンティティSETUSER方法

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

    return $this; 
    } 

関係動画ユーザーにあなたはおそらくaddVideo(ビデオ$ビデオ)の下であなたのUserエンティティに必要なDB

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

あなたが関係を持つ列の定義を表示することができますか?ユーザとビデオエンティティから。 – miikes

+0

はより多くのコードで更新されました。 – Sk8eR

答えて

2

Useruserに変更してください。

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:User >>>私はそれに変更し、cmdのタイを実行せずに動作します! n1 – Sk8eR

0

{...} $の動画を追加 - > setUser($ this);.

ご覧のとおり、var_dumpの最後の行はnullです。

+0

しかし、それは "ユーザー"、それは実体、var "ユーザー"はそれを埋める... – Sk8eR

関連する問題