Wenn ein versuchen, meine Datenbank aktualisieren ich eine Fehlermeldung anzeigt:Lehre, ralation ManyToOne
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
))
Meine Klasse Benutzer:
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();
}
}
Klasse 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();
}
}
Bitte ok Zeigen Sie den Rest Ihres Codes an, um diese Entitäten persistent zu machen. Doctrine behält die Beziehungen nicht automatisch bei (sofern sie nicht dafür konfiguriert wurden). Dies ist wahrscheinlich die Ursache, aber wir werden mehr wissen, wenn Sie diesen Code hinzufügen. –
ok, ich bearbeite meinen Beitrag – pierzcha
Welchen Code haben Sie, der tatsächlich die Entitäten hält? Ich sehe die Entity-Klassen, aber nichts, was etwas mit ihnen macht. –