mysql - 在 CakePHP 表单中自动填充来自另一个 Controller 的 ID

标签 mysql forms cakephp view controllers

嘿,试图从一个 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模式: enter image description here

调度程序将数据发送到 Controller 。

现在看看你的代码:

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


$this

是一个 Controller 。

->Template

调用关联模型。

request->data()

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

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

应该是:

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

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

  1. 我认为应该是:

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

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

此变量未设置。

放这样的东西:

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

关于mysql - 在 CakePHP 表单中自动填充来自另一个 Controller 的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11788597/

相关文章:

php - 为什么图像不显示只出现字符

java - 我在 Eclipse 中的 java 代码似乎可以运行,但 MySQL 没有将其插入表中,为什么?我们可以使用两个try catch吗?

php - 如何将删除按钮添加到将从 MySQL 表中删除行的 PHP 表单

javascript - HTML 将表单输入传递给 JavaScript 并重置表单字段

cakephp - 在用户模型 cakephp 中获取 $this->Auth->user ('id' )

mysql - IF on WHERE 子句

MySQL随机选择查询限制返回不同数量的结果(不需要)

javascript - Twitter Bootstrap 表单。如何捕获表单中输入的值?

javascript - tinymce 图像插入,第二次工作但不是第一次

mysql - CakePhp根据经纬度从数据库获取结果