原则 2 关联映射(多对一和多对多)



我是Doctrine2的新手,需要一些帮助。我有 2 个实体:位置实体和评级实体评级实体

<?php
use DoctrineORMMapping as ORM;
/**
 * Rating
 *
 * @ORMEntity
 * @ORMTable(name="`RATING`")
 */
class RatingEntity {
    /**
     *
     * @var int
     * @ORMId
     * @ORMColumn(name="`ID`", type="integer", nullable=false)
     * @ORMGeneratedValue(strategy="SEQUENCE")
     * @ORMSequenceGenerator(sequenceName="RATING_ID_SEQ", initialValue=1, allocationSize=1)
     */
    protected $id;
    /**
     *
     * @var int
     * @ORMColumn(name="`LOCATION_ID`", type="integer")
     */
    protected $locationId;
    /**
     * @var LocationEntity
     * @ORMManyToOne(targetEntity="LocationEntity", inversedBy="ratings")
     * @ORMJoinColumn(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;
    }

位置实体

<?php       
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
/**
 * Location
 *
 * @ORMEntity
 * @ORMTable(name="`LOCATION`")
 */
class LocationEntity {
    /**
     *
     * @var int
     * @ORMId
     * @ORMColumn(name="`ID`", type="integer", nullable=false)
     * @ORMGeneratedValue(strategy="SEQUENCE")
     * @ORMSequenceGenerator(sequenceName="LOCATION_ID_SEQ", initialValue=1, allocationSize=1)
     */
    protected $id;            
    /**
     *
     * @var ArrayCollection
     * @ORMOneToMany(targetEntity="RatingEntity", mappedBy="location", cascade={"persist"}, fetch="LAZY")
     */
    protected $ratings;
    /**
     * @return the $ratings
     */
    public function getRatings() {
        return $this->ratings;
    }
    /**
     * @param DoctrineCommonCollectionsArrayCollection $ratings
     */
    public function setRatings($ratings) {
        $this->ratings = $ratings;
    }
    /**
     * Never forget to initialize all your collections!
     */
    public function __construct() {
        $this->ratings = new ArrayCollection();
    }
如果我得到位置,

我会得到位置作为数组的评级,但是如果我想为它获得评级和位置,我只在 getLocation() 中得到 NULL。

有人可以帮忙吗?

我认为您的评级实体中不需要 locationId 属性,因为该列已经使用 location 属性进行了映射。尝试删除 locationId 部分,看看它是否无法按预期工作。

最新更新