Question Overview:
I want to be able to edit an item (a restaurant in this case) and allow the user to select multiple options (cuisines) that use a table of connections to access a list of the options/cuisines.
Question Details:
I have 3 tables:
restaurants: id, name, address, phone...etc
cuisines: id, name, url_name
cuisine_connections: id, restaurant_id, cuisine_id
As you can imagine, the cuisine_connections table is a list of connections between the cuisines, and the restaurants. (If anyone has a better solution for this I’m all ears).
model/restaurant.php:
var $hasMany = array(
'CuisineConnection' => array(
'className' => 'CuisineConnection',
'foreignKey' => 'restaurant_id',
'dependent' => false
)
);
model/cuisine.php
var $hasMany = array(
'CuisineConnection' => array(
'className' => 'CuisineConnection',
'foreignKey' => 'cuisine_id'
)
);
model/cuisine_connection.php
var $belongsTo = array(
'Cuisine' => array(
'className' => 'Cuisine',
'foreignKey' => 'restaurant_id',
'dependent' => false
),
'Restaurant' => array(
'className' => 'Restaurant',
'foreignKey' => 'restaurant_id'
)
);
I’ve tried this so far:
controllers/restaurants_controller.php:
$this->set('cuisines', $this->Restaurant->CuisineConnection->Cuisine->find('list'));
admin_add.ctp:
echo $this->Form->input('cuisine_id', array('multiple'=>'checkbox'));
You could read this tutorial for undestand clearly about implementing many-to-many association in CakePhp. Beside that, the line code:
has problems. I think that, depend on your description, you should list all the Cuisine for choosing with coresponding restaurant. The code should be replaced by: