Reputation: 1
Four models Restaurant, RestaurantAddresses, RestaurantCuisines and Cuisine.
I am able to display all the restaurants, thsir addresses and they types of thier cuisines through the page View/Restaurants/index.ctp
However, I am unable to code the view and edit actions, following are the codes I am using for both actions :
Edit action (code):
Edit works but it adds two much entries in the Restaurant_Addresses and Cucines table.
public function edit($id = null) { $this->Restaurant->id = $id;
$this->loadModel('Cusine');
$model_cusine_edit_data = $this->Cusine->find('list');
$this->set('CusineEditList', $model_cusine_edit_data);
if (!$this->Restaurant->exists()) {
throw new NotFoundException('Invalid Restaurant');
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->Restaurant->save($this->request->data);
$this->Session->setFlash('The restaurant has been saved');
$this->redirect(array('action' => 'index'));
} else {
$this->request->data = $this->Restaurant->read();
}
}
View action (code):
the following code only displays restaurant. name information, unable to see street and postal code.
public function view($id = null) { $this->Restaurant->id = $id;
if (!$this->Restaurant->exists()) {
throw new NotFoundException('Invalid user');
}
if (!$id) {
$this->Session->setFlash('Invalid user');
$this->redirect(array('action' => 'index'));
}
$this->set('restaurant', $this->Restaurant->Read());
}
Upvotes: 0
Views: 435
Reputation: 98
Here is how I would code your methods. I think your main issue in regards to the data not showing up, is the read method only grabs the first models data and does not get associated data... So I would do a Model::find(), instead of a Model::read() with the contain parameter to get the associated data as well.
Don't forget to add 'Containable' to your models $actsAs parameter.
public function edit($id=null){
$this->Restaurant->id = $id;
if (!$this->Restaurant->exists()) {
throw new NotFoundException('Invalid Restaurant');
}
if(!empty($this->request->data)){
if($this->Restaurant->save($this->request->data)){
$this->Session->setFlash('The restaurant has been saved');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to save the restaurant');
}
}
$model_cusine_edit_data = $this->Restaurant->RestaurantCusine->Cusine->find('list');
$this->set('CusineEditList', $model_cusine_edit_data);
$this->request->data = $this->Restaurant->find('first', array('conditions'=>array('Restaurant.id'=>$id), 'contain'=>array('RestaurantAddresses','RestaurantCusine')));
}
public function view($id=null){
$this->Restaurant->id = $id;
if (!$this->Restaurant->exists()) {
throw new NotFoundException('Invalid user');
}
if (!$id) {
$this->Session->setFlash('Invalid user');
$this->redirect(array('action' => 'index'));
}
$this->set('restaurant', $this->Restaurant->find('first', array('conditions'=>array('Restaurant.id'=>$id), 'contain'=>array('RestaurantAddresses','RestaurantCusine'))));
}
Upvotes: 1
Reputation: 6767
For the View you need to do a find('first', array('conditions' => array('Restaurant.id' => $id), 'recursive' => 2));
or, a better way, use the ContainableBehavior
which lets you choose exactly which models to fetch. Search the book for the usage and example.
As for the edit, provide a link to the post data looks like.
Upvotes: 0