Abstract class Phalcon\Db\Adapter\Pdo

extends abstract class Phalcon\Db\Adapter

implements Phalcon\Events\EventsAwareInterface

Phalcon\Db\Adapter\Pdo is the Phalcon\Db that internally uses PDO to connect to a database

  1. <?php
  2. $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
  3. 'host' => '192.168.0.11',
  4. 'username' => 'sigma',
  5. 'password' => 'secret',
  6. 'dbname' => 'blog',
  7. 'port' => '3306'
  8. ));

Methods

public __construct (unknown $descriptor)

Constructor for Phalcon\Db\Adapter\Pdo

public boolean connect ([unknown $descriptor])

This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. Call it when you need to restore a database connection

  1. <?php
  2. //Make a connection
  3. $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
  4. 'host' => '192.168.0.11',
  5. 'username' => 'sigma',
  6. 'password' => 'secret',
  7. 'dbname' => 'blog',
  8. ));
  9. //Reconnect
  10. $connection->connect();

public PDOStatement prepare (unknown $sqlStatement)

Returns a PDO prepared statement to be executed with ‘executePrepared’

  1. <?php
  2. $statement = $db->prepare('SELECT * FROM robots WHERE name = :name');
  3. $result = $connection->executePrepared($statement, array('name' => 'Voltron'));

public PDOStatement executePrepared (unknown $statement, unknown $placeholders, unknown $dataTypes)

Executes a prepared statement binding. This function uses integer indexes starting from zero

  1. <?php
  2. $statement = $db->prepare('SELECT * FROM robots WHERE name = :name');
  3. $result = $connection->executePrepared($statement, array('name' => 'Voltron'));

public Phalcon\Db\ResultInterface |bool query (unknown $sqlStatement, [unknown $bindParams], [unknown $bindTypes])

Sends SQL statements to the database server returning the success state. Use this method only when the SQL statement sent to the server is returning rows

  1. <?php
  2. //Querying data
  3. $resultset = $connection->query("SELECT * FROM robots WHERE type='mechanical'");
  4. $resultset = $connection->query("SELECT * FROM robots WHERE type=?", array("mechanical"));

public boolean execute (unknown $sqlStatement, [unknown $bindParams], [unknown $bindTypes])

Sends SQL statements to the database server returning the success state. Use this method only when the SQL statement sent to the server doesn’t return any row

  1. <?php
  2. //Inserting data
  3. $success = $connection->execute("INSERT INTO robots VALUES (1, 'Astro Boy')");
  4. $success = $connection->execute("INSERT INTO robots VALUES (?, ?)", array(1, 'Astro Boy'));

public int affectedRows ()

Returns the number of affected rows by the lastest INSERT/UPDATE/DELETE executed in the database system

  1. <?php
  2. $connection->execute("DELETE FROM robots");
  3. echo $connection->affectedRows(), ' were deleted';

public boolean close ()

Closes the active connection returning success. Phalcon automatically closes and destroys active connections when the request ends

public string escapeIdentifier (unknown $identifier)

Escapes a column/table/schema name

  1. <?php
  2. $escapedTable = $connection->escapeIdentifier('robots');
  3. $escapedTable = $connection->escapeIdentifier(array('store', 'robots'));

public string escapeString (unknown $str)

Escapes a value to avoid SQL injections according to the active charset in the connection

  1. <?php
  2. $escapedStr = $connection->escapeString('some dangerous value');

public array convertBoundParams (string $sql, [array $params])

Converts bound parameters such as :name: or ?1 into PDO bind params ?

  1. <?php
  2. print_r($connection->convertBoundParams('SELECT * FROM robots WHERE name = :name:', array('Bender')));

public int|boolean lastInsertId ([unknown $sequenceName])

Returns the insert id for the auto_increment/serial column inserted in the lastest executed SQL statement

  1. <?php
  2. //Inserting a new robot
  3. $success = $connection->insert(
  4. "robots",
  5. array("Astro Boy", 1952),
  6. array("name", "year")
  7. );
  8. //Getting the generated id
  9. $id = $connection->lastInsertId();

public boolean begin ([unknown $nesting])

Starts a transaction in the connection

public boolean rollback ([unknown $nesting])

Rollbacks the active transaction in the connection

