Skip to content
Browse files

Merge cnelissen/master

  • Loading branch information...
2 parents 93c0f91 + b2b7a8b commit 7bbe5fb65041d9830f3e2b7408bd15efe1b857a6 @beberlei committed
View
2 .gitignore
@@ -7,4 +7,4 @@ build/
.buildpath
.project
-tests/Entities/Proxies
+tests/Entities/Proxies
View
41 lib/DoctrineExtensions/Query/Mysql/FindInSet.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * DoctrineExtensions Mysql Function Pack
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to kontakt@beberlei.de so I can send you a copy immediately.
+ */
+
+namespace DoctrineExtensions\Query\Mysql;
+
+use Doctrine\ORM\Query\AST\Functions\FunctionNode;
+use Doctrine\ORM\Query\Lexer;
+
+class FindInSet extends FunctionNode
+{
+ public $needle = null;
+ public $haystack = null;
+
+ public function parse(\Doctrine\ORM\Query\Parser $parser)
+ {
+ $parser->match(Lexer::T_IDENTIFIER);
+ $parser->match(Lexer::T_OPEN_PARENTHESIS);
+ $this->needle = $parser->ArithmeticPrimary();
+ $parser->match(Lexer::T_COMMA);
+ $this->haystack = $parser->ArithmeticPrimary();
+ $parser->match(Lexer::T_CLOSE_PARENTHESIS);
+ }
+
+ public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
+ {
+ return 'FIND_IN_SET(' .
+ $this->needle->dispatch($sqlWalker) . ', ' .
+ $this->haystack->dispatch($sqlWalker) .
+ ')';
+ }
+}
View
41 lib/DoctrineExtensions/Query/Mysql/StrToDate.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * DoctrineExtensions Mysql Function Pack
+ *
+ * LICENSE
+ *
+ * This source file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE.txt.
+ * If you did not receive a copy of the license and are unable to
+ * obtain it through the world-wide-web, please send an email
+ * to kontakt@beberlei.de so I can send you a copy immediately.
+ */
+
+namespace DoctrineExtensions\Query\Mysql;
+
+use Doctrine\ORM\Query\AST\Functions\FunctionNode;
+use Doctrine\ORM\Query\Lexer;
+
+class StrToDate extends FunctionNode
+{
+ public $dateString = null;
+ public $dateFormat = null;
+
+ public function parse(\Doctrine\ORM\Query\Parser $parser)
+ {
+ $parser->match(Lexer::T_IDENTIFIER);
+ $parser->match(Lexer::T_OPEN_PARENTHESIS);
+ $this->dateString = $parser->ArithmeticPrimary();
+ $parser->match(Lexer::T_COMMA);
+ $this->dateFormat = $parser->ArithmeticPrimary();
+ $parser->match(Lexer::T_CLOSE_PARENTHESIS);
+ }
+
+ public function getSql(\Doctrine\ORM\Query\SqlWalker $sqlWalker)
+ {
+ return 'STR_TO_DATE(' .
+ $this->dateString->dispatch($sqlWalker) . ', ' .
+ $this->dateFormat->dispatch($sqlWalker) .
+ ')';
+ }
+}
View
39 tests/DoctrineExtensions/Query/MysqlUdfTest.php
@@ -26,7 +26,8 @@ public function setUp()
$config->addCustomNumericFunction('DATEDIFF', 'DoctrineExtensions\Query\Mysql\DateDiff');
$config->addCustomDatetimeFunction('DATE_ADD', 'DoctrineExtensions\Query\Mysql\DateAdd');
-
+ $config->addCustomStringFunction('STR_TO_DATE', 'DoctrineExtensions\Query\MySql\StrToDate');
+ $config->addCustomStringFunction('FIND_IN_SET', 'DoctrineExtensions\Query\MySql\FindInSet');
$this->entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);
}
@@ -61,5 +62,41 @@ public function testDateAdd2()
$q->getSql();
}
+
+ public function testStrToDate()
+ {
+ $dql = "SELECT p FROM DoctrineExtensions\Query\BlogPost p WHERE STR_TO_DATE(p.created, :dateFormat) < :currentTime";
+ $q = $this->entityManager->createQuery($dql);
+ $q->setParameter('dateFormat', '%Y-%m-%d %h:%i %p');
+ $q->setParameter('currentTime', date('Y-m-d H:i:s'));
+
+ var_dump($q->getSql());
+ }
+
+ public function testFindInSet()
+ {
+ $dql = "SELECT p FROM DoctrineExtensions\Query\BlogPost p WHERE FIND_IN_SET(p.id, p.testSet) != 0";
+ $q = $this->entityManager->createQuery($dql);
+ var_dump($q->getSql());
+ }
}
+
+/**
+ * @Entity
+ */
+class BlogPost
+{
+ /** @Id @Column(type="string") @GeneratedValue */
+ public $id;
+
+ /**
+ * @Column(type="String")
+ */
+ public $testSet;
+
+ /**
+ * @Column(type="DateTime")
+ */
+ public $created;
+}

0 comments on commit 7bbe5fb

Please sign in to comment.
Something went wrong with that request. Please try again.