0

I have two tables: Ingredients and Customers. The relationship between them is that Customers hasMany Ingredients. By default when doing the cakebake using the console, the only way to change them is by assigning an ingredient to the customer in the Ingredients page. However, I want to have in Customers page a checkbox list of Ingredients that can be assigned. Is it possible to do this? If yes, how?

edit:

What I have done until now is that I add this code to my add.ctp:

echo $this->Form->input('Ingredient',
        array('label'=>'',
        'type'=>'select',
        'multiple'=>'checkbox',
        'options'=>$ingredients));

However, it gives me "Undefined variable: ingredients" error when I tried to open the add view.

4

2 回答 2

0

您想要并且需要 HABTM 关系。不同的客户可以访问和使用相同的成分。查看此处的文档,您的文档与使用相同标签的不同帖子非常相似。

于 2013-10-16T15:36:38.503 回答
0

如果您收到此错误:

“未定义变量:成分”

听起来你还没有在控制器中声明这个变量,并设置它以便视图可以使用它。在不知道您的代码的情况下,您可能需要做这样的事情(请注意,我猜测您的应用程序结构是什么样的,并且尚未测试此代码)。

客户控制器.php

// The controller action for your view
public function view() {

    // Get the ID and name of all your ingredients
    $ingredients = $this->Ingredient->find('all', array(
        'fields' => array('id', 'name'),
        'order' => 'name',
        'recursive' => -1
    ));

    // We will use this array to store all the HTML select options
    $ingredientOptions = array();

    // Loop through all the ingredients and add them to the select
    // options in a format that is suitable for CakePHP to use in
    // the view to build your HTML select menu.
    foreach ($ingredients as $i) {
        $ingredient = $i['Ingredient'];
        $ingredientOptions[$ingredient['id']] = $ingredient['name'];
    }

    // Make the variable available to the view
    $this->set('ingredients', $ingredientOptions);
}
于 2013-10-17T11:39:25.483 回答