2013-11-20 78 views
1

我正在建立一个汽车维修系统。每辆车都有一个或多个附件。每个车辆和组件都由一个账户“拥有”。用户可能希望从车辆上拆下部件并将其放置在架子上,以便以后重新连接到同一辆或另一辆车上。该组件拥有一个vehicle_id字段,因此拆卸非常容易,我只需将vehicle_id字段置空,并且组件被有效“搁置”。先进的雄辩关系与枢轴?

我的困难在于找到可连接的组件来显示可连接到选定车辆的搁架部件列表。 vehicle_types_component_types表是定义哪些组件类型可以附加到哪些车型的枢轴。某些组件类型可以安装在多种车型上,每种车型都有一种或多种可以安装的组件类型。

class VehicleType extends Eloquent { 
    public function ComponentTypes() { return $this->belongsToMany('ComponentType', 'vehicle_types_component_types')->withTimestamps(); } 
} 

class Vehicle extends Eloquent { 
    public function Account() { return $this->belongsTo('Account', 'account_id'); } 
    public function VehicleType() { return $this->belongsTo('VehicleType', 'vehicle_type_id'); } 
    public function Components() { return $this->hasMany('Component'); } 
} 

class ComponentType extends Eloquent { 
    public function VehicleTypes() { return $this->belongsToMany('VehicleTypes', 'vehicle_types_component_types')->withTimestamps(); } 
    public function Components() { return $this->hasMany('Component'); } 
} 

class Component extends Eloquent { 
    public function Account() { return $this->belongsTo('Account', 'account_id'); } 
    public function Vehicle() { return $this->belongsTo('Vehicle', 'vehicle_id'); } // This is null when component is not attached to vehicle 
    public function ComponentType() { return $this->belongsTo('ComponentType', 'component_type_id'); } 
} 

所以说我希望做的是找到属于指定ACCOUNT_ID的所有组件,并且有一个空vehicle_id,并在组件的组件类型的车辆类型为“安装”中的定义, vehicle_types_component_types数据透视表。最重要的是,我还需要通过其组件类型排除某些组件......例如,component_type!='tire'。

我已经试过了显然是行不通的,但应证明什么,我试图做...

public function findAttachable($vehicle_type_id, $account_id) 
{ 
    return Component::with('ComponentType') 
     ->where('vehicle_id', null) 
     ->where('account_id', $account_id) 
     ->where('vehicle_type_component_type.vehicle_type_id', $vehicle_type_id) 
     ->where('vehicle_type_component_type.component_type_id', 'components.components_type_id') 
     ->where('component_types.type', '!=', 'tire'); 
} 

如果有东西像“withPivot”方法什么的,那会真的很酷...

$components = Component::where('vehicle_id', null) 
    ->where('account_id', $account_id) 
    ->withPivot('component_type_id', $vehicle_type_id); 

任何帮助,将不胜感激!提前致谢。

回答

0

我找到了一个似乎可行的解决方案。我只希望有一个更优雅的方式来做到这一点。如果有,请告诉我。

public function findAttachable($vehicle_type_id, $account_id) 
{ 
    return Component::with('ComponentType') 
     ->where('vehicle_id', null) 
     ->where('account_id', $account_id) 
     ->whereIn('component_type_id', function($query) use ($vehicle_type_id) { 
      $query->select('component_type_id') 
       ->from('vehicle_types_component_types') 
       ->where('vehicle_type_id', $vehicle_type_id); 
      }) 
     ->whereNotIn('component_type_id', function($query) { 
      $query->select('id') 
       ->from('component_types') 
       ->where('type', 'tire'); 
      }) 
     ->get(); 
} 
8

介绍

你的故事略显混乱,但我认为你正在尝试做这样的事情。 答案是3个月的晚,但因为你想要一个更优雅的方式在这里。

实际上有一种方法“withPivot”就像你建议的那样。它的工作原理与您所建议的略有不同。

- > withPivot( 'field1frompivottable', 'field2frompivottable', 'ECT')

参见:http://laravel.com/docs/eloquent#working-with-pivot-tables

表的:

表:车辆

  • ID
  • 名称
  • 类型

表:vehicletype

  • ID
  • 名称

表:成分

  • ID
  • 车辆

表:vehicletypecomponentpivottable

  • ID
  • vehicletypeid
  • 的ComponentID
  • 在停滞

锋型号:

车辆模型:

class Vehicle extends Eloquent { 

    protected $table = 'vehicle'; 

    public function type() { 
     return $this->belongsTo('Vehicletype', 'type'); 
    } 
    public function component() { 
     return $this->hasMany('Component', 'vehicle'); 
    } 

} 

车辆类型的模型:

class Vehicletype extends Eloquent { 

    protected $table = 'vehicletype'; 

    public function vehicle() { 
     return $this->hasMany('Vehicle', 'type'); 
    } 
    public function component() { 
     return $this->belongsToMany('Component', 'vehicletypecomponentpivottable', 'vehicletypeid', 'componentid') 
     ->withPivot('installed'); 
    } 

} 

组分:

class Component extends Eloquent { 

    protected $table = 'component'; 

    public function vehicle() { 
     return $this->belongsTo('Vehicle', 'vehicle'); 
    } 
    public function vehicletype() { 
     return $this->belongsToMany('Vehicletype', 'vehicletypecomponentpivottable', 'componentid', 'vehicletypeid') 
     ->withPivot('installed'); 
    } 

} 

控制器功能

public function get_uninstalled_components_that_fit_my_vehicle($vehicletypeid){ 
    return Vehicletype::find($vehicletypeid)->component()->wherePivot('installed', 0)->get(); 
} 

public function is_component_installed($componentid){ 
    return Component::find($componentid)->Vehicletype()->pivot->installed; 
} 

祝你好运!

希望这有助于!您可以使用wherePivotorWherePivot