0

私はcakephp2.1.3を使用していて、テーブルIngredientsとテーブルIngredientAliasesがあります。IngredientAliasを選択すると、Ingredientのページインデックスに表示されます。IngredientAliasとIngredientsのいくつかのファイルが表示されます。コントローラに2つのクエリを実行するか、components_idを設定するだけでidとcakephpが自動的にデータを取得します。今、コントローラーで2つのクエリを実行しますが、それが最善の方法かどうかわかりません

$this->set('ingredient', $this->Ingredient->read());            $this->set('ingredient_alias',$this->Ingredient->IngredientAlias->read()); 

これが私のテーブルです:

class Ingredient extends AppModel {
    public $name = 'Ingredient';
    public $useTable = 'ingredients';
    public $belongsTo = 'User';

    public $hasMany = array (
        'IngredientAlias' => array (
            'className'     => 'IngredientAlias',
            'foreignKey'    => 'ingredient_id'
        )
    );
}

class IngredientAlias extends AppModel {
    public $name = 'IngredientAlias';
    public $useTable = 'ingredient_aliases';
    public $belongsTo = array(
        'Ingredient' => array(
            'className'    => 'Ingredient',
            'foreignKey'   => 'ingredient_id'
        )
    );
}

そして、これが私のIngredientsControllerです(引数にエイリアスを置くようにルートを変更しましたが、機能しています)

public function edit ($alias) {
        $ing = $this->Ingredient->IngredientAlias->find('first', array(
                    'conditions' => array('IngredientAlias.alias' => $alias)));
        $this->Ingredient->IngredientAlias->id= $ing['IngredientAlias']['id'];
        $this->Ingredient->IngredientAlias->set('ingredient_id',$ing['IngredientAlias']['ingredient_id']);
        $this->Ingredient->id= $ing['IngredientAlias']['ingredient_id'];
        if (!$this->Ingredient->IngredientAlias->exists()) {
                throw new NotFoundException ('Nessuna corrispondenza trovata per questo ingrediente');
            }
            if (!$alias) {
                $this->set('flash_element','error');
                $this->Session->setFlash ('Ingrediente non valido');
            }
            $this->Ingredient->recursive = 2;
            $this->set('ingredient', $this->Ingredient->read());
            $this->set('ingredient_alias',$this->Ingredient->IngredientAlias->read());
    }
4

1 に答える 1

0

あなたが説明した関係に基づいて、そしてあなたがあなたが呼ぶならばあなたがingredient_idあなたのテーブルに鍵を持っているかどうか:ingredient_aliases

$this->Ingredient->find('first', array('conditions' => array('Ingredient.id' => $id)));

$ idで指定されたIngredientレコードと、それに関連するすべてのIngredientAliasレコードを、次のような配列構造で取得します。

Array =>
    [Ingredient] => Array
                        [id] => 3
                        [other_data_from_DB] =>
                        .....
    [IngredientAlias] => Array => 
                         [0] => Array =>
                                [id] => 1
                                [ingredient_id] => 3
                                [other_data_from_DB] =>
                                ....
                         [1] => Array =>
                                [id] => 2
                                [ingredient_id] => 3
                                [other_data_from_DB] =>
                                ....
                         [2] => Array =>
                                [id] => 3
                                [ingredient_id] => 3
                                [other_data_from_DB] =>
                                ....

IngredientAlias配列サブ構造は、hasMany関係のためにインデックスが付けられます。関連するIngredientAliasレコードが表示されない場合は、モデルの再帰プロパティを確認してください

debug($this->Ingredient->recursive);

-1に等しい場合、成分モデルデータのみがフェッチされます。私が提供したリンクで、他の再帰値が何をするかを確認してください。

逆に行きたい場合は、IngredientAliasモデルを使用する必要があります。

$this->IngredientAlias->find('first', array(CONDITIONS));

CONDITIONS IngredientAliasレコードで指定されたものに、接続されたIngredientレコードを提供します。関係があるため、材料レコードは1つだけになりますIngredientAlias belongsTo Ingredient

于 2012-06-20T19:12:46.327 に答える