本文整理汇总了PHP中Cake\Database\Query::newExpr方法的典型用法代码示例。如果您正苦于以下问题:PHP Query::newExpr方法的具体用法?PHP Query::newExpr怎么用?PHP Query::newExpr使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\Database\Query
的用法示例。
在下文中一共展示了Query::newExpr方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _selectQueryTranslator
/**
* Modify the limit/offset to TSQL
*
* @param \Cake\Database\Query $query The query to translate
* @return \Cake\Database\Query The modified query
*/
protected function _selectQueryTranslator($query)
{
$limit = $query->clause('limit');
$offset = $query->clause('offset');
if ($limit && $offset === null) {
$query->modifier(['_auto_top_' => sprintf('TOP %d', $limit)]);
}
if ($offset !== null && !$query->clause('order')) {
$query->order($query->newExpr()->add('(SELECT NULL)'));
}
if ($this->_version() < 11 && $offset !== null) {
return $this->_pagingSubquery($query, $limit, $offset);
}
return $this->_transformDistinct($query);
}
示例2: testSqlCaseStatement
/**
* Tests that case statements work correctly for various use-cases.
*
* @return void
*/
public function testSqlCaseStatement()
{
$query = new Query($this->connection);
$publishedCase = $query->newExpr()->addCase($query->newExpr()->add(['published' => 'Y']), 1, 'integer');
$notPublishedCase = $query->newExpr()->addCase($query->newExpr()->add(['published' => 'N']), 1, 'integer');
//Postgres requires the case statement to be cast to a integer
if ($this->connection->driver() instanceof \Cake\Database\Driver\Postgres) {
$publishedCase = $query->func()->cast([$publishedCase, 'integer' => 'literal'])->type(' AS ');
$notPublishedCase = $query->func()->cast([$notPublishedCase, 'integer' => 'literal'])->type(' AS ');
}
$results = $query->select(['published' => $query->func()->sum($publishedCase), 'not_published' => $query->func()->sum($notPublishedCase)])->from(['comments'])->execute()->fetchAll('assoc');
$this->assertEquals(5, $results[0]['published']);
$this->assertEquals(1, $results[0]['not_published']);
$query = new Query($this->connection);
$query->insert(['article_id', 'user_id', 'comment', 'published'])->into('comments')->values(['article_id' => 2, 'user_id' => 1, 'comment' => 'In limbo', 'published' => 'L'])->execute()->closeCursor();
$query = new Query($this->connection);
$conditions = [$query->newExpr()->add(['published' => 'Y']), $query->newExpr()->add(['published' => 'N'])];
$values = ['Published', 'Not published', 'None'];
$results = $query->select(['id', 'comment', 'status' => $query->newExpr()->addCase($conditions, $values)])->from(['comments'])->execute()->fetchAll('assoc');
$this->assertEquals('Published', $results[2]['status']);
$this->assertEquals('Not published', $results[3]['status']);
$this->assertEquals('None', $results[6]['status']);
}
示例3: testInsertExpressionValues
/**
* Test that insert can use expression objects as values.
*
* @return void
*/
public function testInsertExpressionValues()
{
$query = new Query($this->connection);
$query->insert(['title', 'author_id'])->into('articles')->values(['title' => $query->newExpr("SELECT 'jose' FROM DUAL"), 'author_id' => 99]);
$result = $query->execute();
$result->closeCursor();
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
$this->assertCount(1, $result);
}
$result = (new Query($this->connection))->select('*')->from('articles')->where(['author_id' => 99])->execute();
$this->assertCount(1, $result);
$expected = ['id' => 4, 'title' => 'jose', 'body' => null, 'author_id' => '99', 'published' => 'N'];
$this->assertEquals($expected, $result->fetch('assoc'));
$subquery = new Query($this->connection);
$subquery->select(['name'])->from('authors')->where(['id' => 1]);
$query = new Query($this->connection);
$query->insert(['title', 'author_id'])->into('articles')->values(['title' => $subquery, 'author_id' => 100]);
$result = $query->execute();
$result->closeCursor();
//PDO_SQLSRV returns -1 for successful inserts when using INSERT ... OUTPUT
if (!$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver) {
$this->assertCount(1, $result);
}
$result = (new Query($this->connection))->select('*')->from('articles')->where(['author_id' => 100])->execute();
$this->assertCount(1, $result);
$expected = ['id' => 5, 'title' => 'mariano', 'body' => null, 'author_id' => '100', 'published' => 'N'];
$this->assertEquals($expected, $result->fetch('assoc'));
}
示例4: testQuotingInsert
/**
* Tests that insert query parts get quoted automatically
*
* @return void
*/
public function testQuotingInsert()
{
$this->connection->driver()->autoQuoting(true);
$query = new Query($this->connection);
$sql = $query->insert(['bar', 'baz'])->into('foo')->where(['something' => 'value'])->sql();
$this->assertQuotedQuery('INSERT INTO <foo> \\(<bar>, <baz>\\)', $sql);
$query = new Query($this->connection);
$sql = $query->insert([$query->newExpr()->add('bar')])->into('foo')->where(['something' => 'value'])->sql();
$this->assertQuotedQuery('INSERT INTO <foo> \\(\\(bar\\)\\)', $sql);
}