2014-01-20 23 views
0

讓我們例如在http://book.cakephp.org/2.0/en/models/associations-linking-models-together.htmlCakePHP命名約定:需要顯式聲明嗎?

使用實體RecipeIngredient並按照約定找到的文檔,在hasAndBelongsToMany關係,連接表將被稱爲ingredients_recipes

據我瞭解,下面的命名約定的一點是讓cakePHP做的班級自動佈線而無需顯式聲明一些變量,在這種情況下,該表的名稱:ingredients_recipes

現在,在下面的示例中(直接從上面的鏈接中提取),它們明確地聲明'foreignKey' => 'recipe_id','associationForeignKey' => 'ingredient_id''joinTable' => 'ingredients_recipes'

我的問題是:即使我遵循了表,外鍵等的命名約定是否必須顯式聲明連接表,外鍵等的名稱,或者它們是否在示例中聲明下面只是爲了向讀者展示一個可能的輸入值?

// Example extracted from the documentation 
class Recipe extends AppModel { 
    public $hasAndBelongsToMany = array(
     'Ingredient' => 
      array(
      'className' => 'Ingredient', 
      'joinTable' => 'ingredients_recipes', 
      'foreignKey' => 'recipe_id', 
      'associationForeignKey' => 'ingredient_id', 
      'unique' => true, 
      'conditions' => '', 
      'fields' => '', 
      'order' => '', 
      'limit' => '', 
      'offset' => '', 
      'finderQuery' => '', 
      'with' => '' 
      ) 
    ); 
} 

回答

2

如果遵循的一切,你不必編寫的代碼,整個負載,用一個簡單的

public $hasAndBelongsToMany = array('Ingredient'); 

你會沒事的。他們給了你這個例子,告訴你你可以用配置做多少事情,但除非你想改變那裏設置的任何默認值,否則你可以使用單線程。