Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.6k views
in Technique[技术] by (71.8m points)

php - Connect multiple entities to a locations entity that has a composite primary key

I have some entities that define the locations such that:

Cities

/**
 * cities
 *
 * @ORMEntity
 * @ORMTable(name="cities")
 */
class cities
{
    /**
     * @var int
     *
     * @ORMId
     * @ORMColumn(name="id", type="integer")
     * @ORMGeneratedValue(strategy="NONE")
     */
    private $id;

    /**
     * @var string
     *
     * @ORMId
     * @ORMColumn(name="lang", type="string", length=5)
     * @ORMGeneratedValue(strategy="NONE")
     */
    private $lang;

    /**
     * @var Provinces
     *
     * @ORMManyToOne(targetEntity="Provinces")
     * @ORMJoinColumns({
     *   @ORMJoinColumn(name="id_province", referencedColumnName="id"),
     *   @ORMJoinColumn(name="lang", referencedColumnName="lang")
     * })
     */
    private $idProvince;

    /**
     * @var int
     *
     * @ORMColumn(name="id_region", type="integer")
     */
    private $idRegion;

    /**
     * @var int
     *
     * @ORMColumn(name="id_country", type="integer")
     */
    private $idCountry;

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

    /**
     * @var geometry
     *
     * @ORMColumn(name="chords", type="geometry", nullable=false)
     */
    private $chords;
    
    // ... getters and setters
}

This is followed by the entity of provinces, regions and countries that have a similar structure. Since each city has an ID, I create an ID-LANG composite index, in which I have a city, for example Madrid, with the ID: 125 and this one in five languages:

  • ID: 125 LANG: ES
  • ID: 125 LANG: EN
  • ID: 125 LANG: FR
  • ID: 125 LANG: PT
  • ID: 125 LANG: IT

This entity will be connected to all the entities in which you need to save an address. And to be able to use the different languages, I don't use the name of the city, but rather I use its ID and show the name based on the language the user navigates in.

An example of an entity in which I need the address is for example the Business entity:

/**
 * Empresas
 *
 * @ORMEntity
 * @ORMTable(name="business")
 */
class Business
{
    /**
     * @var int
     *
     * @ORMId
     * @ORMColumn(name="id", type="integer")
     * @ORMGeneratedValue
     */
    private $id;

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

    /**
     * @var int
     *
     * @ORMColumn(name="id_city", type="integer")
     */
    private $idCity;

    /**
     * @var int
     *
     * @ORMColumn(name="id_province", type="integer")
     */
    private $idProvince;

    /**
     * @var int
     *
     * @ORMColumn(name="id_region", type="integer")
     */
    private $idRegion;

    /**
     * @var int
     *
     * @ORMColumn(name="id_country", type="integer")
     */
    private $idCounry;

    /**
     * @var string
     *
     * @ORMColumn(name="img", type="string", length=250)
     */
    private $img;

    /**
     * @var Cities
     *
     * @ORMOneToOne(targetEntity="Cities", mappedBy="id") || @ORMOneToMany(targetEntity="Cities", mappedBy="id")
     */
    private $city;

    /**
     * @var Provinces
     */
    private $province;

    /**
     * @var Regions
     */
    private $region;

    /**
     * @var Countries
     */
    private $country;
}

As you can see in my code I try to connect the $city property with the Cities entity to obtain all the city data.

I don't care if it has to be OneToOne to get and have the city data already in their language or if it should be OneToMany and then search within the ArrayCollection for the language corresponding to the user.

What I would like is for it to be unidirectional from Business to Cities since if I do it bidirectional (in this case I have no problems) a large amount of data accumulates and I also have to be introducing a property in the city entity for each entity that connects to the cities entity. Since the entity of cities will also be connected to other entities such as Users, Clients, Workers, etc ...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...