主義實體協會我已經配置稱爲用戶和ApiKey兩個實體。他們下面有一對一的關係映射。不工作
用戶:
/**
* @var integer
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="username", type="string", length=225, unique=true)
* @Assert\NotBlank(message="Please enter a username.")
* @Assert\Length(
* min = 2,
* max = 180,
* minMessage = "Your username must be at least {{ limit }} characters",
* maxMessage = "Your username cannot be longer than {{ limit }} characters"
*)
*/
protected $username;
/**
* @ORM\Column(name="email", type="string", length=225)
* @Assert\NotBlank(message="Please enter a email.")
* @Assert\Email(message = "The email '{{ value }}' is not a valid email.")
*/
protected $email;
/**
* @ORM\Column(name="salt", type="text")
*/
protected $salt;
/**
* @ORM\Column(name="password", type="string", length=225)
* @Assert\NotBlank(message="Please enter a password.")
* @Assert\Length(
* min = 7,
* minMessage = "Your password must be at least {{ limit }} characters long."
*)
*/
protected $password;
/**
* @Assert\NotBlank(message="Please enter a confirmation password.")
*/
protected $confirmationPassword;
/**
* @ORM\Column(name="roles", type="json_array")
*/
protected $roles;
/**
* @ORM\OneToOne(targetEntity="ApiKey", mappedBy="user")
* @ORM\JoinColumn(name="id", referencedColumnName="user_id")
*/
protected $apiKey;
ApiKey:
/**
* @var integer
* @ORM\Column(name="user_id", type="integer")
* @ORM\Id
*/
protected $user_id;
/**
* @var string
* @ORM\Column(name="api_key", unique=true, type="string", length=225)
* @Assert\NotBlank(message="Please enter a username.")
*/
protected $api_key;
/**
* @var string
* @ORM\Column(name="expires", type="datetime")
* @Assert\NotBlank(message="Please enter a username.")
*/
protected $expires;
/**
* @ORM\OneToOne(targetEntity="User", inversedBy="apiKey")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
然而,當我運行下面的代碼:
$user = new User();
$user->setUsername($request->get('username'));
$user->setSalt(uniqid(mt_rand(), true));
$user->setPassword(hash('sha256', $user->getSalt() . $request->get('password')));
$user->setRoles(['ROLE_API']);
$apiKey = new ApiKey();
$apiKey->setUser($user);
$this->getDoctrine()->getManager()->persist($user);
$this->getDoctrine()->getManager()->flush();
我得到這個錯誤,我究竟做錯了什麼?
An exception occurred while executing 'INSERT INTO users (username, email, salt, password, roles, id) VALUES (?, ?, ?, ?, ?, ?)' with params ["bobsmith", "[email protected]", "147423531058bd98d9ac9af4.48409486", "6196012972b1294e7f3fbf9c140bc26f0bf3b354ea37d00753bc1ecba0a72866", "[\"ROLE_API\"]", null]:SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
app
.users
, CONSTRAINTFK_1483A5E9BF396750
FOREIGN KEY (id
) REFERENCESapi_key
(user_id
))
變化:
$user = new User();
$user->setUsername($request->get('username'));
$user->setSalt(uniqid(mt_rand(), true));
$user->setPassword(hash('sha256', $user->getSalt() . $request->get('password')));
$user->setRoles(['ROLE_API']);
$apiKey = new ApiKey();
$this->getDoctrine()->getManager()->persist($apiKey);
$apiKey->setUser($user);
$this->getDoctrine()->getManager()->persist($user);
$this->getDoctrine()->getManager()->flush();
Entity of type AppBundle\Entity\ApiKey is missing an assigned ID for field 'user_id'. The identifier generation strategy for this entity requires the ID field to be populated before EntityManager#persist() is called. If you want automatically generated identifiers instead you need to adjust the metadata mapping accordingly.
嘗試添加 「$用戶> setApiKey($ apiKey);」 $ apiKey =新ApiKey後(); –
我添加了您的更改^。但是,現在出現此錯誤:的appbundle \實體\ ApiKey @ 000000004adc072f00000000540f2869: 一個新的實體通過中沒有配置級聯堅持操作的實體之間的關係「的appbundle \實體\用戶#apiKey」發現。爲了解決這個問題:無論是顯式調用的EntityManager#堅持()對這個未知的實體或配置級聯堅持這種關聯在例如@ManyToOne映射(..,級聯= {「堅持」})。如果您無法找到哪個實體導致問題實現'AppBundle \ Entity \ ApiKey #__ toString()'以獲取線索。 – Freid001
刷新之前,也加入這一行函數:$ this-> getDoctrine() - > getManager() - >堅持($ apiKey); –