2017-12-14 27 views
0

私のデータベースを更新しようと、私はエラーを得た:教義、ralationのManyToOne

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (symfony . #sql-d8c_55 , CONSTRAINT FK_957A6479A233CB39 FOREIGN KEY (klient_id) REFERENCES klient (id))

は私のクラスのユーザー:私の意見では

namespace AppBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use FOS\UserBundle\Model\User as BaseUser; 
use Doctrine\ORM\Mapping as ORM; 


/** 
* Class User 
* @package AppBundle\Entity 
* 
* @ORM\Table("fos_user") 
* @ORM\Entity() 
*/ 
class User extends BaseUser 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 


    /** 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Klient", inversedBy="users") 
    * @ORM\JoinColumn(nullable=false) 
    */ 
    private $klient; 

    /** 
    * @return mixed 
    */ 
    public function getKlient() 
    { 
     return $this->klient; 
    } 

    /** 
    * @param mixed $klient 
    */ 
    public function setKlient($klient) 
    { 
     $this->klient = $klient; 
    } 

    public function __construct() 
    { 
     parent::__construct(); 

    } 
} 

クラスKlient

namespace AppBundle\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* Klient 
* 
* @ORM\Table(name="klient") 
* @ORM\Entity(repositoryClass="AppBundle\Repository\KlientRepository") 
*/ 
class Klient 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @var string 
    * 
    * @ORM\Column(name="nazwa", type="string", length=255, unique=true) 
    */ 
    private $nazwa; 


    /** 
    * @ORM\OneToMany(targetEntity="AppBundle\Entity\User", mappedBy="klient") 
    */ 
    private $users; 

    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set nazwa 
    * 
    * @param string $nazwa 
    * 
    * @return Klient 
    */ 
    public function setNazwa($nazwa) 
    { 
     $this->nazwa = $nazwa; 

     return $this; 
    } 

    /** 
    * Get nazwa 
    * 
    * @return string 
    */ 
    public function getNazwa() 
    { 
     return $this->nazwa; 
    } 

    public function __construct() 
    { 
     $this->users = new ArrayCollection(); 
    } 
} 
+1

[OK]をする必要がありますこれらのエンティティを永続化することに関連する残りのコードを表示します。 Doctrineは自動的にリレーションシップを維持しません(そのように構成されていない限り)ので、それが原因である可能性がありますが、そのコードを追加すると詳細がわかります。 –

+0

ok、投稿を編集します – pierzcha

+0

エンティティを実際に保持しているコードはありますか?エンティティクラスは見えますが、何もしません。 –

答えて

0

あなたがこれを持っているがエラーはデータベースに既にデータがあるためです。あなたのユーザテーブルに外部キーを追加しようとすると、kcient_idはnullになります。そしてあなたの定義では、nullable:falseを指定します。

あなたは2回進むことをお勧めします。

  1. 編集NULL可能にあなたの注釈:trueの場合、データベースを更新し、NULL可能に

  2. 再編集し、あなたの注釈klientするようにクライアントをリンク:偽を、それが