2012-08-06 111 views
1

嘿傢伙們試圖在Fields Controller中的CakePHP中聲明一個變量。此變量將顯示模板表中的模板ID,但視圖說變量是未定義的,即使我們在控制器中刪除了它。 Temaplates具有許多屬於模板的字段和字段。CakePHP聲明變量

這裏是場控制器:

<?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.'); 
     } 
    } 
    } 

} 

這裏是我們的add視圖這增加字段:做對模板的find()方法在控制器後

<?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(); 


?> 

回答

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(); 

?> 

請檢查並確認其是否爲你工作或沒有。

+0

沒有在下拉框中仍然沒有拉動模板ID和名稱。當你點擊submit時,它會出現一個有關create()的致命錯誤:致命錯誤:調用非對象的成員函數create() – user1402677 2012-08-06 05:00:30

+0

SELECT'Template'.''',Template.id,Template.name FROM'pra'.'templates' AS'Template' WHERE 1 = 1 多數民衆贊成什麼SQL語句正在尋找 – user1402677 2012-08-06 05:01:11

+0

其工作,但形式是從數據庫返回每個模板ID,但我們只想要一個模板ID,其中用戶正在添加字段。我們如何做到這一點? – user1402677 2012-08-06 06:51:59

1

你缺少$this->set('templates', $templates);

+0

它實際上在字段上打印$模板,我們實際上想要模板ID,變量抓取什麼。有任何想法嗎? – user1402677 2012-08-06 03:02:41

+0

當前的代碼不會從數據庫加載信息。發現條件存在問題。 – user1393064 2012-08-06 03:06:22