OneToMany关系仅返回1行

时间:2013-08-31 04:07:50

标签: doctrine-orm doctrine one-to-many

我正在寻找一些帮助,以解释为什么我在教义中的OneToMany关系只返回一个值。我的数据模型有三个表。用户,授权和应用程序。 Authorization表是将用户映射到应用程序的粘合剂,还包含一个访问级别字段,用于指示其对该应用程序的访问级别。

我有一个用户在三个不同的应用程序的授权中有三个条目,但由于某种原因,doctrine只加载其中一个授权记录。我在下面列出了我的完整数据模型。有问题的属性是Webusers表中的“authorization”。

任何人都知道我做错了什么?

class Webusers {
/**
 * @var integer
 *
 * @ORM\Column(name="userid", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $userid;    
/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Applications", mappedBy="userid")
 */
private $applications;

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\OneToMany(targetEntity="Authorization", mappedBy="user")
 */
private $authorization;

/**
 * Constructor
 */
public function __construct() {
    $this->applications = new \Doctrine\Common\Collections\ArrayCollection();
    $this->authorization = new \Doctrine\Common\Collections\ArrayCollection();  
}

class Authorization {
/**
 * @var integer
 *
 * @ORM\Column(name="accesslevel", type="integer", nullable=false)
 */
private $accesslevel;

/**
 * @var integer
 *
 * @ORM\Column(name="applicationid", type="integer", nullable=false)
 * $ORM\Id
 */
private $applicationid;

/**
 * @var integer
 *
 * @ORM\Column(name="userid", type="integer", nullable=false)
 * @ORM\Id
 */
private $userid;

 /**
 * @var \Webusers
 *
 * @ORM\ManyToOne(targetEntity="Webusers")
 * @ORM\JoinColumn(name="userid", referencedColumnName="userid")
 */
private $user;
}

class Applications {
/**
 * @var integer
 *
 * @ORM\Column(name="applicationid", type="integer", nullable=false)
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $applicationid;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=50, nullable=false)
 */
private $name;
/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="Webusers", inversedBy="applicationid")
 * @ORM\JoinTable(name="authorization",
 *   joinColumns={
 *     @ORM\JoinColumn(name="applicationid", referencedColumnName="applicationid")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="userid", referencedColumnName="userid")
 *   }
 * )
 */
private $userid;

/**
 * Constructor
 */
public function __construct()
{
    $this->userid = new \Doctrine\Common\Collections\ArrayCollection();
}
}

1 个答案:

答案 0 :(得分:0)

我能够通过确保将webusers和应用程序关系指定为ManyToOne并为它们提供@ORM \ Id属性来解决这个问题,因为它们构成了复合主键。

我认为失败的主要原因是因为我没有将授权与应用程序相关联。

我的新模型的关键点如下:

class Webusers {
 /**
 * @var \Doctrine\Common\Collections\Collection
 * @ORM\OneToMany(targetEntity="Authorization", mappedBy="user")
 */
 private $authorization;
 }

class Authorization {
/**
 *
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Applications")
 * @ORM\JoinColumn(name="applicationid", referencedColumnName="applicationid")
 */
private $application;

 /**
 * 
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Webusers")
 * @ORM\JoinColumn(name="userid", referencedColumnName="userid")
 */
private $user;
}

class Applications {
/**
 * @ORM\OneToMany(targetEntity="Authorization", mappedBy="application")
 */
 private $authorization;
}
相关问题