从CakePHP表单中的另一个控制器自动填充id

时间:2012-08-03 02:54:07

标签: mysql forms cakephp views controllers

嘿试图从一个Controller(模板)到另一个(Field)获取id。试图让模板的id自动出现在表单的add(view)输入字段中。

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'是'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 with add function:

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:

的视图中的表单
<?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();


?>

2 个答案:

答案 0 :(得分:1)

$this->Template->request->data($template['Template']['id']);

错了。

看看MVC模式: enter image description here

调度员将数据发送到控制器。

现在看看你的代码:

 $this->Template->request->data($template['Template']['id']);


$this

是控制器。

->Template

调用相关模型。

request->data()

使用View中的数据从此模型调用函数。

简而言之:您将视图中的数据传递给模型,从而打破了MVC模式。

应该是:

 $this->request->data['Template']['id'];

编辑: 我认为有两个问题:

  1. 我认为应该是:

    $ current_template = $ this-&gt; request-&gt; data ['Field'] ['template_id'];

  2. $current_template = $this->request->data($template['Template']['id']);

  3. 未设置此变量。

    这样的东西:

    $this->set(compact('current_template'));
    

答案 1 :(得分:0)

要从其他模型获取数据,您必须先加载它:

$this->loadModel('Template');
$this->Template->find('first');
相关问题