After a lot of try, I noticed that ajax call to a controller always return nothing when using ->save() or ->create() on a foreign models inside the function. Does it is a bug, or a misconfig' on my side?? here is my setup:
ajax are sorted in baseController.php:
// After route executed event
public function afterExecuteRoute(\Phalcon\Mvc\Dispatcher $dispatcher) {
if($this->request->isAjax() == true) {
$this->view->disableLevel(array(
View::LEVEL_ACTION_VIEW => true,
View::LEVEL_LAYOUT => true,
View::LEVEL_MAIN_LAYOUT => true,
View::LEVEL_AFTER_TEMPLATE => true,
View::LEVEL_BEFORE_TEMPLATE => true
));
$this->response->setContentType('application/json', 'UTF-8');
$data = $this->view->getParamsToView();
/* Set global params if is not set in controller/action */
if (is_array($data)) {
$data['success'] = isset($data['success']) ?: true;
$data['message'] = isset($data['message']) ?: 'blurp';
$data = json_encode($data);
}
$this->response->setContent($data);
}
return $this->response->send();
}
in first model Controller, saving second model is done correctly, but response is empty
public function createNewBlocAction() {
if ($this->request->isPost()) {
if ($this->request->isAjax()) {
$paramBloc = json_decode($this->request->getPost('paramBloc'));
$checkBlocExist = Blocarticle::findFirst(array( //using here a foreign model (we are in InvoiceController.php)
'columns' => '*',
'conditions' => 'operationID = ?1 AND nomBloc = ?2',
'bind' =>
[
1 => $paramBloc->operationID,
2 => $paramBloc->nomBloc,
]
));
if(!$checkBlocExist) {
$blocArticle = new Blocarticle();
$blocArticle->operationID = $paramBloc->operationID;
$blocArticle->nomBloc = $paramBloc->nomBloc;
$blocArticle->save();
$this->view->messageAddNewBloc = "ok"; /// ----> doesn't work, ajax response always empty, but in Db, $blocArticle have been correctly saved
}
else {
$this->view->messageAddNewBloc = "bad";
}
}
}
}
ajax response : {"title":"Devis","success":true,"message":""}
and the same, bypassing the 'save' op:
public function createNewBlocAction() {
if ($this->request->isPost()) {
if ($this->request->isAjax()) {
$paramBloc = json_decode($this->request->getPost('paramBloc'));
$checkBlocExist = Blocarticle::findFirst(array( //using here a foreign model (we are in InvoiceController.php)
'columns' => '*',
'conditions' => 'operationID = ?1 AND nomBloc = ?2',
'bind' =>
[
1 => $paramBloc->operationID,
2 => $paramBloc->nomBloc,
]
));
if(!$checkBlocExist) {
$blocArticle = new Blocarticle();
$blocArticle->operationID = $paramBloc->operationID;
$blocArticle->nomBloc = $paramBloc->nomBloc;
// $blocArticle->save(); we bypass the save operation
$this->view->messageAddNewBloc = "ok"; /// ----> is workin correctly, ajax response not empty
}
else {
$this->view->messageAddNewBloc = "bad";
}
}
}
}
ajax response : {"title":"Devis","messageAddNewBloc":"ok","success":true,"message":""}
any hint?