I am new to CakePHP & just learning it, I have already inserted data by save() but I don't have any idea to how to update or delete data from MySQL database. I have find so many things on that these are not understandable for me.. can anyone help me out...
My ordersController is-
class OrdersController extends AppController
{
var $name = 'Orders';
var $helpers = array('Html', 'Form');
//var $ords = array('id', 'order_no' );
public function order()
{
$this->set('orders', $this->Order->find('all'));
}
public function add_order()
{
if (!empty($this->data)) {
if ($this->Order->save($this->data)) {
$this->redirect('/');
}
}
}
public function edit()
{
///// this is my page name in view, what to do here..????
}
///my model Order is --
App::uses('Model', 'Model');
class Order extends AppModel
{
var $name = 'Order';
}
// I have no idea what to do in edit.php in view ...
thanks in advance
Just create an File edit.ctp in your view folder
same as your add.ctp
then create an edit function in your controller like
public function edit($id = null){ }
Now, From your index.ctp give link to this edit.ctp like
eg.
<?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $order['Order']['Id'])); ?>
will solve your purpose.
hi Try this controller function
public function edit($id = null) {
if (!$this->Order->exists()) {
throw new NotFoundException(__('Invalid order'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Order->save($this->request->data)) {
$this->Session->setFlash(__('Your order has been Modified Sucessfully'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('Your order not been Modified Sucessfully.'));
}
}
else {
$this->request->data = $this->Order->read(null, $id);
}
}