首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理论2关联映射(ManyToOne和OneToMany)

理论2关联映射(ManyToOne和OneToMany)
EN

Stack Overflow用户
提问于 2013-12-20 13:47:59
回答 1查看 246关注 0票数 0

我是Doctrine2的新手,需要一些帮助。我有两个实体: LocationEntity和RatingEntity RatingEntity

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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。

有人能帮忙吗?

EN

回答 1

Stack Overflow用户

发布于 2013-12-20 14:29:30

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20705051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档