2016-01-16 78 views
0

我試圖在cakephp 3中的兩個模型(服務器和application_endpoints)表之間創建關聯。我想實現的是將所有數據從application_endpoints表中獲取到服務器模型。所以在視圖中我可以訪問所有的數據。CakePHP 3在兩個表之間關聯

服務器可以在application_endpoints表中具有多個VIP和端點,並且我正在使用hasMany關聯。因此,在ServersTable.php我添加下面的代碼:

$this->hasMany('ApplicationEndpoints', [ 
      'foreignKey' => 'server_id', 
      'dependent' => true 
     ]); 

所以,當我瀏覽服務器視圖頁面,點擊調試工具包變量選項卡里面,我沒有看到任何連接。我看到陣列中的服務器列表,但陣列中的每個服務器都不顯示與application_endpoints表的任何關係。

請評論並讓我知道如果它不清楚,我也鏈接sqlfiddle關於mysql模式。

更新1

我也ServersTable.php嘗試這種代碼,以及:

$this->belongsTo('ApplicationEndpoints', [ 
      'bindingKey' => 'server_id', 
      'joinType' => 'INNER', 
     ]); 

回答

0

所以hasMay是讓兩款車型之間的關聯的正確途徑。

$this->hasMany('ApplicationEndpoints', [ 
     'foreignKey' => 'server_id', 
     'dependent' => true 
    ]); 

我不得不在服務器的控制器添加額外的代碼,使其與下面的代碼工作:

// View method 
    $query = $this->Servers->get($id, [ 
     'contain' => ['ApplicationEndpoints'] 
    ]); 
    $this->set('data', $query);