1

嘿家伙试图在字段控制器中的 CakePHP 中声明一个变量。此变量将显示模板表中的模板 id,但视图显示该变量未定义,即使我们在控制器中声明了它。模板有很多字段,字段属于模板。

这是字段控制器:

<?php
class FieldsController extends AppController{
public $uses = array('Template');


 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.");
    $templates = $this->Template->find('list');
    //$current_template = $this->request->data['Field']['template_id'];

    // right way to do it, but Template is undefined, and says undefined var
    //$template = $this->request->data['Field']['template_id'];

    // makes sense with the find, no errors, but still doesnt print in form, says undefined var
    //$current_template = $this->request->data($template['Field']['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('template_id',array('label'=>'Template ID: ', 'type' => 'select', 'options' => $templates));
    echo $this->Form->input('template_id',array('label'=>'Template ID: ', 'type' => 'text', 'default'=> $templates));
    //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();


?>
4

2 回答 2

1

$this->set('templates', $templates);为控制器中的模板执行 find() 后,您丢失了。

于 2012-08-06T02:43:44.587 回答
1

您应该尝试以下代码:

<?php
class FieldsController extends AppController{
  public $uses = array('Template', 'Field');
  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.");
$templates = $this->Template->find('list', array('fields' => array('Template.id, Template.template_name' );
$this->set('templates', $templates);

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

// right way to do it, but Template is undefined, and says undefined var
//comment: You should check the request data with in if condition 
//$template = $this->request->data['Field']['template_id'];

// makes sense with the find, no errors, but still doesnt print in form, says undefined var
//$current_template = $this->request->data($template['Field']['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->input('name', array('label'=>'Name: '));
echo $this->Form->input('description', array('label'=>'Description: '));
echo $this->Form->input('template_id',array('label'=>'Template ID: ', 'options' => $templates));
//echo $this->Form->input('template_id',array('label'=>'Template ID: ', 'type' => 'text', 'default'=> $templates));
//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();

 ?>

请检查并验证它是否适合您。

于 2012-08-06T04:31:29.227 回答