0

我是 CakePHP 的新手,刚刚学习它,我已经通过 save() 插入了数据,但我不知道如何更新或删除 MySQL 数据库中的数据。我发现了很多事情,这些对我来说是无法理解的......谁能帮助我......

我的订单控制器是-

    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()
     { 

///// 这是我在视图中的页面名称,在这里做什么..????

       }

///我的模型订单是--

      App::uses('Model', 'Model');

     class Order extends AppModel 
      {
   var  $name = 'Order';

       }

// 我不知道在视图中的edit.php中做什么...

提前致谢

4

1 回答 1

0

只需在您的视图文件夹中创建一个 File edit.ctp

和你的 add.ctp 一样

然后在您的控制器中创建一个编辑功能,例如

公共函数编辑($id = null){}

现在,从你的 index.ctp 给出这个 edit.ctp 的链接,比如

 eg.
 <?php echo $this->Html->link(__('Edit'), array('action' => 'edit', $order['Order']['Id'])); ?>

将解决您的目的。

嗨试试这个控制器功能

公共功能编辑($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);
         }

}   
于 2013-09-30T08:43:58.453 回答