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->create( 'Content', array('action' => 'edit') );
echo $form->input( 'id', array('type' => 'hidden') );
echo $form->input( 'title' );
echo $form->input( 'nicename' );
echo $form->end( 'Save' );
但是,当我访问“编辑”屏幕时,输入字段并未填充请求的数据。
我使用 HTML Helper 生成链接,我的编辑链接采用以下形式:
http://localhost/contents/edit/id:4c8efaa0-02fc-46bf-ac65-06a07614f57d
如您所见,我在控制器的编辑功能中引入了两个调试器转储...这些显示 $this->当我访问此网址时,;Content->id 保持为 NULL。
但是,如果我修改此表单的 URL(请注意,我使用了 '=' 而不是 ':'):
http://localhost/contents/edit/id=4c8efaa0-02fc-46bf-ac65-06a07614f57d
调试器报告 $this- >Content->id 以获得正确的值...
但是,我的字段仍然没有填充。
索引函数工作正常...
function index() {
$this->set( 'contents', $this->Content->find( 'all' ) );
}
上面的函数列出的内容正确!
我哪里错了?我是否错过了 View 中一些重要的代码? 任何帮助将不胜感激。
谢谢, 米^e
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题就在这里:
基本上你的 url 应该看起来像:
正如你可能注意到的 id: 从正确的 url 中丢失。原因:如果您传递 id:这是命名参数,并且它不会分配给函数中的 $id 。
编辑表单的链接应如下所示:
The problem is here:
Basically your url should look like:
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: