我有一个包含三个函数的DishesController,dish_list
,viewdish
和edit
,我可以导航到前两个很好......我试图在按钮时更新记录在viewdish中单击(我发送它们进行编辑)。是否期望$id
参数?
或者也许我可以在同一个viewdish
函数中更新?因此,每当我点击该按钮时,无论发生什么事都没有发生,它都会让我登录。
我的auth allow在应用程序控制器中设置为允许('*')。我不明白发生了什么......
这是我的应用程序控制器:
<?php
class AppController extends Controller {
var $helpers = array('Html', 'Form', 'Javascript', 'Session');
var $components = array('Auth', 'Session');
function beforeFilter() {
$this->set('userData', $this->Auth->user());
$this->Auth->allow('*');
$this->Auth->autoRedirect = false;
}
}
?>
这是我的DIshesController
<?php
class DishesController extends AppController {
var $uses = array("Dish");
function list_dish() {
$this->set('dishes', $this->Dish->find('all'));
$this->layout = 'master_layout';
}
function viewdish($id)
{
$this->set('dishes', $this->Dish->find('first', array(
'conditions' => array(
'Dish.id' => $id
)
)));
$this->layout = 'master_layout';
}
function edit($id){
if(!empty($this->data)) {
if($this->Dish->save($this->data)) {
$this->Session->setFlash("Dish Saved!");
$this->redirect(array('controller' => 'dishes', 'action' => 'list_dish'));
}
}
else
$this->data = $this->Dish->findById($id);
$this->layout = 'master_layout';
}
}
?>
这是我的观点:
<?php echo $this->Form->create('Dish', array('action' => 'edit')); ?>
<div><h3>Admin - Update Data</h3><p>Update the selected dish information</p></div>
<table id='results'>
<tr>
<th>ID</th>
<th>Image</th>
<th>Name</th>
<th>Price</th>
<th>Price Label</th>
<th>Description</th>
<th>Update</th>
</tr>
<?php
print("<tr>");
print("<td>");
echo '<p style="font-size:14px; color:blue; padding:0;">'.$dishes['Dish']['id'].'</p>';
echo '<input type="hidden" name="'.$dishes['Dish']['dish_name'].'" value="'.$dishes['Dish']['id'].'" id="DishDishId" />';
echo $this->Form->hidden('id', array('value'=> $dishes['Dish']['id']));
print("</td>");
print("<td>");
print("<img class='custom_rate' alt='' src='/mywebsite/img/".$dishes['Dish']['dish_image']."' />");
print("</td>");
print("<td width='100px'>");
echo $this->Form->input('dish_name', array('type' => 'text', 'label'=>'false', 'value' => $dishes['Dish']['dish_name'],'div' =>'false', 'label'=>''));
print("</td>");
print("<td>");
echo $this->Form->input('dish_price', array('type' => 'text', 'label'=>'false', 'value' => $dishes['Dish']['dish_price'],'div' =>'false', 'label'=>''));
print("</td>");
print("<td width='100px'>");
echo $this->Form->input('dish_price_label', array('type' => 'text', 'label'=>'false', 'value' => $dishes['Dish']['dish_price_label'], 'div' =>'', 'label'=>''));
print("</td>");
print("<td width='100px'>");
echo $this->Form->input('dish_disc', array('type' => 'textarea', 'escape' => false, 'value'=>$dishes['Dish']['dish_disc'] , 'rows'=>'2','cols'=>'15', 'div' =>'false', 'label'=>''));
print("</td>");
print("<td width='100px'>");
echo $this->Form->end('Submit');
print("</td>");
print("</tr>");
print("</table>");
?>
<?php
class UsersController extends AppController {
var $uses = array("User");
var $components = array('Auth', 'Session');
function index()
{
$this->set('users', $this->User->find('all'));
$this->layout = 'master_layout';
}
function add() {
if (!empty($this->data)) {
//Password is hashed already
//->data['User']['password'] = $this->Auth->password($this->data['User']['password']);
if ($this->User->save($this->data)) {
$this->Session->setFlash('Your were registered!.');
$this->redirect(array('action' => 'get_home'));
}
}
$this->layout = 'master_layout';
}
//IF THE DATABASE IS SET UP CORRECTLY, CAKEPHP AUTHENTICATES AUTOMATICALLY. NO
//LOGIC IS NEEDED FOR LOGIN, http://book.cakephp.org/view/1250/Authentication
function login() {
$this->layout = 'master_layout';
if ($this->data) {
if ($this->Auth->login($this->data)) {
// Retrieve user data
$results = $this->User->find(array('User.username' => $this->data['User']['username']));
$this->redirect(array('controller' => 'dish_categories', 'action' => 'get_home/7'));
}
}
$this->data['User']['password'] = '';
}
function logout() {
$this->redirect($this->Auth->logout());
}
}
?>
答案 0 :(得分:0)
您正在创建错误的视图文件。不要放入类似的打印语句,只需将其保留为HTML。
对于您的编辑方法,请将方法签名更改为
function edit($id = null){
//code
}
这将使用$ id字段(如果存在),或指定默认值。