Php 条令实体关联不起作用
我配置了两个名为User和ApiKey的实体。他们在下面有一对一的关系映射 用户: ApiKey:Php 条令实体关联不起作用,php,doctrine-orm,symfony,Php,Doctrine Orm,Symfony,我配置了两个名为User和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) * @Ass
/**
* @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();
我犯了这个错误,我做错了什么
使用参数[“bobsmith”,“执行”INSERT INTO users(用户名、电子邮件、salt、密码、角色、id)值(?,,,,,,,,,?)时发生异常bob@live.com“,”147423531058bd98d9ac9af4.48409486”,“6196012972B1294E7F3FBF9C140BC26F03B3B35EA37D00753BC1ECBA0A722866”、“[\”角色\u API\“]”,null]:SQLSTATE[23000]:完整性约束冲突:1452无法添加或更新子行:外键约束失败(app
users
,constraintFK_1483A5E9BF396750
外键(id
)引用api_-key
(用户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();
AppBundle\Entity\ApiKey类型的实体缺少为字段“user\u ID”分配的ID。此实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果希望自动生成标识符,则需要相应地调整元数据映射
你似乎有一个级联问题。您正在数据库中保存用户,但该用户链接到尚未保存在数据库中的ApiKey。要告诉玩家它需要将它的ApiKey和它一起持久化,您需要在用户和ApiKey之间添加一个关于persist的级联
/**
* @ORM\OneToOne(targetEntity="ApiKey", mappedBy="user", cascade="persist")
*/
protected $api_key;
如果您还想删除ApiKey和用户,可以使用cascade={“persist”,“remove”}
您可以找到关于关联的所有条令文档
编辑:删除了所有者端不应该存在的行
/@ORM\JoinColumn(name=“id”,referencedColumnName=“user\u id”)
,正如Chausser所评论的那样,您似乎在这里遇到了级联问题。您正在数据库中保存用户,但该用户链接到尚未保存在数据库中的ApiKey。要告诉玩家它需要将它的ApiKey和它一起持久化,您需要在用户和ApiKey之间添加一个关于persist的级联
/**
* @ORM\OneToOne(targetEntity="ApiKey", mappedBy="user", cascade="persist")
*/
protected $api_key;
如果您还想删除ApiKey和用户,可以使用cascade={“persist”,“remove”}
您可以找到关于关联的所有条令文档
编辑:删除了所有者端不应该存在的行
/@ORM\JoinColumn(name=“id”,referencedColumnName=“user\u id”)
,正如Chausser所评论的那样这是Cyril所说的两倍,但这也是因为您的用户类中有额外的映射:
/**
* @ORM\OneToOne(targetEntity="ApiKey", mappedBy="user")
* @ORM\JoinColumn(name="id", referencedColumnName="user_id") // Remove this line
*/
protected $apiKey;
应该是:
/**
* @ORM\OneToOne(targetEntity="ApiKey", mappedBy="user")
*/
protected $apiKey;
只有实际执行映射的映射才需要join列。通过使用
mappedBy=“user”
您告诉doctrine查看ApiKey类user
属性以了解如何映射此关联。在这个is类中有一个连接列会导致问题。这是Cyril所说的两倍,但这也是因为您的用户类中有额外的映射:
/**
* @ORM\OneToOne(targetEntity="ApiKey", mappedBy="user")
* @ORM\JoinColumn(name="id", referencedColumnName="user_id") // Remove this line
*/
protected $apiKey;
应该是:
/**
* @ORM\OneToOne(targetEntity="ApiKey", mappedBy="user")
*/
protected $apiKey;
只有实际执行映射的映射才需要join列。通过使用
mappedBy=“user”
您告诉doctrine查看ApiKey类user
属性以了解如何映射此关联。在这个is类中有一个连接列会导致问题。尝试在$apiKey=new apiKey()之后添加“$user->setApiKey($apiKey);”;我加了你的零钱。但现在出现此错误:通过关系“AppBundle\entity\User\apiKey”找到了一个新实体,该关系未配置为级联实体:AppBundle\entity的持久化操作\ApiKey@000000004adc072f00000000540f2869. 要解决此问题,请对此未知实体显式调用EntityManager#persist(),或在映射中配置cascade persist此关联,例如@manytone(..,cascade={“persist”})。如果您无法找到导致问题的实体,请执行“AppBundle\entity\ApiKey#uu-toString()”以获取线索。在刷新之前也添加此行:$this->getdoctor()->getManager()->persist($ApiKey);现在出现此错误:AppBundle\Entity\ApiKey类型的实体缺少为字段“user\u ID”分配的ID。此实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果希望自动生成标识符,则需要相应地调整元数据映射。请尝试在$apiKey=new apiKey()之后添加“$user->setApiKey($apiKey);”;我加了你的零钱。但现在出现此错误:通过关系“AppBundle\entity\User\apiKey”找到了一个新实体,该关系未配置为级联实体:AppBundle\entity的持久化操作\ApiKey@000000004adc072f00000000540f2869. 要解决此问题,请对此未知实体显式调用EntityManager#persist(),或在映射中配置cascade persist此关联,例如@manytone(..,cascade={“persist”})。如果您无法找到导致问题的实体,请执行“AppBundle\entity\ApiKey#uu-toString()”以获取线索。在刷新之前也添加此行:$this->getdoctor()->getManager()->persist($ApiKey);现在出现此错误:AppBundle\Entity\ApiKey类型的实体缺少为字段“user\u ID”分配的ID。此实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果您希望自动生成标识符,则需要相应地调整元数据映射。我进行了更改,但现在出现以下错误:AppBundle\Entity\ApiKey类型的实体缺少为字段“user\u ID”分配的ID。此实体的标识符生成策略要求在调用EntityManager#persist()之前填充ID字段。如果希望自动生成标识符,则需要相应地调整元数据映射。我进行了更改,但现在出现以下错误:AppBundle\Entity\ApiKey类型的实体缺少