私はいくつかの関係を持つデータベースを持っています。この例には 2 つのテーブルがあります。
activity_ingredients ingredient_aliases
--------------- ---------------
id id
ingredient_id ingredient_id
フィールド「ingredient_id」でテーブルを接続する条件をモデルに作成したいと考えています。私はこのモードで行いました:
class ActivityIngredients extends AppModel{
public $name = 'ActivityIngredients';
public $useTable = 'activity_ingredients';
public $hasMany = array (
'IngredientAlias' => array (
'className' => 'IngredientAlias',
'foreignKey' => false,
'conditions' => array('ActivityIngredients.ingredient_id = IngredientAlias.ingredient_id')
)
);
}
class IngredientAlias extends AppModel {
public $name = 'IngredientAlias';
public $useTable = 'ingredient_aliases';
public $belongsTo = array(
'ActivityIngredients' => array(
'className' => 'ActivityIngredients',
'foreignKey' => false
)
);
}
このコードを書くと、次のようなエラーが表示されます。
列が見つかりません: 1054 不明な列 'ActivityIngredients.ingredient_id' in 'where clause'
しかし、hasMany の代わりに hasOne を記述した場合、エラーは返されず、完璧です。しかし、私の関係は hasMany です..なぜですか?
データを取得するには、UserController にクエリを作成します。このモードでは、User は hasMany の Activity に関連付けられ、Activity は ActivityIngredients に関連付けられます。
public $hasOne = array(
'ActivityIngredients' => array(
'className' => 'ActivityIngredients',
'conditions' => '',
'dependent' => true,
'foreignKey' => 'activity_id'
)
データを取得するために、現在このクエリを使用していますが、すべてのクエリを変更する必要があると思います
$this->User->Activity->recursive = 2;
$activity = $this->User->Activity->findAllByUser_id($id);
$this->set('activity', $activity);