We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

bug: how to prevent ORM save/create to break ajax request?

It appears that ORM save/create() function are unsuitable thru ajax call (randomly return empty response or 404, even when row is correctly inserted.

Would this be solved in upcoming update, or maybe there is some workaround already here?

Could you please provide the code that makes save/create to break an ajax request?



11.6k
edited Jun '15

example: js:

function addListeArticleRequest() {
    var paramRaw = {};
    paramRaw['tarifClient'] = parseInt($("#tarif-c").text());
    paramRaw['selArticleCode'] = selectedArticleCode;
    var paramJson = JSON.stringify(paramRaw);
    $.ajax({
        type: "POST",
        dataType: 'json',
        data: {
            param: paramJson
        },
        headers: {'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'},
        url: 'localhost/appli/devis/newListArticle/',
        complete: function (response) {
            var rep = JSON.parse(response.responseText);
            var testResponse = rep.content;
            console.log(testResponse);
    });
}

php (controller):

        public function newListArticleAction()
        {
        if ($this->request->isPost()) {
            if ($this->request->isAjax()) {
                $receivedDatas = json_decode($this->request->getPost('param'));
                $newSelArticleCode = $receivedDatas->selArticleCode;
                $tarif = $receivedDatas->tarifClient;
                if (($tarif == '') || (!$tarif)) {
                    $tarif = 1;
                }
                $tarifs = array();
                $selectedArticle = Articles::findFirstByCode($newSelArticleCode);
                if (!$selectedArticle) {
                    $this->view->messagesInfo = "erreur: article introuvable";
                    $this->jsonMessages['contenu'] = 'erreur';
                } else {
                    $tarifs[0] = $selectedArticle->prixVente;
                    $tarifs[1] = $selectedArticle->prixLocation1;
                    $tarifs[2] = $selectedArticle->prixLocation2;
                   if( strcmp($receivedDatas->vl, "VENTE") == 0 ) {
                        $selectedArticle->tarif = $tarifs[0];
                    }
                    else {
                        $selectedArticle->tarif = $tarifs[$tarif];
                    }
                    $tarif = $selectedArticle->tarif;
                    $newListeArticle = new Listearticle();
                    $newListeArticle->tarifFinal = $selectedArticle->tarif;
                    $newListeArticle->articleID = $selectedArticle->id;
                    $newListeArticle->descriptionV = $selectedArticle->description;
                    $newListeArticle->designationV = $selectedArticle->designation;
                    $newListeArticle->stockVente = 0;  //TODO

                    $newListeArticle->save();

                    $this->jsonMessages['content'] = "test message";
                    return $this->jsonMessages;
                }
            }
        }
    }

so, here $newListeArticle object is correctly saved in DB (postgres), but response.responseText is empty. If I comment "$newListeArticle->save();" , then I get a response as expected

($this->jsonMessages is handled in ControllerBase )