Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

PHQL

hi,

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

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

                $.ajax({
                  url: "http://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?



60.8k

is the ajax called at all ?

is the ajax called at all ?

yes



35.6k
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