开发者

Problem with `id` field in URL when Editing in CakePHP

I am facing issue about id field in the regular http://localhost/bekzcart/admin/users/edit/6 structure.

I have 6 fields for user, all are validated as 'non empty' through model. While editing one user I put one hidden field.

On submitting the form, naturally it gives me error (coming from model) saying 'non empty'. Without entering anything in that field, I hit submit again, now I face the issue.

What happens this time is that 'id' field from the url is n开发者_运维百科ow gone, http://localhost/bekzcart/admin/users/edit) and there is a new entry in database (ideally it should update though).

What might be the error?

My Users Controller:

class UsersController extends AppController {
    var $name = 'Users';

    function admin_edit($id) {
        $this->User->id = $id;
        $userLevels = $this->User->Level->find('list', array('fields' => array('LEVEL_ID', 'lEVEL_NAME')));
        $this->set('levels', $userLevels);

        if (empty($this->data)) {
            $this->data = $this->User->read();
        } else {
            $this->User->set($this->data);

            if ($this->User->validates(array('fieldList' => array('USER_LOGIN', 'USER_NAME', 'USER_EMAIL', 'USER_ADDRESS', 'USER_PHONE')))) { 
                $this->data['User']['USER_MODIFIED'] = DboSource::expression('NOW()');

                if ($this->User->save($this->data)) { 
                    $this->Session->setFlash(__('Edit User Success.', true));
                } else {
                    $this->Session->setFlash(__('Something wrong with the query', true));    
                }
            }    
        }                
    }
}

User Model:

class User extends AppModel {
    var $name = 'User';
    var $primaryKey = 'USER_ID';

    // Validation in here
    // Association in here
}

My related view: admin_edit.ctp

$this->Form->input('id', array('type' => 'hidden')) // The Hidden Id Not Work

Many-many thanks for advance,

regrad

Brian ...


what version of cake are you using? update to the latest one, echo $this->Form->input('id'); will automatically be hidden.

And post the full code that generates the form, the output form should be something like this:

<form id="AdminUserEditForm" accept-charset="utf-8" action="/admin/users/edit/1" method="post">

One more suggestion: add 'created' and 'modified' fields to your users table (type datetime). Cake will keep track of these fields for you.


The field id must be replaced by the primary key used by the related table, in this case is USER_ID

$this->Form->input('USER_ID', array('type' => 'hidden'))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