1

私はいくつかの関係を持つデータベースを持っています。この例には 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);
4

2 に答える 2

0

モデルクラスを定義するときに最初に単数形の単語を使用する必要があります。これは、モデル名がActivityIngredientの代わりになっている必要があることを意味しActivityIngredientsます。

ただしactivity_ingredientsテーブル内の場合、ingredient_ididがテーブルに属する外部キーの場合ingredient_aliases。次に、という名前を付ける必要がありますingredient_alias_id。同様に、これをテーブルで行う場合ingredient_aliases、つまり外部キー名はになりますactivity_ingredient_idそうすれば全く問題ありません。

しかし、私はあなたの問題の解決策を提供しています。確認してください。

次に、コードは次のようになります。

class ActivityIngredient extends AppModel{
    public $name = 'ActivityIngredient'; 
    public $useTable = 'activity_ingredients'; // if you use singular term then no need to define $useTable

    public $hasMany = array (
    'IngredientAlias' => array (
        'className'     => 'IngredientAlias',
        'foreignKey'   => false,
        'finderQuery' => 'select * from ingredient_aliases as `IngredientAlias`
                          where `IngredientAlias`.`ingredient_id` = {$__cakeID__$}'
                               )
    );
}   

class IngredientAlias extends AppModel {

    public $name = 'IngredientAlias';
    public $useTable = 'ingredient_aliases';
    public $belongsTo = array(
                      'ActivityIngredient' => array(
                                   'className'    => 'ActivityIngredient',
                                   'foreignKey'   => 'ingredient_id' 
                                                   )
                             );  
} 
于 2012-07-25T04:12:02.537 に答える
0

あなたが使用した

条件' => array('ActivityIngredients.ingredient_id = IngredientAlias.ingredient_id')

そのはず

条件' => array('ActivityIngredients.ingredient_id => IngredientAlias.ingredient_id')

以下のコードを使用

            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
            )

        );  
    }
    ?>
于 2012-07-25T04:34:29.367 に答える