嘿,试图从一个 Controller (模板)获取一个 ID 到另一个 Controller (字段)。试图让模板的 id 自动出现在表单的添加( View )输入字段中。
FieldsController 与我们已经尝试过的段:
$template = $this->Template->find('first');
$current_template = $this->Template->request->data($template['Template']['id']);
添加字段 View 中的输入字段:
echo $this->Form->input('templates_id', array('label'=>'Template ID: ', 'type' => 'text', 'default' => $current_template['templates_id']));
那么我们可以做些什么来完成这个任务呢?我以为我尝试过的方法会奏效,但没有奏效,结果是“在非对象上调用成员函数 find()”。
'templates_id' 是 'Field' 表中的外键,它链接到 'Template' 表中的 'id'。
编辑:
添加了 LoadModel(如下)并吐出错误:调用非对象上的成员函数 data()。
我们做的是否正确?或者应该是 $this->request->data($template['Template']['id']);
$this->loadModel('Template');
$template = $this->Template->find('first');
$current_template = $this->Template->request->data($template['Template']['id']);
编辑 2:
具有添加功能的FieldsController:
function add(){
$this->set('title_for_layout', 'Please Enter Your Invoice Headings');
$this->set('stylesheet_used', 'style');
$this->set('image_used', 'eBOXLogo.jpg');
$this->Session->setFlash("Please create your required fields.");
$this->loadModel('Template');
$template = $this->Template->find('first');
//$current_template = $template['Template']['id'];
// right way to do it, but Template is undefined, and says undefined var
//$current_template = $this->request->data['Template']['id'];
// makes sense with the find, no errors, but still doesnt print in form, says undefined var
$current_template = $this->request->data($template['Template']['id']);
if($this->request->is('post'))
{
$this->Field->create();
if ($this->Field->save($this->request->data))
{
if($this->request->data['submit'] == "type_1")
{
$this->Session->setFlash('The field has been saved');
$this->redirect( array('controller' => 'fields','action' => 'add'));
}
if($this->request->data['submit'] == "type_2")
{
$this->Session->setFlash('The template has been saved');
$this->redirect( array('controller' => 'templates','action' => 'index'));
}
}
else
{
$this->Session->setFlash('The field could not be saved. Please, try again.');
}
}
}
来自名为 add 的 View 的表单:
<?php
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->create('Field', array('action'=>'add'));
echo $this->Form->input('name', array('label'=>'Name: '));
echo $this->Form->input('description', array('label'=>'Description: '));
echo $this->Form->input('templates_id', array('label'=>'Template ID: ', 'type' => 'text', 'default' => $current_template['templates_id']));//this would be the conventional fk fieldname
echo $this->Form->button('Continue adding fields', array('name' => 'submit', 'value' => 'type_1'));
echo $this->Form->button('Finish adding fields', array('name' => 'submit', 'value' => 'type_2'));
echo $this->Form->end();
?>
最佳答案
$this->Template->request->data($template['Template']['id']);
错了。
看MVC模式:
调度程序将数据发送到 Controller 。
现在看看你的代码:
$this->Template->request->data($template['Template']['id']);
$this
是一个 Controller 。
->Template
调用关联模型。
request->data()
使用 View 中的数据调用此模型中的函数。
简而言之:您将数据从 View 传递到模型,这打破了 MVC 模式。
应该是:
$this->request->data['Template']['id'];
编辑: 我认为的两个问题:
我认为应该是:
$current_template = $this->request->data['Field']['template_id'];
$current_template = $this->request->data($template['Template']['id']);
此变量未设置。
放这样的东西:
$this->set(compact('current_template'));
关于mysql - 在 CakePHP 表单中自动填充来自另一个 Controller 的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11788597/