Я использую cakephp 2.1.3, и у меня есть таблица Ingredients и таблица IngredientAliases. Я хочу, чтобы это было в моем индексе страницы в Ingredient, когда я выбираю IngredientAlias. Я хочу видеть IngredientAlias и некоторые поля Ingredients. Но я не знаю, если Мне нужно сделать два запроса к моему контроллеру или только установить 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 (я изменил маршруты, чтобы добавить псевдоним в свои аргументы, и он работает)
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());
}