我的控制器类
class BlistsController extends AppController{
public $components = array('session');
public function index(){
$data = $this->Blist->find('all');
$this->set('var_Blist', $data);
}
public function add(){
print_r($this->request->data);
if($this->request->is(array ('post', 'put'))){
$this->Blist->create();
if($this->Blist->save($this->request->data)){
$this->session->setFlash("book added successfully");
$this->reirect('index');
}
else{
$this->session->setFlash("Unable to add book");
}
}
}
}
我在家用电脑上使用过此代码。它的工作正常。但是同样的代码在我的工作场所不起作用。在wamp中是否有任何启用需求。
当我打印print_r($this->request->data)
时
它显示
Array ( [Blist] => Array ( [F_bookId] => [F_name] => sadfasdf [F_author] => asdfasdf ) )
字段“F_bookId”在数据库表中自动递增。 因此它不会显示在显示页面上。
App::uses('AppModel', 'Model');
/**
* Blist Model
*
*/
class Blist extends AppModel {
/**
* Primary key field
*
* @var string
*/
public $primaryKey = 'F_bookId';
/**
* Display field
*
* @var string
*/
public $displayField = 'F_name';
/**
* Validation rules
*
* @var array
*/
public $validate = array(
'F_bookId' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'F_name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'F_author' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
);
}
Add.ctp
<h1> Add Book Details </h1>
<?php
echo $this->Form->create('Blist');
echo $this->Form->input('F_bookId');
echo $this->Form->input('F_name');
echo $this->Form->input('F_author');
echo $this->Form->end('Add Book');
?>
最终代码正常工作..
public function add(){
$data = $this->request->data;//getting the values from the input fields
//get the last record
$lastRecordId = $this->Blist->find('first', array('order' =>array('F_bookId' => 'DESC')));
//Split the book id, to generate a new book id
$temp = str_split($lastRecordId['Blist']['F_bookId'], '4');
$data['Blist']['F_bookId'] = ($temp[0]. ((String)((int)$temp[1]+1)));
if($this->request->is(array ('post', 'put'))){
$this->Blist->create();
if($this->Blist->save($data)){
$this->session->setFlash("book added successfully");
$this->redirect('add');
}
else{
debug($this->Blist->validationErrors);
$this->session->setFlash("Unable to add book");
}
}
}
答案 0 :(得分:0)
您的ID由数据库的自动增量功能自动创建。没有必要验证F_BookId的用户输入,因为没有,如果在该字段上设置规则notEmpty验证将触发错误。
从$ validate数组中删除此部分:
'F_bookId' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
这应该让你保存书籍。
旁注:您可能希望了解有关CakePHP的命名约定,以使您的生活更轻松:http://book.cakephp.org/2.0/en/getting-started/cakephp-conventions.html