no way to save to db via ajax, need help...

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?

...20$ bounty for the one who helps me to solve that ;-)



36.4k

Hey man

I have take look code you. It haven't good you can try following code below

//Controllerbase

    /**
     * @var array
     */
    public $jsonMessages = [];

    /**
     * After execute route event
     *
     * @param Dispatcher $dispatcher
     */
    public function afterExecuteRoute(Dispatcher $dispatcher)
    {
        if ($this->request->isAjax()) {
            $this->view->disable();
            $this->response->setContentType('application/json', 'UTF-8');

            $data = $dispatcher->getReturnedValue();
            if (is_array($data)) {
                $this->response->setJsonContent($data);
            }
            echo $this->response->getContent();
        }
    }

Then in some controller you an use it

 [...]
  $user = //Dosomething
  if (!$user) {
            $this->jsonMessages['messages'][] = [
                'type'    => 'error',
                'content' =>  'You need to login first'
            ];
            return $this->jsonMessages;
   }

And the final in javscript just to call it