学说,关系 ManyToOne
Doctrine, 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
))
我的 class 用户:
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();
}
}
class客户端
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();
}
}
我认为您出现此错误是因为您的数据库中已有数据。当您尝试在用户 table 上添加外键时,kcient_id 为空。在您的定义中,您指定 nullable: false.
建议分两次进行。
将您的注释编辑为可为空:true,将您的数据库和link您的客户端更新为 klient
重新编辑你的注释为 nullable: false,应该没问题
尝试更新我的数据库时出现错误:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
symfony
.#sql-d8c_55
, CONSTRAINTFK_957A6479A233CB39
FOREIGN KEY (klient_id
) REFERENCESklient
(id
))
我的 class 用户:
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();
}
}
class客户端
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();
}
}
我认为您出现此错误是因为您的数据库中已有数据。当您尝试在用户 table 上添加外键时,kcient_id 为空。在您的定义中,您指定 nullable: false.
建议分两次进行。
将您的注释编辑为可为空:true,将您的数据库和link您的客户端更新为 klient
重新编辑你的注释为 nullable: false,应该没问题