通过外国实体的身份

时间:2011-09-18 16:09:29

标签: doctrine-orm

我有一个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;
}

}

...但是,尝试构建此错误会导致:“没有为实体指定身份/主键...”

我当前的解决方法(正确识别关系)是手动设置ID:

<?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;  

1 个答案:

答案 0 :(得分:0)

可能会发生此错误,因为您要查找的密钥不在关系中...当您运行许多数据夹具时会发生这种情况:导入及其带有ID的夹具是静态的......

我也有这个问题,然后清理整个基础,包括序列,看看是否有效。

考虑为应用程序创建单元测试,以确保您的课程正确,从而解决问题。

拥抱