【问题标题】:Auto-fill an id from another controller in a CakePHP form在 CakePHP 表单中自动填充另一个控制器的 id
【发布时间】:2012-08-03 02:54:07
【问题描述】:

嘿,试图从一个控制器(模板)获取一个 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.'); 
        } 
    } 
  }

从视图中调用 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();


?>

【问题讨论】:

    标签: mysql forms cakephp views controllers


    【解决方案1】:
    $this->Template->request->data($template['Template']['id']);
    

    错了。

    看看MVC模式:

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

    现在看看你的代码:

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

    是一个控制器。

    ->Template
    

    调用关联的模型。

    request->data()
    

    使用视图中的数据调用此模型中的函数。

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

    应该是:

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

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

    1. 我认为应该是:

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

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

    此变量未设置。

    这样写:

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

    【讨论】:

    • $this->loadModel('Template'); $current_template = $this->request->data['Template']['id'];谢谢,上面有。但是现在说:'未定义的索引模板'并且在表格上说'未定义的变量 current_template',当我在控制器中定义它时,如上所示。那么我该如何解决这些问题呢?
    • 看来你犯了几个错误。能不能把表单的代码放在视图里,把方法的代码放在控制器里?
    • 添加的代码,如您所见,尝试了几种不同的方法。
    • $current_template = $this->request->data['Field']['template_id'];这会引发错误:未定义的索引:字段。把这个: $this->set(compact('current_template'));在控制器中,仍然说未定义的变量 current_template。
    • 好的。然后使用 firebug 查看从视图发送的数据。这应该可以帮助您解决第一个问题。
    【解决方案2】:

    要从另一个模型中获取数据,您必须先加载它:

    $this->loadModel('Template');
    $this->Template->find('first');
    

    【讨论】:

    • 刚刚更新了上面的代码,并说“在非对象上调用成员函数 data()。”我们使用 $this->data 是否正确?
    • 我认为这是错误的 '$this->Template->request->data($template['Template']['id'])' 你可以使用 pr($template );死;然后根据结构分配这应该是这样的: $current_template = $template['Template']['id'];
    • 不,那也行不通。玩过调试,看起来 find('first') 只抓取第一个(DUH!)所以实际上并没有抓取正确的,我们也无法将其发布到输入框。还有其他想法吗?
    猜你喜欢
    • 1970-01-01
    • 2013-08-11
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-24
    相关资源
    最近更新 更多