2014-03-01 5 views
0

여러 모델을 양식에 바인딩하려고합니다. 현재, 나는 4 개 모델이 있습니다Laravel 4 다른 모델을 통한 모델 관계

<?php 

class DemDataSet extends Eloquent { 

    public $timestamps = false; 
    protected $connection = 'epcr_dem_data'; 
    protected $table = 'DEMDataSet'; 
    protected $primaryKey = 'pk_DEMDataSet'; 

    public function DemographicReport(){ 
     return $this->hasOne('DemographicReport','fk_DemDataSet','pk_DemDataSet'); 
    } 

} 

class DemographicReport extends Eloquent { 

    public $timestamps = false; 
    protected $connection = 'epcr_dem_data'; 
    protected $table = 'DemographicReport'; 
    protected $primaryKey = 'pk_DemographicReport'; 

    public function DemDataSet(){ 
     return $this->belongsTo('DemDataSet','fk_DemDataSet','pk_DemDataSet'); 
    } 

    public function dAgency(){ 
     return $this->hasOne('dAgency','fk_DemographicReport','pk_DemographicReport'); 
    } 

} 

class dAgency extends Eloquent { 

    public $timestamps = false; 
    protected $connection = 'epcr_dem_data'; 
    protected $table = 'dAgency'; 
    protected $primaryKey = 'pk_dAgency'; 

    public function DemographicReport(){ 
     return $this->belongsTo('DemographicReport','fk_DemographicReport','pk_DemographicReport'); 
    } 

    public function dAgency_10(){ 
     return $this->hasMany('dAgency_10','fk_dAgency','pk_dAgency'); 
    } 

} 

class dAgency_10 extends Eloquent { 

    public $timestamps = false; 
    protected $connection = 'epcr_dem_data'; 
    protected $table = 'dAgency_10'; 
    protected $primaryKey = 'pk_dAgency_10'; 

    public function dAgency(){ 
     return $this->belongsTo('dAgency','fk_dAgency','pk_dAgency'); 
    } 

} 

?> 

을 그리고 난 그렇게처럼 내 컨트롤러를 통해 내보기에 전달 해요 :

나는 DemDataSet 모델로부터 데이터를 제외한 모든 결합 할 수있어
public function index() 
{ 
    //THIS is the line I need help with (I think): 
    $agency = dAgency::with('dAgency_10','DemographicReport')->find(1); 

    //for troubleshooting: 
    echo "<pre>",print_r(dAgency::with('dAgency_10','DemographicReport')->find(1)->toArray()),"</pre>"; 

    return View::make('test') 
     ->with('agency', $agency); 
} 

나는 그것과 나의 dAgency 모델 사이의 관계를 수립하는 방법을 알지 못한다. 기본적으로 DemDataSet 모델의 필드를 내보기에서 사용할 수있게하려면 결국 모든 CRUD 작업을 수행 할 수 있어야합니다.

답변

0

모델을 살펴보면 DemographicReport을 통해 액세스 할 수 있습니다.

즉,

$dataset = $agency->DemographicReport->DemDataSet; 
+0

여전히 작동하지 않습니다. 내 컨트롤러에서'echo "를 호출하면 DemDataSet이 비어 있음을 나타내는'[relations : protected] => Array ([DemDataSet] =>)'이 표시됩니다. 그러나 그 모델의 결과 ('echo "

",print_r(DemDataSet::find(1)),"
";')를 보면 분명히 데이터가 있습니다. 난 내 hasOne()과 belongsTo()를 두 번 확인했다. – jreed121

+0

'$ agency-> DemographicReport'는'fk_DemDataSet'에 대한 값을 갖고 있습니까? – duellsy

+0

네, 그렇습니다. 내 모델의 관계에는 분명히 문제가있다. 비슷한 문제가있는 비슷한 질문이 여기에 있습니다 : http://stackoverflow.com/questions/22165781/laravel-4-deleting-multiple-models-related-by-foreign-keys. 이 문제가 실제로 발생할 가능성에 대해 실제로 생각하기에 앞서 만들었습니다. 모양 사람을 가져 주셔서 감사합니다, 나는 이것 위에서 나 자신을 때리고 있었다. 그리고 나는 그것이 아마 정말로 단순한 무엇인가 알고있다. – jreed121