通过外国实体进行身份识别

发布于 2024-12-05 10:59:18 字数 2177 浏览 0 评论 0原文

我有一个将用户链接到项目的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世晨晓 2024-12-12 10:59:18

可能会发生此错误,因为您正在寻找的密钥不在关系中...当您运行许多数据装置时会发生这种情况:导入及其带有 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文