thinkingmedia 9 years ago
parent
commit
d696802ea0
2 changed files with 36 additions and 0 deletions
  1. 22 0
      src/Database/Query.php
  2. 14 0
      tests/TestCase/Database/QueryTest.php

+ 22 - 0
src/Database/Query.php

@@ -14,6 +14,7 @@
  */
 namespace Cake\Database;
 
+use Cake\Database\Expression\IdentifierExpression;
 use Cake\Database\Expression\OrderByExpression;
 use Cake\Database\Expression\OrderClauseExpression;
 use Cake\Database\Expression\QueryExpression;
@@ -1462,6 +1463,27 @@ class Query implements ExpressionInterface, IteratorAggregate
     }
 
     /**
+     * Creates an expression that refers to an identifier. Identifiers are used to refer to field names and allow
+     * the SQL compiler to apply quotes or escape the identifier.
+     *
+     * The value is used as is, and you might be required to use aliases or include the table reference in
+     * the identifier. Do not use this method to inject SQL methods or logical statements.
+     *
+     * ### Example
+     *
+     * ```
+     * $query->newExp()->lte('count', $query->identifier('total'));
+     * ```
+     *
+     * @param string $identifier The identifier for an expression
+     * @return ExpressionInterface
+     */
+    public function identifier($identifier)
+    {
+        return new IdentifierExpression($identifier);
+    }
+
+    /**
      * Set the values for an insert query.
      *
      * Multi inserts can be performed by calling values() more than one time,

+ 14 - 0
tests/TestCase/Database/QueryTest.php

@@ -3370,6 +3370,20 @@ class QueryTest extends TestCase
     }
 
     /**
+     * Tests that the identifier method creates an expression object.
+     *
+     * @return void
+     */
+    public function testIdentifierExpression()
+    {
+        $query = new Query($this->connection);
+        /** @var IdentifierExpression $expression */
+        $expression = $query->identifier('foo');
+        $this->assertInstanceOf(IdentifierExpression::class, $expression);
+        $this->assertEquals('foo', $expression->getIdentifier());
+    }
+
+    /**
      * Tests that functions are correctly transformed and their parameters are bound
      *
      * @group FunctionExpression