cakephp关系属于

时间:2012-06-19 21:36:45

标签: cakephp-2.1

我正在使用cakephp 2.1.3,我有一张桌子配料和一张表IngredientAliases 当我选择一种IngredientAlias时,我想在我的页面索引中选择一种成分,我想看看IngredientAlias和一些成分的成分 但我不知道我是否必须对我的控制器进行两次查询,或者只是设置ingredients_id,而id和cakephp会自动检索我的数据。现在我在我的控制器中进行了两次查询,但我不知道这是否是最好的方法

$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(我已经将putt别名的路由更改为我的参数并且它正在工作)

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());
    }

1 个答案:

答案 0 :(得分:0)

根据您描述的关系以及如果您致电ingredient_id表格中有ingredient_aliases密钥:

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

您将获得$ id指定的成分记录,其中包含与所有相关的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] =>
                                ....

由于hasMany关系,IngredientAlias数组子结构已编入索引。 如果您没有看到相关的IngredientAlias记录,请检查your Model's recursive property

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

如果等于-1,则仅获取成分模型数据。查看我提供的链接中其他递归值的作用。

如果你想走另一条路,你应该通过IngredientAlias模型:

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

将在CONDITIONS IngredientAlias记录中提供其连接的成分记录。由于IngredientAlias belongsTo Ingredient关系,成分记录只有一个。