通过外国实体进行身份识别
我有一个将用户链接到项目的 UserProject 实体,为此我希望 $projects_id 和 $users_id 作为主键。按照此处的示例: http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/composite-primary-keys.html#identity-through-foreign-entities 我期待一些东西像下面这样工作:
<?php
namespace Hoj\Entity;
/**
* @Entity (repositoryClass="Hoj\Entity\Repository\UserProjectRepository")
* @Table(name="users_projects")
*/
class UserProject
{
/**
* @Id
* @ManyToOne(targetEntity="Project", cascade={"all"}, fetch="EAGER", inversedBy="userProject")
*/
private $project;
/**
* @Id
* @ManyToOne(targetEntity="User", cascade={"all"}, fetch="EAGER", inversedBy="userProject")
*/
private $user;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}
...但是尝试构建此结果会导致错误:“没有为实体指定身份/主键...”
我当前的解决方法(正确识别关系)是手动设置 ids像这样:
<?php
namespace Hoj\Entity;
/**
* @Entity (repositoryClass="Hoj\Entity\Repository\UserProjectRepository")
* @Table(name="users_projects")
*/
class UserProject
{
/** @Id @Column(type="integer", length=11) */
private $users_id;
/** @Id @Column(type="integer", length=11) */
private $projects_id;
/**
* @var Project
* @ManyToOne(targetEntity="Project", cascade={"all"}, fetch="EAGER")
* @JoinColumns({
* @JoinColumn(name="projects_id", referencedColumnName="id")
* })
*/
private $project;
/**
* @var User
* @ManyToOne(targetEntity="User", cascade={"all"}, fetch="EAGER")
* @JoinColumns({
* @JoinColumn(name="users_id", referencedColumnName="id")
* })
*/
private $user;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
项目
表和用户表像这样连接回来:
/**
* @param \Doctrine\Common\Collections\Collection $userProject
* @OneToMany(targetEntity="UserProject", mappedBy="project", cascade={"persist","remove"})
*/
private $userProject;
I have a UserProject entity linking users to projects, for this I would like $projects_id and $users_id to be the the primary keys. Following the example here: http://www.doctrine-project.org/docs/orm/2.0/en/tutorials/composite-primary-keys.html#identity-through-foreign-entities I expected something like the following to work:
<?php
namespace Hoj\Entity;
/**
* @Entity (repositoryClass="Hoj\Entity\Repository\UserProjectRepository")
* @Table(name="users_projects")
*/
class UserProject
{
/**
* @Id
* @ManyToOne(targetEntity="Project", cascade={"all"}, fetch="EAGER", inversedBy="userProject")
*/
private $project;
/**
* @Id
* @ManyToOne(targetEntity="User", cascade={"all"}, fetch="EAGER", inversedBy="userProject")
*/
private $user;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}
...however trying to build this results in an error: "No identity/primary key specified for Entity..."
My current work around (which correctly recognises the relationships) is to set the ids manually like so:
<?php
namespace Hoj\Entity;
/**
* @Entity (repositoryClass="Hoj\Entity\Repository\UserProjectRepository")
* @Table(name="users_projects")
*/
class UserProject
{
/** @Id @Column(type="integer", length=11) */
private $users_id;
/** @Id @Column(type="integer", length=11) */
private $projects_id;
/**
* @var Project
* @ManyToOne(targetEntity="Project", cascade={"all"}, fetch="EAGER")
* @JoinColumns({
* @JoinColumn(name="projects_id", referencedColumnName="id")
* })
*/
private $project;
/**
* @var User
* @ManyToOne(targetEntity="User", cascade={"all"}, fetch="EAGER")
* @JoinColumns({
* @JoinColumn(name="users_id", referencedColumnName="id")
* })
*/
private $user;
public function __get($property)
{
return $this->$property;
}
public function __set($property, $value)
{
$this->$property = $value;
}
}
The project and user tables join back like so:
/**
* @param \Doctrine\Common\Collections\Collection $userProject
* @OneToMany(targetEntity="UserProject", mappedBy="project", cascade={"persist","remove"})
*/
private $userProject;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能会发生此错误,因为您正在寻找的密钥不在关系中...当您运行许多数据装置时会发生这种情况:导入及其带有 ids 的装置是静态的...
我也遇到了这个问题,然后清理了整个基础包括序列并看到如此有效。
考虑为应用程序创建单元测试,以便确保您的类是正确的,从而解决问题。
拥抱
probably this error occurs because the key you are looking for is not in the relationship ... it happens when you run many data-fixtures: import and its fixtures with ids are static ...
I had this problem too, then cleaned the entire base including sequence and see so worked.
Consider creating unittests for application so you ensure that your class is correct and so fro solving the problem.
hug