CakePHP: Fields not populating in Edit screen

情到浓时终转凉″ 提交于 2020-01-14 06:18:09

问题


Simple question from an absolute n00b.

I'm trying to create an edit content screen based on the code given in the Blog Tutorial on the Cake site.

Here's my edit function in the controller (it's named Contents):

function edit( $id = null ) {
    $this->Content->id = $id;
    Debugger::dump( $this->Content->id );
    if( empty( $this->data ) ) {
        $this->data = $this->Content->read();
        Debugger::dump( $this->Content );
    }
    else {
        if( $this->Content->save( $this->data ) ) {
        $this->Session->setFlash( 'Content has been updated.' );
        $this->redirect( array('action' => 'index') );
        }
    }
}

And this is edit.ctp:

echo $form->create( 'Content', array('action' => 'edit') );
echo $form->input( 'id', array('type' => 'hidden') );
echo $form->input( 'title' );
echo $form->input( 'nicename' );
echo $form->end( 'Save' );

However, when I visit the Edit screen, the input fields are NOT GETTING POPULATED with the requested data.

I'm using HTML Helper to generate the links and my edit link takes the form of:

http://localhost/contents/edit/id:4c8efaa0-02fc-46bf-ac65-06a07614f57d

As you can see, I've introduced two Debugger dumps in the controller's edit function... those are showing that $this->Content->id remains NULL when I visit this url.

BUT, if I modify the URL to this form (notice that I've used '=' instead of a ':'):

http://localhost/contents/edit/id=4c8efaa0-02fc-46bf-ac65-06a07614f57d

Debugger reports $this->Content->id to have the correct value...

However, my fields still don't get populated.

The index function is working fine though...

function index() {
    $this->set( 'contents', $this->Content->find( 'all' ) );
}

The above function is listing the contents correctly!

Where am I going wrong? Am I missing out on some essential bit of code in the View ? Any help will be much appreciated.

Thanks, m^e


回答1:


The problem is here:

http://localhost/contents/edit/id:4c8efaa0-02fc-46bf-ac65-06a07614f57d

Basically your url should look like:

http://localhost/contents/edit/4c8efaa0-02fc-46bf-ac65-06a07614f57d

As you may notice the id: is missing from the correct url. The reason: If you pass the id: this is named parameter and it's not assigned to $id in the function.

Your link for the edit form should look like this:

$this->Html->link('Edit', array('controller'=>contents', 'action'=>'edit', $id_variable));


来源:https://stackoverflow.com/questions/3890464/cakephp-fields-not-populating-in-edit-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!