Cake\Database\Query::execute PHP Метод

execute() публичный Метод

Executing a query internally executes several steps, the first one is letting the connection transform this object to fit its particular dialect, this might result in generating a different Query object that will be the one to actually be executed. Immediately after, literal values are passed to the connection so they are bound to the query in a safe way. Finally, the resulting statement is decorated with custom objects to execute callbacks for each row retrieved if necessary. Resulting statement is traversable, so it can be used in any loop as you would with an array. This method can be overridden in query subclasses to decorate behavior around query execution.
public execute ( ) : Cake\Database\StatementInterface
Результат Cake\Database\StatementInterface
    public function execute()
    {
        $statement = $this->_connection->run($this);
        $driver = $this->_connection->driver();
        $typeMap = $this->selectTypeMap();
        if ($typeMap->toArray() && $this->_typeCastAttached === false) {
            $this->decorateResults(new FieldTypeConverter($typeMap, $driver));
            $this->_typeCastAttached = true;
        }
        $this->_iterator = $this->_decorateStatement($statement);
        $this->_dirty = false;
        return $this->_iterator;
    }

Usage Example

Пример #1
0
 /**
  * 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'));
 }
All Usage Examples Of Cake\Database\Query::execute