Doctrine\DBAL\Connection::prepare PHP Method

prepare() public method

Prepares an SQL statement.
public prepare ( string $statement ) : Statement
$statement string The SQL statement to prepare.
return Statement The prepared statement.
    public function prepare($statement)
    {
        try {
            $stmt = new Statement($statement, $this);
        } catch (Exception $ex) {
            throw DBALException::driverExceptionDuringQuery($this->_driver, $ex, $statement);
        }
        $stmt->setFetchMode($this->defaultFetchMode);
        return $stmt;
    }

Usage Example

 /**
  * @test
  */
 public function it_saves_and_reads()
 {
     $schema = new Schema();
     SnapshotStoreSchema::create($schema, 'foo_snapshot');
     foreach ($schema->toSql($this->connection->getDatabasePlatform()) as $sql) {
         $this->connection->executeQuery($sql);
     }
     $adapter = new DoctrineSnapshotAdapter($this->connection);
     $aggregateType = AggregateType::fromString('foo');
     $aggregateRoot = new \stdClass();
     $aggregateRoot->foo = 'bar';
     $time = microtime(true);
     if (false === strpos($time, '.')) {
         $time .= '.0000';
     }
     $now = \DateTimeImmutable::createFromFormat('U.u', $time);
     $snapshot = new Snapshot($aggregateType, 'id', $aggregateRoot, 1, $now);
     $adapter->save($snapshot);
     $snapshot = new Snapshot($aggregateType, 'id', $aggregateRoot, 2, $now);
     $adapter->save($snapshot);
     $this->assertNull($adapter->get($aggregateType, 'invalid'));
     $readSnapshot = $adapter->get($aggregateType, 'id');
     $this->assertEquals($snapshot, $readSnapshot);
     $statement = $this->connection->prepare('SELECT * FROM foo_snapshot');
     $statement->execute();
     $snapshots = $statement->fetchAll();
     $this->assertCount(1, $snapshots);
 }
All Usage Examples Of Doctrine\DBAL\Connection::prepare