Skip to content

Instantly share code, notes, and snippets.

@sergiors
Last active August 29, 2015 14:26
Show Gist options
  • Save sergiors/ae2bcfc339e50f58092c to your computer and use it in GitHub Desktop.
Save sergiors/ae2bcfc339e50f58092c to your computer and use it in GitHub Desktop.
<?php
namespace Inbep\Doctrine\Query\PostgreSql;
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
use Doctrine\ORM\Query\Lexer;
use Doctrine\ORM\Query\Parser;
use Doctrine\ORM\Query\SqlWalker;
class JsonbHashGreaterGreater extends FunctionNode
{
private $expr1 = null;
private $expr2 = null;
public function parse(Parser $parser)
{
$parser->match(Lexer::T_IDENTIFIER);
$parser->match(Lexer::T_OPEN_PARENTHESIS);
$this->expr1 = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_COMMA);
$this->expr2 = $parser->ArithmeticPrimary();
$parser->match(Lexer::T_CLOSE_PARENTHESIS);
}
public function getSql(SqlWalker $sqlWalker)
{
return '('.
$this->expr1->dispatch($sqlWalker).' #>> '.
$this->expr2->dispatch($sqlWalker).
')';
}
}
// how to use
$qb = $this->_em->createQueryBuilder()
->select('u')
->from(User::class, 'u')
->andWhere('u.email = ?1')
->orWhere("JSONB_HGG(u.metadata, '{phones,mobile}') = ?1")
->setParameter(1, $username)
->getQuery()
->getSingleResult();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment