教条实体关系混乱 [英] Doctrine Entities Relations confusing

查看:244
本文介绍了教条实体关系混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过几个小时的努力,我明白,我不明白这个概念。我有三个实体定义如下:

After some hours trying to understand I can't get the concept clear. I have three entities defined as shown:

class Countries
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="flag", type="string", length=100, nullable=true)
     */
    private $flag;

    /**
     * @ORM\OneToMany(targetEntity="Regions", mappedBy="country_id")
     */
    private $regions_in_country;

    /**
     * @ORM\OneToMany(targetEntity="Cities", mappedBy="country_id")
     */
    private $cities_in_country;

    public function __construct() {
        $this->regions_in_country= new ArrayCollection();
        $this->cities_in_country= new ArrayCollection();
    }
}







class Regions
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100)
     */
    private $name;

    /**
     * @var int
     *
     * @ORM\Column(name="country_id", type="integer")
     * @ORM\ManyToOne(targetEntity="Countries", inversedBy="regions")
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id")
     */
    private $countryId;

    /**
     * @ORM\OneToMany(targetEntity="Cities", mappedBy="region_id")
     */
    private $cities_in_region;

    public function __construct() {
        $this->cities_in_region= new ArrayCollection();
    }
}







class Cities
{
    /**
    * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var int
     *
     * @ORM\Column(name="country_id", type="integer")
     * @ORM\ManyToOne(targetEntity="Countries", inversedBy="cities_in_country")
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id")
     */
    private $countryId;

    /**
     * @var int
     *
     * @ORM\Column(name="region_id", type="integer")
     * @ORM\ManyToOne(targetEntity="Regions", inversedBy="cities_in_region")
     * @ORM\JoinColumn(name="region_id", referencedColumnName="id")
     */
    private $regionId;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=100)
     */
    private $name;
}

问题是Profiler说:

The problem is that the Profiler says:

AppBundle\Entity\Regions    

The association AppBundle\Entity\Regions#cities_in_region refers to
the owning side field AppBundle\Entity\Cities#region_id which does not exist.

但是,AppBundle\Entity\Cities中的fields_id区域存在!!

But the field region_id in AppBundle\Entity\Cities exists!!

任何建议?

EDITED:
感谢Rafix和Alvin的概念。我是一个新的教条/ Symfony,有时候...

EDITED: Thanks Rafix and Alvin for the concepts. I'm a new to Doctrine/Symfony and some times...

其实我想做的是使用一个选择与CountryType字段的形式,事实是它使用code_2a作为值。

In fact what I want to do is to use in a form a select with a CountryType field, the thing is that it uses code_2a as values.

在我的实体中,像user_profile,我将有一个字段国家与'code_2a',OneToOne到实体国家和然后在Region实体中使用此字段。这意味着国家实体将有一个字段'code_2a'OneToMany指向区域实体,它将有一个'code_2a'ManyToOne反转到国家。因为他们不是'id'我不知道该怎么做。没有例子来说明如何做到这一点。

In my entities, like user_profile, I will have a field country with the'code_2a', OneToOne to the entity Country and then use this field in the Region entity. That means that the Country entity will have a field 'code_2a' OneToMany pointing to the Region Entity, which will have a 'code_2a' ManyToOne reversing to Country. Because they are not 'id' I wonder how to do it. there are no examples to figure how to do it.

推荐答案

类可以像下面的例子那样创建。
我基于@rafix的答案,但有一些问题。

The classes can be created like my example below. I based this on @rafix 's answer, but there were some problems.

另外,你不以@rafix为单位命名你的类,你可以命名他们,但是你想要的。但是,这样做更好的做法,使您的代码更容易阅读和支持。

Also, you don't have to name your classes in the singular like @rafix indicated, you can name them however you want. But, it's much better practive to do it that way and make your code much easier to read and support.

例如:如果您有一个名为国家的类,那么你知道一个国家有城市,每个城市都是城市。所以课程是指单个对象。

For example: If you have a class called "Country", then you know a country has "cities", and each one of those "cities" is a "City". So the class refers to the individual object.

以下是我建议的更改:

class Country
{
    /**
     * @ORM\Column(name="country_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
   private $country_id;

    /**
     * @ORM\Column(name="flag", type="string", length=100, nullable=true)
     */
    private $flag;

   /**
    * @ORM\OneToMany(targetEntity="Region", mappedBy="country")
    */
   private $regions = null;

   public function __construct() {
       $this->regions = new ArrayCollection();

   }
}







class Region
{
    /**
     * @ORM\Column(name="region_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $region_id;

    /**
     * @ORM\Column(name="name", type="string", length=100)
     */
    private $name;

    /**
     * @ORM\ManyToOne(targetEntity="Country", inversedBy="regions")
     * @ORM\JoinColumn(name="region_country_id", referencedColumnName="country_id")
     */
    private $country;

    /**
     * @ORM\OneToMany(targetEntity="City", mappedBy="region")
     */
    private $cities = null;

    public function __construct() {
        $this->cities = new ArrayCollection();
    }
}







class City
{
    /**
     * @ORM\Column(name="city_id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $city_id;

    /**
     * @ORM\ManyToOne(targetEntity="Region", inversedBy="cities")
     * @ORM\JoinColumn(name="region_id", referencedColumnName="region_id")
     */
    private $region;

    /**
     * @ORM\Column(name="name", type="string", length=100)
     */
    private $name;
}

注意,我重命名了每个类中的每个id所以你会知道这是该特定实体的ID。例如:国家类有一个country_id。

Notice, I renamed each of the "id" s in each of the classes so you would know that it is the ID for that particular Entity. For example: The Country class has a "country_id".

另外,因为例如,Country类有regions,你需要将数组声明为null 。确保您在完成代码后,将正确的 addRegion 添加到数组中。

Also, since for example, the Country class has "regions", you need to declare the array as "null". Make sure you have the proper addRegion to add regions to the array when you finish your code.

这篇关于教条实体关系混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