2017-01-13 163 views
1

我有四個表CakePHP的多層次關係

course_schedules

id name 

course_prices

id price currency_id 

price_schedules

id course_schedule_id course_price_id 

貨幣

id name code 
  1. 課程表模型有很多價格清單

  2. 課程的價格擁有衆多價格清單

  3. 課程價格屬於貨幣

  4. 價格表屬於課程表

  5. Price Sche獨樂寺屬於課程價格

  6. 貨幣有很多課程價格

現在,這一切的東西工作正常,但我的問題是

在PriceSchedulesController

我能得到course_schedules表的詳細信息course_prices表詳細信息

但我也希望獲得貨幣代碼以及價格像200美元 這意味着price_schedules間接對貨幣

依賴沒有爲這種關係的

public function index() { 
    $this->PriceSchedule->recursive = 0; 
    $this->set('PriceSchedules', $this->Paginator->paginate()); 
} 

結果的任何技術

Array 
(
    [CourseSchedule] => Array 
     (
      [id] => 1 
      [name] => course one 
     ) 
    [CoursePrice] => Array 
     (
      [id] => 2 
      [currency_id] => 1 
      [price] => 200 
     ) 
) 

可以在陣列currency_id看到的是有,但對像貨幣代碼是什麼200美元

回答

1

根據您的關係,您的price_schedules表已與貨幣相關:

1.price_schedules belongs to course_price 
2.course_price belongs to currency 
3.hence price_schedule also dependent on currency 

當您檢索數據,您需要使用含有好,如:

public function index() { 
    // $this->PriceSchedule->recursive = 0; 
    $this->paginate = [ 
    'contain'=>['CoursePrice.Currency'] // using contain this way you can easiy get that 
    ]; 
    $this->set('PriceSchedules', $this->Paginator->paginate()); 
} 

或者你也可以做到這一點從遞歸特性,以及:

$this->PriceSchedule->recursive = 2;// this will retrieve all the related models 
+0

謝謝你這麼多 – daulat

+0

不用謝 ! –