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

PHQL

hi,

i have problem to update element, it is my javascript code

           if(seconds_left < 0.00 && seconds_left > -1) {

                $.ajax({
                  url: "https://TEST:8080/invo/index/timerupdate",
                  type: "POST",
                  data: {id: 66},
                  success: function(data){

                  }
               });
           }

controller code:

default type = 1

     public function timerupdateAction() {

           if ($this->request->isAjax()) {

               $id = $this->request->getPost('id', 'int');

               $sql = "UPDATE Videos SET type=0 WHERE id='".$id."'";

               $query = $this->modelsManager->createQuery($sql);

               $items = $query->execute();

               $this->view->disable();

         }
     }
     public function timerupdateAction() {

           if ($this->request->isAjax()) {

               $id = $this->request->getPost('id', 'int');

               $sql = "UPDATE Videos SET type=0 WHERE id='".$id."'";

               echo $sql;

               $query = $this->modelsManager->createQuery($sql);

               $items = $query->execute();

               $this->view->disable();

         }
     }

and this sql code "UPDATE Videos SET type=0 WHERE id='66'" working in phpmyadmin

Idea?



85.5k

is the ajax called at all ?

is the ajax called at all ?

yes



77.7k
Accepted
answer
edited Sep '15

createQuery expects a PHQL, is your Videos model in the global namespace?

For regular SQL, you could also try $this->db->execute(UPDATE videos SET type=0 WHERE id=.$id.)

edited Sep '15

Try calling a function in the controller and direct it to the model to save();

Something like:

$video = new Videos();
$video->saveVideo();

and in model Videos.php:

public function saveVideo() {
 .....
 $this->save();
}

Not quite, if you want to use Models, you'll have to do something like this instead:

public function timerupdateAction() {
    if ($this->request->isAjax()) {
        $id = $this->request->getPost('id', 'int');
        $video = Videos::findFirst($id);
        if($video) {
            $video->assign(array('type'=>0));
            // or $video->setType(0);
            // or $video->type = 0;
            if(!$video->save()) {
                // there was some error, results are in $video->getMessages()
            } else {
                // update success
            }
        } else {
            // video not found
        }
    }
}

Try calling a function in the controller and direct it to the model to save();

Something like:

$video = new Videos(); $video->saveVideo();

and in model Videos.php:

public function saveVideo() { ..... $this->save(); }

edited Sep '15

createQuery expects a PHQL, is your Videos model in the global namespace?

For regular SQL, you could also try $this->db->execute(UPDATE videos SET type=0 WHERE id=.$id.)

it's working perfectly