public boolean commit ([unknown $nesting])

Commits the active transaction in the connection

public int getTransactionLevel ()

Returns the current transaction nesting level

public boolean isUnderTransaction ()

Checks whether the connection is under a transaction

  1. <?php
  2. $connection->begin();
  3. var_dump($connection->isUnderTransaction()); //true

public PDO getInternalHandler ()

Return internal PDO handler

public array getErrorInfo ()

Return the error info, if any

public getDialectType () inherited from Phalcon\Db\Adapter

Name of the dialect used

public getType () inherited from Phalcon\Db\Adapter

Type of database system the adapter is used for

public getSqlVariables () inherited from Phalcon\Db\Adapter

Active SQL bound parameter variables

public setEventsManager (unknown $eventsManager) inherited from Phalcon\Db\Adapter

Sets the event manager

public Phalcon\Events\ManagerInterface getEventsManager () inherited from Phalcon\Db\Adapter

Returns the internal event manager

public setDialect (unknown $dialect) inherited from Phalcon\Db\Adapter

Sets the dialect used to produce the SQL

public Phalcon\Db\DialectInterface getDialect () inherited from Phalcon\Db\Adapter

Returns internal dialect instance

public array fetchOne (unknown $sqlQuery, [unknown $fetchMode], [unknown $bindParams], [unknown $bindTypes]) inherited from Phalcon\Db\Adapter

Returns the first row in a SQL query result

  1. <?php
  2. //Getting first robot
  3. $robot = $connection->fecthOne("SELECT * FROM robots");
  4. print_r($robot);
  5. //Getting first robot with associative indexes only
  6. $robot = $connection->fecthOne("SELECT * FROM robots", Phalcon\Db::FETCH_ASSOC);
  7. print_r($robot);

public array fetchAll (unknown $sqlQuery, [unknown $fetchMode], [unknown $bindParams], [unknown $bindTypes]) inherited from Phalcon\Db\Adapter

Dumps the complete result of a query into an array

  1. <?php
  2. //Getting all robots with associative indexes only
  3. $robots = $connection->fetchAll("SELECT * FROM robots", Phalcon\Db::FETCH_ASSOC);
  4. foreach ($robots as $robot) {
  5. print_r($robot);
  6. }
  7. //Getting all robots that contains word "robot" withing the name
  8. $robots = $connection->fetchAll("SELECT * FROM robots WHERE name LIKE :name",
  9. Phalcon\Db::FETCH_ASSOC,
  10. array('name' => '%robot%')
  11. );
  12. foreach($robots as $robot){
  13. print_r($robot);
  14. }

public string| fetchColumn (unknown $sqlQuery, [unknown $placeholders], [unknown $column]) inherited from Phalcon\Db\Adapter

Returns the n’th field of first row in a SQL query result

  1. <?php
  2. //Getting count of robots
  3. $robotsCount = $connection->fetchColumn("SELECT count(*) FROM robots");
  4. print_r($robotsCount);
  5. //Getting name of last edited robot
  6. $robot = $connection->fetchColumn("SELECT id, name FROM robots order by modified desc");
  7. print_r($robot);

public boolean insert (unknown $table, unknown $values, [unknown $fields], [unknown $dataTypes]) inherited from Phalcon\Db\Adapter

Inserts data into a table using custom RBDM SQL syntax

  1. <?php
  2. //Inserting a new robot
  3. $success = $connection->insert(
  4. "robots",
  5. array("Astro Boy", 1952),
  6. array("name", "year")
  7. );
  8. //Next SQL sentence is sent to the database system
  9. INSERT INTO `robots` (`name`, `year`) VALUES ("Astro boy", 1952);

public boolean insertAsDict (unknown $table, unknown $data, [unknown $dataTypes]) inherited from Phalcon\Db\Adapter

Inserts data into a table using custom RBDM SQL syntax Another, more convenient syntax

  1. <?php
  2. //Inserting a new robot
  3. $success = $connection->insert(
  4. "robots",
  5. array(
  6. "name" => "Astro Boy",
  7. "year" => 1952
  8. )
  9. );
  10. //Next SQL sentence is sent to the database system
  11. INSERT INTO `robots` (`name`, `year`) VALUES ("Astro boy", 1952);

