1

FieldsControllerのCakePHPで変数を宣言しようとしています。この変数は、テンプレートテーブルのテンプレートIDを表示しますが、ビューは、コントローラーで変数を拡張したにもかかわらず、変数が未定義であると言っています。Temaplatesには多くのフィールドがあり、フィールドはテンプレートに属しています。

フィールドコントローラは次のとおりです。

<?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 に答える