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

Extend Reserved Word

Hi ,

I need to extend the reserved wordsin PHQL, I do not use Mysql Adapter, and I need to extend it.

When I'm writing : SELECT Id From Contact WHERE CreatedDate=TODAY;

I got a error, telling that TODAY is unknown but TODAY is a good Date fonction for my Database, and support this word.

I saw in a documentation that e could put words between brackets, but it doesn't work.

Is it possible to extend or override any class to add our own Reserved word ?

Best regards

David



85.5k

in pdo its [], so

 SELECT Id From [Contact] WHERE [CreatedDate]=[today];

You have to create your dialect for it. With MySQL, it looks like this: https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Db/Dialect/MysqlExtended.php

edited Feb '17
interface DialectFunctionInterface
{
    public function getFunction();
}

class Today implements DialectFunctionInterface {
    public function getFunction()
    {
        return function(Dialect $dialect, $expression) {
              if (!empty($arguments[0])) {
                  return (new DateTime)->format($arguments[0]);
              }

               return (new DateTime)->format('Y-m-d'); // change default format according to your needs
        };
    }
}

class Mysql extends \Phalcon\Db\Dialect\Mysql
{
    /**
     * Mysql constructor.
     */
    public function __construct()
    {
        $this->registerCustomFunctions();
    }

    /**
     * Register Custom dialect functions
     */
    public function registerCustomFunctions()
    {
        $customFunctions = [
            'TODAY'  => 'Today',
        ];
        foreach ($customFunctions as $key => $value) {
            $className = 'YourNamespaceForExtensionFunctions\\'.$value;
            /** @var DialectFunctionInterface $object */
            $object = new $className;
            $this->registerCustomFunction($key, $object->getFunction());
        }
    }
}

I would use something like this.