public boolean update (unknown $table, unknown $fields, unknown $values, [unknown $whereCondition], [unknown $dataTypes]) inherited from Phalcon\Db\Adapter

Updates data on a table using custom RBDM SQL syntax

  1. <?php
  2. //Updating existing robot
  3. $success = $connection->update(
  4. "robots",
  5. array("name"),
  6. array("New Astro Boy"),
  7. "id = 101"
  8. );
  9. //Next SQL sentence is sent to the database system
  10. UPDATE `robots` SET `name` = "Astro boy" WHERE id = 101
  11. //Updating existing robot with array condition and $dataTypes
  12. $success = $connection->update(
  13. "robots",
  14. array("name"),
  15. array("New Astro Boy"),
  16. array(
  17. 'conditions' => "id = ?",
  18. 'bind' => array($some_unsafe_id),
  19. 'bindTypes' => array(PDO::PARAM_INT) //use only if you use $dataTypes param
  20. ),
  21. array(PDO::PARAM_STR)
  22. );

Warning! If $whereCondition is string it not escaped.

public boolean updateAsDict (unknown $table, unknown $data, [unknown $whereCondition], [unknown $dataTypes]) inherited from Phalcon\Db\Adapter

Updates data on a table using custom RBDM SQL syntax Another, more convenient syntax

  1. <?php
  2. //Updating existing robot
  3. $success = $connection->update(
  4. "robots",
  5. array(
  6. "name" => "New Astro Boy"
  7. ),
  8. "id = 101"
  9. );
  10. //Next SQL sentence is sent to the database system
  11. UPDATE `robots` SET `name` = "Astro boy" WHERE id = 101

public boolean delete (unknown $table, [unknown $whereCondition], [unknown $placeholders], [unknown $dataTypes]) inherited from Phalcon\Db\Adapter

Deletes data from a table using custom RBDM SQL syntax

  1. <?php
  2. //Deleting existing robot
  3. $success = $connection->delete(
  4. "robots",
  5. "id = 101"
  6. );
  7. //Next SQL sentence is generated
  8. DELETE FROM `robots` WHERE `id` = 101

public string getColumnList (unknown $columnList) inherited from Phalcon\Db\Adapter

Gets a list of columns

public string limit (unknown $sqlQuery, unknown $number) inherited from Phalcon\Db\Adapter

Appends a LIMIT clause to $sqlQuery argument

  1. <?php
  2. echo $connection->limit("SELECT * FROM robots", 5);

public boolean tableExists (unknown $tableName, [unknown $schemaName]) inherited from Phalcon\Db\Adapter

Generates SQL checking for the existence of a schema.table

  1. <?php
  2. var_dump($connection->tableExists("blog", "posts"));

public boolean viewExists (unknown $viewName, [unknown $schemaName]) inherited from Phalcon\Db\Adapter

Generates SQL checking for the existence of a schema.view

  1. <?php
  2. var_dump($connection->viewExists("active_users", "posts"));

public string forUpdate (unknown $sqlQuery) inherited from Phalcon\Db\Adapter

Returns a SQL modified with a FOR UPDATE clause

public string sharedLock (unknown $sqlQuery) inherited from Phalcon\Db\Adapter

Returns a SQL modified with a LOCK IN SHARE MODE clause

public boolean createTable (unknown $tableName, unknown $schemaName, unknown $definition) inherited from Phalcon\Db\Adapter

Creates a table

public boolean dropTable (unknown $tableName, [unknown $schemaName], [unknown $ifExists]) inherited from Phalcon\Db\Adapter

Drops a table from a schema/database

public boolean createView (unknown $viewName, unknown $definition, [unknown $schemaName]) inherited from Phalcon\Db\Adapter

Creates a view

public boolean dropView (unknown $viewName, [unknown $schemaName], [unknown $ifExists]) inherited from Phalcon\Db\Adapter

Drops a view

public boolean addColumn (unknown $tableName, unknown $schemaName, unknown $column) inherited from Phalcon\Db\Adapter

Adds a column to a table

public boolean modifyColumn (unknown $tableName, unknown $schemaName, unknown $column) inherited from Phalcon\Db\Adapter

Modifies a table column based on a definition

public boolean dropColumn (unknown $tableName, unknown $schemaName, unknown $columnName) inherited from Phalcon\Db\Adapter

