2013-12-20 159 views
0

我是新的Doctrine2,需要一些幫助。 我有2個實體:LocationEntity和RatingEntity RatingEntity學說2關聯映射(ManyToOne和OneToMany)

<?php 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Rating 
* 
* @ORM\Entity 
* @ORM\Table(name="`RATING`") 
*/ 
class RatingEntity { 

    /** 
    * 
    * @var int 
    * @ORM\Id 
    * @ORM\Column(name="`ID`", type="integer", nullable=false) 
    * @ORM\GeneratedValue(strategy="SEQUENCE") 
    * @ORM\SequenceGenerator(sequenceName="RATING_ID_SEQ", initialValue=1, allocationSize=1) 
    */ 
    protected $id; 

    /** 
    * 
    * @var int 
    * @ORM\Column(name="`LOCATION_ID`", type="integer") 
    */ 
    protected $locationId; 

    /** 
    * @var LocationEntity 
    * @ORM\ManyToOne(targetEntity="LocationEntity", inversedBy="ratings") 
    * @ORM\JoinColumn(name="`LOCATION_ID`", referencedColumnName="`ID`") 
    */ 
    protected $location; 

    /** 
    * @return the $location 
    */ 
    public function getLocation() { 
     return $this->location; 
    } 

    /** 
    * @param field_type $location 
    */ 
    public function setLocation($location) { 
     $this->location = $location; 
    } 

LocationEntity

<?php  

use Doctrine\ORM\Mapping as ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\Common\Collections\Collection; 

/** 
* Location 
* 
* @ORM\Entity 
* @ORM\Table(name="`LOCATION`") 
*/ 
class LocationEntity { 

    /** 
    * 
    * @var int 
    * @ORM\Id 
    * @ORM\Column(name="`ID`", type="integer", nullable=false) 
    * @ORM\GeneratedValue(strategy="SEQUENCE") 
    * @ORM\SequenceGenerator(sequenceName="LOCATION_ID_SEQ", initialValue=1, allocationSize=1) 
    */ 
    protected $id;    

    /** 
    * 
    * @var ArrayCollection 
    * @ORM\OneToMany(targetEntity="RatingEntity", mappedBy="location", cascade={"persist"}, fetch="LAZY") 
    */ 
    protected $ratings; 

    /** 
    * @return the $ratings 
    */ 
    public function getRatings() { 

     return $this->ratings; 
    } 

    /** 
    * @param \Doctrine\Common\Collections\ArrayCollection $ratings 
    */ 
    public function setRatings($ratings) { 

     $this->ratings = $ratings; 
    } 

    /** 
    * Never forget to initialize all your collections! 
    */ 
    public function __construct() { 
     $this->ratings = new ArrayCollection(); 
    } 

如果我的位置,我得到收視位置爲陣, 但如果我想獲取它的評分和位置,我只在getLocation()中獲得NULL。

有人可以幫忙嗎?

回答

0

我不認爲您需要您的評級實體中的locationId屬性,因爲該列已使用位置屬性映射。嘗試刪除locationId部分,看看它是否按預期工作。

+0

這是舊代碼的一部分,我將實體從字段更改爲對象。我已經嘗試刪除它。這沒有幫助。 – user1110233