当我debug($this->data)
使用 SalasControllerreservar()
方法时,来自 json 的 $data 是可以的,但是当我这样做时,this->Sala->save($this->data)
并尝试返回:debug($data)
beforeSave()
Notice (8): Undefined property: FormataDataBehavior::$data [APP\Model\Behavior\FormataDataBehavior.php, line 52]
\app\Model\Behavior\FormataDataBehavior.php (line 52)
null
SalasController.php
App::uses('AppController', 'Controller');
class SalasController extends AppController {
public $components = array('RequestHandler');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('listar', 'reservar');
$this->RequestHandler->addInputType('json', array('json_decode', true));
}
public function reservar() {
$this->Sala->save($this->data); //no conditions just for debugging.
}
模型 Sala.php
class Sala extends AppModel {
public $useTable = 'agenda';
public $actsAs = array(
'FormataData' => array('dia')
);
行为
class FormataDataBehavior extends ModelBehavior {
// Armazena os campos do model a serem formatados.
public $campos;
//Inicializa o behavior
public function setup(Model $model, $settings = array()) {
if (!empty($settings)) {
$this->campos[$model->name] = $settings;
} else {
throw new MethodNotAllowedException('Campos data não informados na declaração do behavior');
}
}
public function beforeSave(Model $model, $options = array()) {
parent::beforeSave($model, $options);
debug($this->data); //trying show data, but is !isset... =/
die();
}
我的
function adicionarReserva() {
var dadosForm = {Sala: {}};
$.each($("#form-reservas").serializeArray(), function(index, value) {
dadosForm.Sala[value.name] = value.value;
});
$.post("/intracake/Salas/reservar.json", dadosForm)
.done(function(data) {
sysMsg(data.html);
atualizaTabelas();
});
}