dqyat62284 2013-05-27 19:21
浏览 13
已采纳

教义复合键

I have these 3 table

"Business" with these fields: Id, Name, etc.. "City" with these fields: Id, Name, etc..

And then I have a table called BusinessCity (given that a bussines can be related to many cities). This table has the fields "BusinessId" and "CityId".

Im trying to relate the CityId to the City entity, and BusinessId to the business entity, on the class BusinessCity. I've been googling this for the past 3 days and couldnt find an answer, if this has been asked before im sorry i didnt see it. Could anyone help me or give me some pointers on how to get this done. Thanks in advance

  • 写回答

2条回答 默认 最新

  • duanpaxin3531 2013-05-27 20:31
    关注

    What you are trying to achieve is a bi-directional many-to-many relation with a joinTable.

    Many businesses can reside in multiple cities and in one city there can be multiple businesses.

    In a many-to-many relationship either side can be the owning side. JoinTable definition can be left out and has sensible defaults but if you want to specify it concretely i included it in the example.

    Business (in this example: owning side = inversedBy = JoinTable definition)

    /**
     * @ORM\ManyToMany(targetEntity="Your/Bundle/City", inversedBy="businesses",cascade="{persist,merge}" fetch="EAGER")
     * @ORM\JoinTable(name="BusinessCity",
     *    joinColumns={@JoinColumn(name="business_id", referencedColumnName="id")},
     *    inverseJoinColumns={@JoinColumn(name="city_id", referencedColumnName="id")}
     *  )
     */
    protected $cities;
    
    public function __construct()
    {
       $this->cities = new ArrayCollection();
    }
    
    public function getCities()
    {
       return $this->cities;
    }
    
    public function setCities(Collection $cities)
    {
       // using map with closure to have dublicate/type-checking provided by addCity
       $this->cities->map(function($city) {
           $this->addCity($city);
       });
    
       return $this;
    }
    
    public function addCity(CityInterface $city)
    {
        // ... you don't want dublicates in your collection
        if (!$this->cities->contains($city)) {
           $this->cities->add($city);
        }
    
       return $this;
    }
    
    public function removeCity(CityInterface $city)
    {
        $this->cities->removeElement($city);
    
        return $this;
    }
    // other properties and methods ..
    

    City (inverse side = mappedBy)

    /**
     * @ORM\ManyToMany(targetEntity="Your/Bundle/Business", mappedBy="cities")
     */
    protected $businesses;
    
    // getters & setters ...
    // other properties and methods ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用