dongsunny1113 2013-10-27 23:19
浏览 86
已采纳

Eloquent ORM - 如何在父模型中处理与同一子模型的两个实例的关系

I have a parent model called Journey that should relate to two instances of model-type Address - once for 'origin' and once for 'destination'. I can't work out what's the most efficient way of doing this. I would like to use Laravel's Eloquent ORM with eager-loading to achieve something elegant like the following when I return a journey:

return Journey::with('origin','destination');

I've tried the following:

1) Using Eloquent ORM's one to one relationships

Journey
-------
id
user_id
date

Address
-------
id
journey_id
street
city
zipcode

Observations:

  • Requires 'journey_id' in the Address model, meaning I can't really re-use this model for anything other than Journeys
  • Address is linked to Journey as a whole, so I can't distinguish between origin and destination instances

2) Using Eloquent ORM's polymorphic relationships

Journey
-------
id
user_id
date

Address
-------
id
street
city
zipcode
addressable_id
addressable_type

Observations:

  • I can now re-use the Address model with other models, but...
  • Address is still linked to Journey as a whole, so I can't distinguish between origin and destination instances

3) Relating 'journey.origin' or 'journey.destination' by joining it to 'address.id'

Journey
-------
id
user_id
origin (address.id)
destination (address.id)
date

Address
-------
id
street
city
zipcode

Observations:

  • I can now distinguish between the origin & destination instances, but...
  • I think I have to use fluent queries for the joins, so I don't get origin & destination back as Address models
  • There is no link back from the address to the journey

Does anyone have any suggestions for table / relationship structure that makes journey (with origin & destination) insertion and selection as elegant as possible, preferably using Eloquent so that I can utilise my model attributes and functions?

Also, although accessing the addresses through the Journey model will be the primary use case, I'd also like to be able to access the parent Journey through the Address model.

  • 写回答

1条回答 默认 最新

  • duanjiao5261 2013-10-27 23:52
    关注

    What you should have is:

    Database Strucuture

    Journey
    ---------
    id
    origin_id
    destionation_id
    user_id
    date
    
    Address
    ---------
    id
    street
    city
    zipcode
    

    Then, link your Journey model to Address using a One-to-Many relationship:

    Journey.php

    class Journey extends Model {
        // ...
    
        public function origin() {
            return $this->belongsTo('Address');
        }
    
        public function destination() {
            return $this->belongsTo('Address');
        }
    }
    

    Address.php

    class Address extends Model {
        // ...
    
        public function journeysAsOrigin() {
            return $this->hasMany('Journey', 'origin_id');
        }
    
        public function journeysAsDestination() {
            return $this->hasMany('Journey', 'destination_id');
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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