/**
* 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'"), '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'));
}