моего контроллера классаНевозможно добавить данные в базе данных с помощью CakePHP
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");
}
}
}
}
Я использовал этот код в моем домашнем компьютере. его рабочий штраф. но тот же код не работает на моем рабочем месте. есть ли какая-либо необходимость в вафе.
, когда я напечатать 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");
}
}
}
, что это ошибка, которую вы получаете? что говорит журнал? у вас есть опечатка в вашем коде $ this-> reDirect ('index'), не связанная с вашей проблемой. –
нет ошибки, показывающей bro ... просто выполняется другое выполнение блока и показывает msg неспособность добавить книгу ... –
любая проверка ошибки? поместите в свой блок else: debug ($ this-> Blist-> validationErrors); –