2013-08-31 60 views
0

我在尋找一些幫助,爲什麼我在學說中的OneToMany關係只返回一個值。我的數據模型有三個表。用戶,授權和應用程序。授權表是將用戶映射到應用程序的粘合劑,並且還包含訪問級別字段以指示其對該應用程序的訪問級別。OneToMany Relationship Only Only 1 Row

我有一個用戶在三個不同的應用程序的授權中有三個條目,但由於某種原因,原則只加載了這些授權記錄中的一個。我已經在下面包含了我的完整數據模型。有問題的屬性是Webusers表中的「授權」。

任何人都知道我在做什麼錯了?

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(); 
} 
} 

回答

0

我能夠確保我指定了該網站用戶和應用關係,多對一,並給他們的@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; 
}