gpt4 book ai didi

php - 在 CakePHP 中查询更新数据

转载 作者:行者123 更新时间:2023-11-30 01:15:34 26 4
gpt4 key购买 nike

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

我的ordersController是-

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

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

       }

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

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

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

}

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

提前致谢

最佳答案

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

与您的add.ctp相同

然后在 Controller 中创建编辑功能 喜欢

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

现在,从您的index.ctp中提供指向此edit.ctp的链接 就像

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

将解决您的目的。

嗨试试这个 Controller 功能

公共(public)函数编辑($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);
}

}

关于php - 在 CakePHP 中查询更新数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19088120/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com