2014-09-30 7 views
2

나는 차량 클래스 morphTo (버스)와 MorphTo (SUV)와 MorphTo (세단)를 가지고있다.laravel 4 다형성 관계 저장소를 구현하는 방법

VehicleController에 저장할 때 vehicleRepositoryInterface, vehicleRepository를 호출했습니다.

버스 나 SUV 또는 세단 데이터를 어떻게 저장할 수 있습니까?

내 차량 클래스 morphTo 관계로 (차량 종류가 버스/SUV/밴 인) 경우 처리하는 방법

Class Vehicle controller{ 
    public function get_create(){ 
    if($this->vehicle->create(Input::all())) 
      return Redirect::to() 
    } 
      return Redirect::to()->withInput()->withErrors($this->vehicle->getMessages()); 
} 

내 차량 저장소 클래스?

Class VehicleRepository Implements VehicleRepositoryInterface{ 
    public function create($array $inputs){ 
     if($this->validatator->IsValid($inputs)){ 
       // How to implement BUS or SUV or Van in here 
       $this->model->create($inputs); 
       return true; 
     } 
     return false; 
    } 
} 
+0

마지막으로 저장소에 저장하기 위해 다형성 관계에 대한 답을 얻었습니다. 작성 함수에서 if ($ this-> validator-> isValid ($ attributes) {} –

답변

0

마지막으로 저장소에 저장하기 위해 다형성 관계에 대한 답을 얻었습니다.

에서이 function--

if($this->validator->isValid($inputs){ 
     // first store in temp model. $this model will be destroyed. 
     $temp_model = $this->model->create($inputs); 
     //then Depency Injection comein for polymorphic model 
     $this->polymorphicmodel->create($subinputs): 
     // link with polymorphic relation 
     $this->polymorphicmodel->modelable()->save($temp_model); 
     return true; 
    } 
    return false; 

정말 접근 더 나은 고급 수정 또는 제안을 주셔서 감사 만듭니다.