Возникли проблемы с поведением отправки - PullRequest
0 голосов
/ 11 декабря 2011

У меня есть DishesController с тремя функциями, dish_list, viewdish и edit, я могу перейти на первые два штрафа ... Я пытаюсь обновить запись, когда нажата кнопка в окне просмотра ( Я отправляю их для редактирования). Ожидается ли $id параметр?

Или, может быть, я могу обновить в той же функции viewdish? Поэтому каждый раз, когда я нажимаю на эту кнопку, она отправляет меня в систему, независимо от того, что и что происходит.

Мое разрешение авторизации установлено в контроллере приложения, чтобы разрешить ('*'). Я не понимаю, что происходит ..

Вот мой контроллер приложения:

<?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());
        }
    }
 ?>

1 Ответ

0 голосов
/ 11 декабря 2011

Вы неправильно создаете файлы для просмотра.Не вставляйте такие печатные выражения, просто оставьте их как HTML.

Что касается метода редактирования, измените сигнатуру метода на

function edit($id = null){ 
   //code
}

При этом будет использоваться поле $ id, если оно есть, или назначено значение по умолчанию.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...