嘿傢伙們試圖在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();
?>
沒有在下拉框中仍然沒有拉動模板ID和名稱。當你點擊submit時,它會出現一個有關create()的致命錯誤:致命錯誤:調用非對象的成員函數create() – user1402677 2012-08-06 05:00:30
SELECT'Template'.''',Template.id,Template.name FROM'pra'.'templates' AS'Template' WHERE 1 = 1 多數民衆贊成什麼SQL語句正在尋找 – user1402677 2012-08-06 05:01:11
其工作,但形式是從數據庫返回每個模板ID,但我們只想要一個模板ID,其中用戶正在添加字段。我們如何做到這一點? – user1402677 2012-08-06 06:51:59