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 is showing Error 404 page.

Hello friends, I am trying to use PHQL in my phalcon app but I a not able to understand how it works. I tried different ways to use mysql quesries by PHQL. But now I am getting error 404 page.

My public/index.php file has this codes

    $di->set('modelsManager', function() {
          return new Phalcon\Mvc\Model\Manager();
    });

my sessions.php model has these codes,

<?php

class Sessions extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $id;

    /**
     *
     * @var string
     */
    public $skey;

    /**
     *
     * @var string
     */
    public $time;

    /**
     * Independent Column Mapping.
     */
    public function columnMap()
    {
        return array(
            'id' => 'id', 
            'skey' => 'skey', 
            'time' => 'time'
        );
    }

    //constructor to set session
    public function setSessions(){

        if(!isset($_SESSION['CSRF'])){
            $_SESSION['CSRF'] = self::getSessionKey(self::generateKey());
        }
    }

    public function getSessionKey($var){
        $query = $this->modelsManager->createQuery("SELECT * FROM sessions WHERE skey = :skey:");
        $session = $query->execute(array(
                    'skey' => $var
                ));
    }

    public function generateKey(){

        $charset = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_';
        $len = 7;
        $numrows = 1;
        $csrf_token = '';

        while($numrows != 0 AND strlen($csrf_token) < 8 ){
            for($i = 0; $i <= $len; $i++){
                $rand = rand()% strlen($charset);
                $temp = substr($charset, $rand, 1);
                $csrf_token .= $temp;
            }

            $time = time();
            $find_query = "SELECT `id` FROM `sessions` WHERE BINARY `skey` = '$csrf_token' AND `time` < '$time'";

            $query = new Phalcon\Mvc\Model\Query($find_query);

        }
        return $query;
    }

}

and my index action in index controller has these codes

$session = new Sessions();
$session->setSession();

I tried to follow documentation but I am not able to get it. Can anyone tell me which page should have which codes? And if anyone can suggest me some tutorials to use mysql in phalcon, please let me know.

Hi Viral Joshi, if you place anything else in your IndexController::indexAction are you able to see it?

If not, post your index.php and your IndexController.php

Cheers

If i remove that session model code then yes its working. Not with these codes

Hi Viral Joshi, if you place anything else in your IndexController::indexAction are you able to see it?

If not, post your index.php and your IndexController.php

Cheers



98.9k

Following section is using MySQL escapers which probably could lead to an error:

$time = time();
$find_query = "SELECT `id` FROM `sessions` WHERE BINARY `skey` = '$csrf_token' AND `time` < '$time'";
$query = new Phalcon\Mvc\Model\Query($find_query);

So how can I run my queries in model? I will need to use funcltions of mysql like fetch array, num rows, affected rows etc. So can you tell me how can I use PHQL for that and which codes will go to public/index.php file and other required codes to run PHQL in all models?

Following section is using MySQL escapers which probably could lead to an error:

$time = time();
$find_query = "SELECT `id` FROM `sessions` WHERE BINARY `skey` = '$csrf_token' AND `time` < '$time'";
$query = new Phalcon\Mvc\Model\Query($find_query);


98.9k

This way:

$time = time();
$find_query = "SELECT `id` FROM `sessions` WHERE BINARY `skey` = '$csrf_token' AND `time` < '$time'";
$result = $this->db->fetchAll($find_query);
edited Nov '14

Try this; in your session model public function getSessionKey($var){ $query = $this->modelsManager->createQuery("SELECT * FROM sessions WHERE skey = :skey:"); $this->db->query($query); }

Then in your controller use the following inside the function you are executing $this->getDi()->setShared('session', function() { $session = new Phalcon\Session\Adapter\Files(); $session->start(); return $session; });

error 404 page means your are calling a resource that is currently not in your web server at the time of execution.