- Phalcon\Db\AbstractDb
- Phalcon\Db\Adapter\AbstractAdapter
- Phalcon\Db\Adapter\AdapterInterface
- Phalcon\Db\Adapter\Pdo\AbstractPdo
- Phalcon\Db\Adapter\Pdo\Mysql
- Phalcon\Db\Adapter\Pdo\Postgresql
- Phalcon\Db\Adapter\Pdo\Sqlite
- Phalcon\Db\Adapter\PdoFactory
- Phalcon\Db\Column
- Phalcon\Db\ColumnInterface
- Phalcon\Db\Dialect
- Phalcon\Db\Dialect\Mysql
- Phalcon\Db\Dialect\Postgresql
- Phalcon\Db\Dialect\Sqlite
- Phalcon\Db\DialectInterface
- Phalcon\Db\Enum
- Phalcon\Db\Exception
- Phalcon\Db\Index
- Phalcon\Db\IndexInterface
- Phalcon\Db\Profiler
- Phalcon\Db\Profiler\Item
- Phalcon\Db\RawValue
- Phalcon\Db\Reference
- Phalcon\Db\ReferenceInterface
- Phalcon\Db\Result\Pdo
- Phalcon\Db\ResultInterface
Abstract Class Phalcon\Db\AbstractDb
| Namespace | Phalcon\Db | | Uses | \PDO |
Phalcon\Db and its related classes provide a simple SQL database interface for Phalcon Framework. The Phalcon\Db is the basic class you use to connect your PHP application to an RDBMS. There is a different adapter class for each brand of RDBMS.
This component is intended to lower level database operations. If you want to interact with databases using higher level of abstraction use Phalcon\Mvc\Model.
Phalcon\Db\AbstractDb is an abstract class. You only can use it with a database adapter like Phalcon\Db\Adapter\Pdo
use Phalcon\Db;
use Phalcon\Db\Exception;
use Phalcon\Db\Adapter\Pdo\Mysql as MysqlConnection;
try {
$connection = new MysqlConnection(
[
"host" => "192.168.0.11",
"username" => "sigma",
"password" => "secret",
"dbname" => "blog",
"port" => "3306",
]
);
$result = $connection->query(
"SELECTFROM robots LIMIT 5"
);
$result->setFetchMode(Enum::FETCH_NUM);
while ($robot = $result->fetch()) {
print_r($robot);
}
} catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
Methods¶
Enables/disables options in the Database componentAbstract Class Phalcon\Db\Adapter\AbstractAdapter
| Namespace | Phalcon\Db\Adapter | | Uses | Phalcon\Db\DialectInterface, Phalcon\Db\ColumnInterface, Phalcon\Db\Enum, Phalcon\Db\Exception, Phalcon\Db\Index, Phalcon\Db\IndexInterface, Phalcon\Db\Reference, Phalcon\Db\ReferenceInterface, Phalcon\Db\RawValue, Phalcon\Events\EventsAwareInterface, Phalcon\Events\ManagerInterface | | Implements | AdapterInterface, EventsAwareInterface |
Base class for Phalcon\Db\Adapter adapters
Properties¶
/**
* Connection ID
*/
protected static connectionConsecutive = 0;
/**
* Active connection ID
*
* @var long
*/
protected connectionId;
/**
* Descriptor used to connect to a database
*/
protected descriptor;
/**
* Dialect instance
*/
protected dialect;
/**
* Name of the dialect used
*
* @var string
*/
protected dialectType;
/**
* Event Manager
*
* @var ManagerInterface
*/
protected eventsManager;
/**
* The real SQL statement - what was executed
*
* @var string
*/
protected realSqlStatement;
/**
* Active SQL Bind Types
*
* @var array
*/
protected sqlBindTypes;
/**
* Active SQL Statement
*
* @var string
*/
protected sqlStatement;
/**
* Active SQL bound parameter variables
*
* @var array
*/
protected sqlVariables;
/**
* Current transaction level
*/
protected transactionLevel = 0;
/**
* Whether the database supports transactions with save points
*/
protected transactionsWithSavepoints = false;
/**
* Type of database system the adapter is used for
*
* @var string
*/
protected type;
Methods¶
Phalcon\Db\Adapter constructor Adds a column to a tablepublic function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): bool;
public function addPrimaryKey( string $tableName, string $schemaName, IndexInterface $index ): bool;
public function delete( mixed $table, mixed $whereCondition = null, mixed $placeholders = null, mixed $dataTypes = null ): bool;
// Deleting existing robot
$success = $connection->delete(
"robots",
"id = 101"
);
// Next SQL sentence is generated
DELETE FROM `robots` WHERE `id` = 101
public function dropForeignKey( string $tableName, string $schemaName, string $referenceName ): bool;
public function dropTable( string $tableName, string $schemaName = null, bool $ifExists = bool ): bool;
public function dropView( string $viewName, string $schemaName = null, bool $ifExists = bool ): bool;
$escapedTable = $connection->escapeIdentifier(
"robots"
);
$escapedTable = $connection->escapeIdentifier(
[
"store",
"robots",
]
);
public function fetchAll( string $sqlQuery, int $fetchMode = static-constant-access, mixed $bindParams = null, mixed $bindTypes = null ): array;
// Getting all robots with associative indexes only
$robots = $connection->fetchAll(
"SELECTFROM robots",
\Phalcon\Db\Enum::FETCH_ASSOC
);
foreach ($robots as $robot) {
print_r($robot);
}
// Getting all robots that contains word "robot" withing the name
$robots = $connection->fetchAll(
"SELECTFROM robots WHERE name LIKE :name",
\Phalcon\Db\Enum::FETCH_ASSOC,
[
"name" => "%robot%",
]
);
foreach($robots as $robot) {
print_r($robot);
}
public function fetchColumn( string $sqlQuery, array $placeholders = [], mixed $column = int ): string | bool;
// Getting count of robots
$robotsCount = $connection->fetchColumn("SELECT count(*) FROM robots");
print_r($robotsCount);
// Getting name of last edited robot
$robot = $connection->fetchColumn(
"SELECT id, name FROM robots ORDER BY modified DESC",
1
);
print_r($robot);
public function fetchOne( string $sqlQuery, mixed $fetchMode = static-constant-access, mixed $bindParams = null, mixed $bindTypes = null ): array;
// Getting first robot
$robot = $connection->fetchOne("SELECTFROM robots");
print_r($robot);
// Getting first robot with associative indexes only
$robot = $connection->fetchOne(
"SELECTFROM robots",
\Phalcon\Db\Enum::FETCH_ASSOC
);
print_r($robot);
// Inserting a new robot with a valid default value for the column 'id'
$success = $connection->insert(
"robots",
[
$connection->getDefaultIdValue(),
"Astro Boy",
1952,
],
[
"id",
"name",
"year",
]
);
// Inserting a new robot with a valid default value for the column 'year'
$success = $connection->insert(
"robots",
[
"Astro Boy",
$connection->getDefaultValue()
],
[
"name",
"year",
]
);
@todo Return NULL if this is not supported by the adapter
Return descriptor used to connect to the active database Returns internal dialect instance Returns the internal event manager Returns the savepoint name to use for nested transactions Active SQL statement in the object without replace bound parameters Active SQL statement in the object Active SQL statement in the objectpublic function insert( string $table, array $values, mixed $fields = null, mixed $dataTypes = null ): bool;
// Inserting a new robot
$success = $connection->insert(
"robots",
["Astro Boy", 1952],
["name", "year"]
);
// Next SQL sentence is sent to the database system
INSERT INTO `robots` (`name`, `year`) VALUES ("Astro boy", 1952);
// Inserting a new robot
$success = $connection->insertAsDict(
"robots",
[
"name" => "Astro Boy",
"year" => 1952,
]
);
// Next SQL sentence is sent to the database system
INSERT INTO `robots` (`name`, `year`) VALUES ("Astro boy", 1952);
public function modifyColumn( string $tableName, string $schemaName, ColumnInterface $column, ColumnInterface $currentColumn = null ): bool;
public function setNestedTransactionsWithSavepoints( bool $nestedTransactionsWithSavepoints ): AdapterInterface;
@deprecated Will re removed in the next version
Generates SQL checking for the existence of a schema.table Gets creation options from a tablepublic function update( string $table, mixed $fields, mixed $values, mixed $whereCondition = null, mixed $dataTypes = null ): bool;
// Updating existing robot
$success = $connection->update(
"robots",
["name"],
["New Astro Boy"],
"id = 101"
);
// Next SQL sentence is sent to the database system
UPDATE `robots` SET `name` = "Astro boy" WHERE id = 101
// Updating existing robot with array condition and $dataTypes
$success = $connection->update(
"robots",
["name"],
["New Astro Boy"],
[
"conditions" => "id = ?",
"bind" => [$some_unsafe_id],
"bindTypes" => [PDO::PARAM_INT], // use only if you use $dataTypes param
],
[
PDO::PARAM_STR
]
);
Warning! If $whereCondition is string it not escaped.
public function updateAsDict( string $table, mixed $data, mixed $whereCondition = null, mixed $dataTypes = null ): bool;
// Updating existing robot
$success = $connection->updateAsDict(
"robots",
[
"name" => "New Astro Boy",
],
"id = 101"
);
// Next SQL sentence is sent to the database system
UPDATE `robots` SET `name` = "Astro boy" WHERE id = 101
Interface Phalcon\Db\Adapter\AdapterInterface
| Namespace | Phalcon\Db\Adapter | | Uses | Phalcon\Db\DialectInterface, Phalcon\Db\ResultInterface, Phalcon\Db\ColumnInterface, Phalcon\Db\IndexInterface, Phalcon\Db\RawValue, Phalcon\Db\ReferenceInterface |
Interface for Phalcon\Db adapters
Methods¶
Adds a column to a tablepublic function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): bool;
public function addPrimaryKey( string $tableName, string $schemaName, IndexInterface $index ): bool;
public function delete( mixed $table, mixed $whereCondition = null, mixed $placeholders = null, mixed $dataTypes = null ): bool;
public function dropForeignKey( string $tableName, string $schemaName, string $referenceName ): bool;
public function dropTable( string $tableName, string $schemaName = null, bool $ifExists = bool ): bool;
public function dropView( string $viewName, string $schemaName = null, bool $ifExists = bool ): bool;
public function execute( string $sqlStatement, mixed $placeholders = null, mixed $dataTypes = null ): bool;
public function fetchAll( string $sqlQuery, int $fetchMode = int, mixed $placeholders = null ): array;
public function fetchColumn( string $sqlQuery, array $placeholders = [], mixed $column = int ): string | bool;
// Getting count of robots
$robotsCount = $connection->fetchColumn("SELECT COUNT(*) FROM robots");
print_r($robotsCount);
// Getting name of last edited robot
$robot = $connection->fetchColumn(
"SELECT id, name FROM robots ORDER BY modified DESC",
1
);
print_r($robot);
public function fetchOne( string $sqlQuery, int $fetchMode = int, mixed $placeholders = null ): array;
// Inserting a new robot with a valid default value for the column 'year'
$success = $connection->insert(
"robots",
[
"Astro Boy",
$connection->getDefaultValue()
],
[
"name",
"year",
]
);
@todo Return NULL if this is not supported by the adapter
Return descriptor used to connect to the active database Returns internal dialect instance Returns the name of the dialect used Return internal PDO handler Returns the savepoint name to use for nested transactions Active SQL statement in the object without replace bound parameters Active SQL statement in the object Active SQL statement in the object Active SQL statement in the object Returns type of database system the adapter is used forpublic function insert( string $table, array $values, mixed $fields = null, mixed $dataTypes = null ): bool;
// Inserting a new robot
$success = $connection->insertAsDict(
"robots",
[
"name" => "Astro Boy",
"year" => 1952,
]
);
// Next SQL sentence is sent to the database system
INSERT INTO `robots` (`name`, `year`) VALUES ("Astro boy", 1952);
public function modifyColumn( string $tableName, string $schemaName, ColumnInterface $column, ColumnInterface $currentColumn = null ): bool;
public function query( string $sqlStatement, mixed $placeholders = null, mixed $dataTypes = null ): ResultInterface | bool;
public function setNestedTransactionsWithSavepoints( bool $nestedTransactionsWithSavepoints ): AdapterInterface;
@deprecated Will re removed in the next version
Generates SQL checking for the existence of a schema.table Gets creation options from a tablepublic function update( string $table, mixed $fields, mixed $values, mixed $whereCondition = null, mixed $dataTypes = null ): bool;
public function updateAsDict( string $table, mixed $data, mixed $whereCondition = null, mixed $dataTypes = null ): bool;
// Updating existing robot
$success = $connection->updateAsDict(
"robots",
[
"name" => "New Astro Boy",
],
"id = 101"
);
// Next SQL sentence is sent to the database system
UPDATE `robots` SET `name` = "Astro boy" WHERE id = 101
Abstract Class Phalcon\Db\Adapter\Pdo\AbstractPdo
| Namespace | Phalcon\Db\Adapter\Pdo | | Uses | Phalcon\Db\Adapter\AbstractAdapter, Phalcon\Db\Column, Phalcon\Db\Exception, Phalcon\Db\Result\Pdo, Phalcon\Db\ResultInterface, Phalcon\Events\ManagerInterface | | Extends | AbstractAdapter |
Phalcon\Db\Adapter\Pdo is the Phalcon\Db that internally uses PDO to connect to a database
use Phalcon\Db\Adapter\Pdo\Mysql;
$config = [
"host" => "localhost",
"dbname" => "blog",
"port" => 3306,
"username" => "sigma",
"password" => "secret",
];
$connection = new Mysql($config);
Properties¶
/**
* Last affected rows
*/
protected affectedRows;
/**
* PDO Handler
*
* @var \PDO
*/
protected pdo;
Methods¶
Constructor for Phalcon\Db\Adapter\Pdo Returns the number of affected rows by the latest INSERT/UPDATE/DELETE executed in the database system Starts a transaction in the connection Closes the active connection returning success. Phalcon automatically closes and destroys active connections when the request ends Commits the active transaction in the connection This method is automatically called in \Phalcon\Db\Adapter\Pdo constructor.Call it when you need to restore a database connection.
use Phalcon\Db\Adapter\Pdo\Mysql;
// Make a connection
$connection = new Mysql(
[
"host" => "localhost",
"username" => "sigma",
"password" => "secret",
"dbname" => "blog",
"port" => 3306,
]
);
// Reconnect
$connection->connect();
print_r(
$connection->convertBoundParams(
"SELECTFROM robots WHERE name = :name:",
[
"Bender",
]
)
);
public function execute( string $sqlStatement, mixed $bindParams = null, mixed $bindTypes = null ): bool;
// Inserting data
$success = $connection->execute(
"INSERT INTO robots VALUES (1, 'Astro Boy')"
);
$success = $connection->execute(
"INSERT INTO robots VALUES (?, ?)",
[
1,
"Astro Boy",
]
);
public function executePrepared( \PDOStatement $statement, array $placeholders, mixed $dataTypes ): \PDOStatement;
use Phalcon\Db\Column;
$statement = $db->prepare(
"SELECTFROM robots WHERE name = :name"
);
$result = $connection->executePrepared(
$statement,
[
"name" => "Voltron",
],
[
"name" => Column::BIND_PARAM_STR,
]
);
// Inserting a new robot
$success = $connection->insert(
"robots",
[
"Astro Boy",
1952,
],
[
"name",
"year",
]
);
// Getting the generated id
$id = $connection->lastInsertId();
use Phalcon\Db\Column;
$statement = $db->prepare(
"SELECTFROM robots WHERE name = :name"
);
$result = $connection->executePrepared(
$statement,
[
"name" => "Voltron",
],
[
"name" => Column::BIND_PARAM_INT,
]
);
public function query( string $sqlStatement, mixed $bindParams = null, mixed $bindTypes = null ): ResultInterface | bool;
// Querying data
$resultset = $connection->query(
"SELECTFROM robots WHERE type = 'mechanical'"
);
$resultset = $connection->query(
"SELECTFROM robots WHERE type = ?",
[
"mechanical",
]
);
@see https://stackoverflow.com/a/8403150
Class Phalcon\Db\Adapter\Pdo\Mysql
| Namespace | Phalcon\Db\Adapter\Pdo | | Uses | Phalcon\Db\Adapter\Pdo\AbstractPdo, Phalcon\Db\Column, Phalcon\Db\ColumnInterface, Phalcon\Db\Enum, Phalcon\Db\Exception, Phalcon\Db\Index, Phalcon\Db\IndexInterface, Phalcon\Db\Reference, Phalcon\Db\ReferenceInterface | | Extends | PdoAdapter |
Specific functions for the MySQL database system
use Phalcon\Db\Adapter\Pdo\Mysql;
$config = [
"host" => "localhost",
"dbname" => "blog",
"port" => 3306,
"username" => "sigma",
"password" => "secret",
];
$connection = new Mysql($config);
Properties¶
Methods¶
public function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): bool;
Class Phalcon\Db\Adapter\Pdo\Postgresql
| Namespace | Phalcon\Db\Adapter\Pdo | | Uses | Phalcon\Db\Adapter\Pdo\AbstractPdo, Phalcon\Db\Column, Phalcon\Db\ColumnInterface, Phalcon\Db\Enum, Phalcon\Db\Exception, Phalcon\Db\RawValue, Phalcon\Db\Reference, Phalcon\Db\ReferenceInterface, Throwable | | Extends | PdoAdapter |
Specific functions for the PostgreSQL database system
use Phalcon\Db\Adapter\Pdo\Postgresql;
$config = [
"host" => "localhost",
"dbname" => "blog",
"port" => 5432,
"username" => "postgres",
"password" => "secret",
];
$connection = new Postgresql($config);
Properties¶
/**
* @var string
*/
protected dialectType = postgresql;
/**
* @var string
*/
protected type = pgsql;
Methods¶
Constructor for Phalcon\Db\Adapter\Pdo\Postgresql This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. Call it when you need to restore a database connection. Creates a table Returns an array of Phalcon\Db\Column objects describing a table Lists table references Returns the default identity value to be inserted in an identity column// Inserting a new robot with a valid default value for the column 'id'
$success = $connection->insert(
"robots",
[
$connection->getDefaultIdValue(),
"Astro Boy",
1952,
],
[
"id",
"name",
"year",
]
);
public function modifyColumn( string $tableName, string $schemaName, ColumnInterface $column, ColumnInterface $currentColumn = null ): bool;
Class Phalcon\Db\Adapter\Pdo\Sqlite
| Namespace | Phalcon\Db\Adapter\Pdo | | Uses | Phalcon\Db\Adapter\Pdo\AbstractPdo, Phalcon\Db\Column, Phalcon\Db\ColumnInterface, Phalcon\Db\Enum, Phalcon\Db\Exception, Phalcon\Db\Index, Phalcon\Db\IndexInterface, Phalcon\Db\RawValue, Phalcon\Db\Reference, Phalcon\Db\ReferenceInterface | | Extends | PdoAdapter |
Specific functions for the SQLite database system
Properties¶
Methods¶
Constructor for Phalcon\Db\Adapter\Pdo\Sqlite This method is automatically called in Phalcon\Db\Adapter\Pdo constructor. Call it when you need to restore a database connection. Returns an array of Phalcon\Db\Column objects describing a table Lists table indexes Lists table references Returns the default value to make the RBDM use the default value declared in the table definition// Inserting a new robot with a valid default value for the column 'year'
$success = $connection->insert(
"robots",
[
"Astro Boy",
$connection->getDefaultValue(),
],
[
"name",
"year",
]
);
@deprecated Will re removed in the next version
Check whether the database system requires an explicit value for identity columns Returns PDO adapter DSN defaults as a key-value map.Class Phalcon\Db\Adapter\PdoFactory
| Namespace | Phalcon\Db\Adapter | | Uses | Phalcon\Factory\AbstractFactory, Phalcon\Helper\Arr | | Extends | AbstractFactory |
This file is part of the Phalcon Framework.
(c) Phalcon Team team@phalcon.io
For the full copyright and license information, please view the LICENSE.txt file that was distributed with this source code.
Methods¶
Constructor Factory to create an instance from a Config object Create a new instance of the adapter Returns the available adaptersClass Phalcon\Db\Column
| Namespace | Phalcon\Db | | Implements | ColumnInterface |
Allows to define columns to be used on create or alter table operations
use Phalcon\Db\Column as Column;
// Column definition
$column = new Column(
"id",
[
"type" => Column::TYPE_INTEGER,
"size" => 10,
"unsigned" => true,
"notNull" => true,
"autoIncrement" => true,
"first" => true,
"comment" => "",
]
);
// Add column to existing table
$connection->addColumn("robots", null, $column);
Constants¶
const BIND_PARAM_BLOB = 3;
const BIND_PARAM_BOOL = 5;
const BIND_PARAM_DECIMAL = 32;
const BIND_PARAM_INT = 1;
const BIND_PARAM_NULL = 0;
const BIND_PARAM_STR = 2;
const BIND_SKIP = 1024;
const TYPE_BIGINTEGER = 14;
const TYPE_BIT = 19;
const TYPE_BLOB = 11;
const TYPE_BOOLEAN = 8;
const TYPE_CHAR = 5;
const TYPE_DATE = 1;
const TYPE_DATETIME = 4;
const TYPE_DECIMAL = 3;
const TYPE_DOUBLE = 9;
const TYPE_ENUM = 18;
const TYPE_FLOAT = 7;
const TYPE_INTEGER = 0;
const TYPE_JSON = 15;
const TYPE_JSONB = 16;
const TYPE_LONGBLOB = 13;
const TYPE_LONGTEXT = 24;
const TYPE_MEDIUMBLOB = 12;
const TYPE_MEDIUMINTEGER = 21;
const TYPE_MEDIUMTEXT = 23;
const TYPE_SMALLINTEGER = 22;
const TYPE_TEXT = 6;
const TYPE_TIME = 20;
const TYPE_TIMESTAMP = 17;
const TYPE_TINYBLOB = 10;
const TYPE_TINYINTEGER = 26;
const TYPE_TINYTEXT = 25;
const TYPE_VARCHAR = 2;
Properties¶
/**
* Column Position
*
* @var string|null
*/
protected after;
/**
* Column is autoIncrement?
*
* @var bool
*/
protected autoIncrement = false;
/**
* Bind Type
*/
protected bindType = 2;
/**
* Default column value
*/
protected _default;
/**
* Position is first
*
* @var bool
*/
protected first = false;
/**
* The column have some numeric type?
*/
protected isNumeric = false;
/**
* Column's name
*
* @var string
*/
protected name;
/**
* Column's comment
*
* @var string
*/
protected comment;
/**
* Column not nullable?
*
* Default SQL definition is NOT NULL.
*
* @var bool
*/
protected notNull = true;
/**
* Column is part of the primary key?
*/
protected primary = false;
/**
* Integer column number scale
*
* @var int
*/
protected scale = 0;
/**
* Integer column size
*
* @var int | string
*/
protected size = 0;
/**
* Column data type
*
* @var int
*/
protected type;
/**
* Column data type reference
*
* @var int
*/
protected typeReference = -1;
/**
* Column data type values
*
* @var array|string
*/
protected typeValues;
/**
* Integer column unsigned?
*
* @var bool
*/
protected unsigned = false;
Methods¶
Phalcon\Db\Column constructor Check whether field absolute to position in table Returns the type of bind handling Check whether column has default value Auto-Increment Check whether column have first position in table Not null Check whether column have an numeric type Column is part of the primary key? Returns true if number column is unsignedInterface Phalcon\Db\ColumnInterface
| Namespace | Phalcon\Db |
Interface for Phalcon\Db\Column
Methods¶
Check whether field absolute to position in table Returns the type of bind handling Returns default value of column Returns column name Returns column scale Returns column size Returns column type Returns column type reference Returns column type values Check whether column has default value Auto-Increment Check whether column have first position in table Not null Check whether column have an numeric type Column is part of the primary key? Returns true if number column is unsignedAbstract Class Phalcon\Db\Dialect
| Namespace | Phalcon\Db | | Implements | DialectInterface |
This is the base class to each database dialect. This implements common methods to transform intermediate code into its RDBMS related syntax
Properties¶
Methods¶
Generate SQL to create a new savepoint Escape identifiers Escape Schema Returns a SQL modified with a FOR UPDATE clausefinal public function getColumnList( array $columnList, string $escapeChar = null, mixed $bindCounts = null ): string;
final public function getSqlColumn( mixed $column, string $escapeChar = null, mixed $bindCounts = null ): string;
public function getSqlExpression( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
// SELECTFROM robots LIMIT 10
echo $dialect->limit(
"SELECTFROM robots",
10
);
// SELECTFROM robots LIMIT 10 OFFSET 50
echo $dialect->limit(
"SELECTFROM robots",
[10, 50]
);
final protected function getSqlExpressionAll( array $expression, string $escapeChar = null ): string;
final protected function getSqlExpressionBinaryOperations( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionCase( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionCastValue( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionConvertValue( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionFrom( mixed $expression, string $escapeChar = null ): string;
final protected function getSqlExpressionFunctionCall( array $expression, string $escapeChar = null, mixed $bindCounts ): string;
final protected function getSqlExpressionGroupBy( mixed $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionHaving( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionJoins( mixed $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionLimit( mixed $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionList( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionObject( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionOrderBy( mixed $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionQualified( array $expression, string $escapeChar = null ): string;
final protected function getSqlExpressionScalar( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionUnaryOperations( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
final protected function getSqlExpressionWhere( mixed $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
protected function prepareColumnAlias( string $qualified, string $alias = null, string $escapeChar = null ): string;
protected function prepareQualified( string $column, string $domain = null, string $escapeChar = null ): string;
protected function prepareTable( string $table, string $schema = null, string $alias = null, string $escapeChar = null ): string;
Class Phalcon\Db\Dialect\Mysql
| Namespace | Phalcon\Db\Dialect | | Uses | Phalcon\Db\Dialect, Phalcon\Db\Column, Phalcon\Db\Exception, Phalcon\Db\IndexInterface, Phalcon\Db\ColumnInterface, Phalcon\Db\ReferenceInterface, Phalcon\Db\DialectInterface | | Extends | Dialect |
Generates database specific SQL for the MySQL RDBMS
Properties¶
Methods¶
public function addColumn( string $tableName, string $schemaName, ColumnInterface $column ): string;
public function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): string;
public function addPrimaryKey( string $tableName, string $schemaName, IndexInterface $index ): string;
public function createView( string $viewName, array $definition, string $schemaName = null ): string;
public function dropForeignKey( string $tableName, string $schemaName, string $referenceName ): string;
public function dropTable( string $tableName, string $schemaName = null, bool $ifExists = bool ): string;
public function dropView( string $viewName, string $schemaName = null, bool $ifExists = bool ): string;
public function modifyColumn( string $tableName, string $schemaName, ColumnInterface $column, ColumnInterface $currentColumn = null ): string;
$sql = $dialect->sharedLock("SELECTFROM robots");
echo $sql; // SELECTFROM robots LOCK IN SHARE MODE
Class Phalcon\Db\Dialect\Postgresql
| Namespace | Phalcon\Db\Dialect | | Uses | Phalcon\Db\Dialect, Phalcon\Db\Column, Phalcon\Db\Exception, Phalcon\Db\IndexInterface, Phalcon\Db\ColumnInterface, Phalcon\Db\ReferenceInterface, Phalcon\Db\DialectInterface | | Extends | Dialect |
Generates database specific SQL for the PostgreSQL RDBMS
Properties¶
Methods¶
public function addColumn( string $tableName, string $schemaName, ColumnInterface $column ): string;
public function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): string;
public function addPrimaryKey( string $tableName, string $schemaName, IndexInterface $index ): string;
public function createView( string $viewName, array $definition, string $schemaName = null ): string;
public function dropForeignKey( string $tableName, string $schemaName, string $referenceName ): string;
public function dropTable( string $tableName, string $schemaName = null, bool $ifExists = bool ): string;
public function dropView( string $viewName, string $schemaName = null, bool $ifExists = bool ): string;
public function modifyColumn( string $tableName, string $schemaName, ColumnInterface $column, ColumnInterface $currentColumn = null ): string;
Class Phalcon\Db\Dialect\Sqlite
| Namespace | Phalcon\Db\Dialect | | Uses | Phalcon\Db\Column, Phalcon\Db\Exception, Phalcon\Db\IndexInterface, Phalcon\Db\Dialect, Phalcon\Db\DialectInterface, Phalcon\Db\ColumnInterface, Phalcon\Db\ReferenceInterface | | Extends | Dialect |
Generates database specific SQL for the SQLite RDBMS
Properties¶
Methods¶
public function addColumn( string $tableName, string $schemaName, ColumnInterface $column ): string;
public function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): string;
public function addPrimaryKey( string $tableName, string $schemaName, IndexInterface $index ): string;
public function createView( string $viewName, array $definition, string $schemaName = null ): string;
public function dropForeignKey( string $tableName, string $schemaName, string $referenceName ): string;
public function dropTable( string $tableName, string $schemaName = null, bool $ifExists = bool ): string;
public function dropView( string $viewName, string $schemaName = null, bool $ifExists = bool ): string;
public function listIndexesSql( string $table, string $schema = null, string $keyName = null ): string;
public function modifyColumn( string $tableName, string $schemaName, ColumnInterface $column, ColumnInterface $currentColumn = null ): string;
Interface Phalcon\Db\DialectInterface
| Namespace | Phalcon\Db |
Interface for Phalcon\Db dialects
Methods¶
public function addColumn( string $tableName, string $schemaName, ColumnInterface $column ): string;
public function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): string;
public function addPrimaryKey( string $tableName, string $schemaName, IndexInterface $index ): string;
public function createView( string $viewName, array $definition, string $schemaName = null ): string;
public function dropForeignKey( string $tableName, string $schemaName, string $referenceName ): string;
public function dropView( string $viewName, string $schemaName = null, bool $ifExists = bool ): string;
public function getSqlExpression( array $expression, string $escapeChar = null, mixed $bindCounts = null ): string;
public function modifyColumn( string $tableName, string $schemaName, ColumnInterface $column, ColumnInterface $currentColumn = null ): string;
Class Phalcon\Db\Enum
| Namespace | Phalcon\Db |
Constants for Phalcon\Db
Constants¶
const FETCH_ASSOC;
const FETCH_BOTH;
const FETCH_BOUND;
const FETCH_CLASS;
const FETCH_CLASSTYPE;
const FETCH_COLUMN;
const FETCH_FUNC;
const FETCH_GROUP;
const FETCH_INTO;
const FETCH_KEY_PAIR;
const FETCH_LAZY;
const FETCH_NAMED;
const FETCH_NUM;
const FETCH_OBJ;
const FETCH_PROPS_LATE;
const FETCH_SERIALIZE;
const FETCH_UNIQUE;
Class Phalcon\Db\Exception
| Namespace | Phalcon\Db | | Extends | \Phalcon\Exception |
Exceptions thrown in Phalcon\Db will use this class
Class Phalcon\Db\Index
| Namespace | Phalcon\Db | | Implements | IndexInterface |
Allows to define indexes to be used on tables. Indexes are a common way to enhance database performance. An index allows the database server to find and retrieve specific rows much faster than it could do without an index
// Define new unique index
$index_unique = new \Phalcon\Db\Index(
'column_UNIQUE',
[
'column',
'column',
],
'UNIQUE'
);
// Define new primary index
$index_primary = new \Phalcon\Db\Index(
'PRIMARY',
[
'column',
]
);
// Add index to existing table
$connection->addIndex("robots", null, $index_unique);
$connection->addIndex("robots", null, $index_primary);
Properties¶
/**
* Index columns
*
* @var array
*/
protected columns;
/**
* Index name
*
* @var string
*/
protected name;
/**
* Index type
*
* @var string
*/
protected type;
Methods¶
Phalcon\Db\Index constructorInterface Phalcon\Db\IndexInterface
| Namespace | Phalcon\Db |
Interface for Phalcon\Db\Index
Methods¶
Gets the columns that corresponds the index Gets the index name Gets the index typeClass Phalcon\Db\Profiler
| Namespace | Phalcon\Db | | Uses | Phalcon\Db\Profiler\Item |
Instances of Phalcon\Db can generate execution profiles on SQL statements sent to the relational database. Profiled information includes execution time in milliseconds. This helps you to identify bottlenecks in your applications.
use Phalcon\Db\Profiler;
use Phalcon\Events\Event;
use Phalcon\Events\Manager;
$profiler = new Profiler();
$eventsManager = new Manager();
$eventsManager->attach(
"db",
function (Event $event, $connection) use ($profiler) {
if ($event->getType() === "beforeQuery") {
$sql = $connection->getSQLStatement();
// Start a profile with the active connection
$profiler->startProfile($sql);
}
if ($event->getType() === "afterQuery") {
// Stop the active profile
$profiler->stopProfile();
}
}
);
// Set the event manager on the connection
$connection->setEventsManager($eventsManager);
$sql = "SELECT buyer_name, quantity, product_name
FROM buyers LEFT JOIN products ON
buyers.pid=products.id";
// Execute a SQL statement
$connection->query($sql);
// Get the last profile in the profiler
$profile = $profiler->getLastProfile();
echo "SQL Statement: ", $profile->getSQLStatement(), "\n";
echo "Start Time: ", $profile->getInitialTime(), "\n";
echo "Final Time: ", $profile->getFinalTime(), "\n";
echo "Total Elapsed Time: ", $profile->getTotalElapsedSeconds(), "\n";
Properties¶
/**
* Active Phalcon\Db\Profiler\Item
*
* @var Phalcon\Db\Profiler\Item
*/
protected activeProfile;
/**
* All the Phalcon\Db\Profiler\Item in the active profile
*
* @var \Phalcon\Db\Profiler\Item[]
*/
protected allProfiles;
/**
* Total time spent by all profiles to complete
*
* @var float
*/
protected totalSeconds = 0;
Methods¶
Returns the last profile executed in the profiler Returns the total number of SQL statements processed Returns all the processed profiles Returns the total time in seconds spent by the profiles Resets the profiler, cleaning up all the profilespublic function startProfile( string $sqlStatement, mixed $sqlVariables = null, mixed $sqlBindTypes = null ): Profiler;
Class Phalcon\Db\Profiler\Item
| Namespace | Phalcon\Db\Profiler |
This class identifies each profile in a Phalcon\Db\Profiler
Properties¶
/**
* Timestamp when the profile ended
*
* @var double
*/
protected finalTime;
/**
* Timestamp when the profile started
*
* @var double
*/
protected initialTime;
/**
* SQL bind types related to the profile
*
* @var array
*/
protected sqlBindTypes;
/**
* SQL statement related to the profile
*
* @var string
*/
protected sqlStatement;
/**
* SQL variables related to the profile
*
* @var array
*/
protected sqlVariables;
Methods¶
Returns the total time in seconds spent by the profileClass Phalcon\Db\RawValue
| Namespace | Phalcon\Db |
This class allows to insert/update raw data without quoting or formatting.
The next example shows how to use the MySQL now() function as a field value.
$subscriber = new Subscribers();
$subscriber->email = "[email protected]";
$subscriber->createdAt = new \Phalcon\Db\RawValue("now()");
$subscriber->save();
Properties¶
Methods¶
Phalcon\Db\RawValue constructorClass Phalcon\Db\Reference
| Namespace | Phalcon\Db | | Implements | ReferenceInterface |
Allows to define reference constraints on tables
$reference = new \Phalcon\Db\Reference(
"field_fk",
[
"referencedSchema" => "invoicing",
"referencedTable" => "products",
"columns" => [
"producttype",
"product_code",
],
"referencedColumns" => [
"type",
"code",
],
]
);
Properties¶
/**
* Local reference columns
*
* @var array
*/
protected columns;
/**
* Constraint name
*
* @var string
*/
protected name;
/**
* Referenced Columns
*
* @var array
*/
protected referencedColumns;
/**
* Referenced Schema
*
* @var string
*/
protected referencedSchema;
/**
* Referenced Table
*
* @var string
*/
protected referencedTable;
/**
* Schema name
*
* @var string
*/
protected schemaName;
/**
* ON DELETE
*
* @var string
*/
protected onDelete;
/**
* ON UPDATE
*
* @var string
*/
protected onUpdate;
Methods¶
Phalcon\Db\Reference constructorInterface Phalcon\Db\ReferenceInterface
| Namespace | Phalcon\Db |
Interface for Phalcon\Db\Reference
Methods¶
Gets local columns which reference is based Gets the index name Gets the referenced on delete Gets the referenced on update Gets referenced columns Gets the schema where referenced table is Gets the referenced table Gets the schema where referenced table isClass Phalcon\Db\Result\Pdo
| Namespace | Phalcon\Db\Result | | Uses | Phalcon\Db\Enum, Phalcon\Db\ResultInterface, Phalcon\Db\Adapter\AdapterInterface | | Implements | ResultInterface |
Encapsulates the resultset internals
$result = $connection->query("SELECTFROM robots ORDER BY name");
$result->setFetchMode(
\Phalcon\Db\Enum::FETCH_NUM
);
while ($robot = $result->fetchArray()) {
print_r($robot);
}
Properties¶
//
protected bindParams;
//
protected bindTypes;
//
protected connection;
/**
* Active fetch mode
*/
protected fetchMode;
/**
* Internal resultset
*
* @var \PDOStatement
*/
protected pdoStatement;
//
protected result;
//
protected rowCount = false;
//
protected sqlStatement;
Methods¶
public function __construct( AdapterInterface $connection, \PDOStatement $result, mixed $sqlStatement = null, mixed $bindParams = null, mixed $bindTypes = null );
$result = $connection->query(
"SELECTFROM robots ORDER BY name"
);
// Move to third row on result
$result->dataSeek(2);
// Fetch third row
$row = $result->fetch();
public function fetch( mixed $fetchStyle = null, mixed $cursorOrientation = null, mixed $cursorOffset = null );
Phalcon\Db\Result\Pdo::setFetchMode()
$result = $connection->query("SELECTFROM robots ORDER BY name");
$result->setFetchMode(
\Phalcon\Enum::FETCH_OBJ
);
while ($robot = $result->fetch()) {
echo $robot->name;
}
public function fetchAll( mixed $fetchStyle = null, mixed $fetchArgument = null, mixed $ctorArgs = null ): array;
Phalcon\Db\Result\Pdo::setFetchMode()
Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. This method is affected by the active fetch flag set using Phalcon\Db\Result\Pdo::setFetchMode()
$result = $connection->query("SELECTFROM robots ORDER BY name");
$result->setFetchMode(
\Phalcon\Enum::FETCH_NUM
);
while ($robot = result->fetchArray()) {
print_r($robot);
}
$result = $connection->query(
"SELECTFROM robots ORDER BY name"
);
echo "There are ", $result->numRows(), " rows in the resultset";
public function setFetchMode( int $fetchMode, mixed $colNoOrClassNameOrObject = null, mixed $ctorargs = null ): bool;
// Return array with integer indexes
$result->setFetchMode(
\Phalcon\Enum::FETCH_NUM
);
// Return associative array without integer indexes
$result->setFetchMode(
\Phalcon\Enum::FETCH_ASSOC
);
// Return associative array together with integer indexes
$result->setFetchMode(
\Phalcon\Enum::FETCH_BOTH
);
// Return an object
$result->setFetchMode(
\Phalcon\Enum::FETCH_OBJ
);
Interface Phalcon\Db\ResultInterface
| Namespace | Phalcon\Db |
Interface for Phalcon\Db\Result objects
Methods¶
Moves internal resultset cursor to another position letting us to fetch a certain row Allows to execute the statement again. Some database systems don't support scrollable cursors. So, as cursors are forward only, we need to execute the cursor again to fetch rows from the beginning Fetches an array/object of strings that corresponds to the fetched row, or FALSE if there are no more rows. This method is affected by the active fetch flag set usingPhalcon\Db\Result\Pdo::setFetchMode()
Returns an array of arrays containing all the records in the result. This method is affected by the active fetch flag set using Phalcon\Db\Result\Pdo::setFetchMode()
Returns an array of strings that corresponds to the fetched row, or FALSE if there are no more rows. This method is affected by the active fetch flag set using Phalcon\Db\Result\Pdo::setFetchMode()
Gets the internal PDO result object Gets number of rows returned by a resultset Changes the fetching mode affecting Phalcon\Db\Result\Pdo::fetch()