我在创建cakephp选项值方面遇到了麻烦。
我在add.ctp中的代码
<h1>New Book</h1>
<?php
echo $this->Form->create('Book',array('enctype'=>'multipart/form-data'));
echo $this->Form->input('title');
echo $this->Form->input('author');
echo $this->Form->input('summary',array('rows' => '3'));
echo $this->Form->input('category',array('type'=>'select','options'=>$category));
echo $this->Form->input('price');
echo $this->Form->input('image',array('type'=>'file'));
echo $this->Form->end('Save');
?>
并且控制器添加功能
public function add(){
$this->loadModel('Category');
$category =$this->Category->find('all', array('fields'=>'name'));
debug($category);
$this->set('category',$category);
if($this->request->is('post')){
$this->Book->create();
$filePath="./img/images/".$this->request->data['Book']['image']['name'];
$filename=$this->request->data['Book']['image']['tmp_name'];
if(move_uploaded_file($filename, $filePath)){
$this->request->data['Book']['cover']=$this->request->data['Book']['image']['name'];
}
if ($this->Book->save($this->request->data)) {
$this->Flash->success(__('Your book has been saved.'));
}
return $this->redirect(array('controller' =>'Books','action' => 'index'));
}
}
我只希望我的类别名称中的列表框不能与我的表单中的foreach一起使用。为什么呢?
答案 0 :(得分:0)
更改
$category =$this->Category->find('all', array('fields'=>'name'));
要
$category =$this->Category->find('list');