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] phalcon Does not support REGEXP in query model

AWESOME !!!!!!!

When i try to execute a query using the mysql regexp, i get this error:

Syntax error, unexpected token IDENTIFIER(REGEXP), near to ' ",(4)," ', when parsing: SELECT [Multiple\Frontend\Models\Test].* FROM [Multiple\Frontend\Models\Test] WHERE  kind REGEXP ",(4),"  

like this error as open question :

https://forum.phalcon.io/discussion/1193/select-query-by-regexp-error

https://github.com/phalcon/cphalcon/issues/2532

edited Oct '14

In secend open question you have an answer to your question:

Add new static function into your model or abstract model:

class MyModel extends \Phalcon\Model {
    public static function findByRawSql($conditions, $params=null)
    {
        // A raw SQL statement
        $sql = "SELECT * FROM robots WHERE $conditions";

        // Base model
        $robot = new Robots();

        // Execute the query
        return new Resultset(null, $robot, $robot->getReadConnection()->query($sql, $params));
    }
}   

Then call

$model = MyModel::findByRawSql('kind REGEXP ?', array('^1[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{4}$'));

tanx softdream for solution but i use another solution with pdo class like you, but why regexp and rlike dosent support in phalcon orm??? its my problem

In secend open question you have an answer to your question:

Add new static function into your model or abstract model:

class MyModel extends \Phalcon\Model {
  public static function findByRawSql($conditions, $params=null)
   {
       // A raw SQL statement
       $sql = "SELECT * FROM robots WHERE $conditions";

       // Base model
       $robot = new Robots();

       // Execute the query
       return new Resultset(null, $robot, $robot->getReadConnection()->query($sql, $params));
   }
}  

Then call

$model = MyModel::findByRawSql('kind REGEXP ?', array('^1[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{4}$'));

Hi amin, but the regxp is supported by Phalcon, but it's not by PHQL. So the solution is as i describe. :) But it's not impossible to use custom PDO or custom Model objects with custom connection to db.