Drops a column from a table

public boolean addIndex (unknown $tableName, unknown $schemaName, unknown $index) inherited from Phalcon\Db\Adapter

Adds an index to a table

public boolean dropIndex (unknown $tableName, unknown $schemaName, unknown $indexName) inherited from Phalcon\Db\Adapter

Drop an index from a table

public boolean addPrimaryKey (unknown $tableName, unknown $schemaName, unknown $index) inherited from Phalcon\Db\Adapter

Adds a primary key to a table

public boolean dropPrimaryKey (unknown $tableName, unknown $schemaName) inherited from Phalcon\Db\Adapter

Drops a table’s primary key

public boolean true addForeignKey (unknown $tableName, unknown $schemaName, unknown $reference) inherited from Phalcon\Db\Adapter

Adds a foreign key to a table

public boolean true dropForeignKey (unknown $tableName, unknown $schemaName, unknown $referenceName) inherited from Phalcon\Db\Adapter

Drops a foreign key from a table

public string getColumnDefinition (unknown $column) inherited from Phalcon\Db\Adapter

Returns the SQL column definition from a column

public array listTables ([unknown $schemaName]) inherited from Phalcon\Db\Adapter

List all tables on a database

  1. <?php
  2. print_r($connection->listTables("blog"));

public array listViews ([unknown $schemaName]) inherited from Phalcon\Db\Adapter

List all views on a database

  1. <?php
  2. print_r($connection->listViews("blog"));

public Phalcon\Db\Index [] describeIndexes (unknown $table, [unknown $schema]) inherited from Phalcon\Db\Adapter

Lists table indexes

  1. <?php
  2. print_r($connection->describeIndexes('robots_parts'));

public Phalcon\Db\Reference [] describeReferences (unknown $table, [unknown $schema]) inherited from Phalcon\Db\Adapter

Lists table references

  1. <?php
  2. print_r($connection->describeReferences('robots_parts'));

public array tableOptions (unknown $tableName, [unknown $schemaName]) inherited from Phalcon\Db\Adapter

Gets creation options from a table

  1. <?php
  2. print_r($connection->tableOptions('robots'));

public boolean createSavepoint (unknown $name) inherited from Phalcon\Db\Adapter

Creates a new savepoint

public boolean releaseSavepoint (unknown $name) inherited from Phalcon\Db\Adapter

Releases given savepoint

public boolean rollbackSavepoint (unknown $name) inherited from Phalcon\Db\Adapter

Rollbacks given savepoint

public Phalcon\Db\AdapterInterface setNestedTransactionsWithSavepoints (unknown $nestedTransactionsWithSavepoints) inherited from Phalcon\Db\Adapter

Set if nested transactions should use savepoints

public boolean isNestedTransactionsWithSavepoints () inherited from Phalcon\Db\Adapter

Returns if nested transactions should use savepoints

public string getNestedTransactionSavepointName () inherited from Phalcon\Db\Adapter

Returns the savepoint name to use for nested transactions

public Phalcon\Db\RawValue getDefaultIdValue () inherited from Phalcon\Db\Adapter

Returns the default identity value to be inserted in an identity column

  1. <?php
  2. //Inserting a new robot with a valid default value for the column 'id'
  3. $success = $connection->insert(
  4. "robots",
  5. array($connection->getDefaultIdValue(), "Astro Boy", 1952),
  6. array("id", "name", "year")
  7. );

public boolean supportSequences () inherited from Phalcon\Db\Adapter

Check whether the database system requires a sequence to produce auto-numeric values

public boolean useExplicitIdValue () inherited from Phalcon\Db\Adapter

Check whether the database system requires an explicit value for identity columns

public array getDescriptor () inherited from Phalcon\Db\Adapter

Return descriptor used to connect to the active database

public string getConnectionId () inherited from Phalcon\Db\Adapter

Gets the active connection unique identifier

public string getSQLStatement () inherited from Phalcon\Db\Adapter

Active SQL statement in the object

public string getRealSQLStatement () inherited from Phalcon\Db\Adapter

Active SQL statement in the object without replace bound paramters

public array getSQLBindTypes () inherited from Phalcon\Db\Adapter

Active SQL statement in the object