嘿,试图从一个控制器(模板)获取一个 id 到另一个(字段)。试图让模板的 id 自动出现在表单的添加(查看)输入字段中。
FieldsController 与我们尝试过的段:
$template = $this->Template->find('first');
$current_template = $this->Template->request->data($template['Template']['id']);
添加字段视图中的输入字段:
echo $this->Form->input('templates_id', array('label'=>'Template ID: ', 'type' => 'text', 'default' => $current_template['templates_id']));
那么我们能做些什么来完成这个任务呢?我以为我尝试过的方法会奏效,但没有提出“在非对象上调用成员函数 find()”。
“templates_id”是“字段”表中的外键,它链接到“模板”表中的“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.');
}
}
}
来自视图的表单称为添加:
<?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();
?>