CakePHP:未在编辑屏幕中填充的字段 [英] CakePHP: Fields not populating in Edit screen

查看:154
本文介绍了CakePHP:未在编辑屏幕中填充的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自绝对n00b的简单问题。



我试图根据Cake网站上的博客教程中给出的代码创建一个编辑内容屏幕。



这是控制器中的编辑功能(名为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'));
}
}
}

edit.ctp :

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

但是,当我访问编辑屏幕时,输入字段未获得请求的数据的POPULATED。



我使用HTML Helper生成链接,我的编辑链接采用以下形式:

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

正如你所看到的,我在控制器的编辑功能中引入了两个Debugger转储。这些显示了 $ this-> Content-> id 我访问这个网址。



但是,如果我将网址修改为此表单(请注意,我使用'='而不是': '):

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

调试器报告 $ this-> Content-> id 正确的值...



$ b

但是,我的字段仍未填充。 b

索引函数虽然工作正常,但...

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

上述函数正确列出了内容!



我在哪里出错?我错过了一些重要的位在视图中的代码?



感谢您,
m ^ e

解决方案

问题在这里:

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

基本上您的网址应如下所示:

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

由于您可能注意到正确的网址缺少id:原因:如果你传递id:这是命名参数,并且它没有分配给函数中的$ id。



编辑表单的链接应该如下所示:

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


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

解决方案

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));

这篇关于CakePHP:未在编辑屏幕中填充的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