Phalcon 数据库
注意
所有类都以前缀命名Phalcon
Db\Adapter\AbstractAdapter
¶
-
命名空间
Phalcon\Db\Adapter
-
使用
Phalcon\Db\ColumnInterface
Phalcon\Db\DialectInterface
Phalcon\Db\Enum
Phalcon\Db\Exception
Phalcon\Db\Index
Phalcon\Db\IndexInterface
Phalcon\Db\RawValue
Phalcon\Db\Reference
Phalcon\Db\ReferenceInterface
Phalcon\Events\EventsAwareInterface
Phalcon\Events\ManagerInterface
-
继承
-
实现
AdapterInterface
EventsAwareInterface
Phalcon\Db\Adapter 适配器的基类。
此类及其相关类为 Phalcon 框架提供了简单的 SQL 数据库接口。Phalcon\Db 是你用来将 PHP 应用程序连接到关系型数据库管理系统(RDBMS)的基本类。每种品牌的关系型数据库都有不同的适配器类。
此组件旨在执行低级数据库操作。如果希望使用更高级别的抽象与数据库交互,请使用 Phalcon\Mvc\Model。
Phalcon\Db\AbstractDb 是一个抽象类。你只能将其与像 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 co_invoices LIMIT 5"
);
$result->setFetchMode(Enum::FETCH_NUM);
while ($invoice = $result->fetch()) {
print_r($invoice);
}
} catch (Exception $e) {
echo $e->getMessage(), PHP_EOL;
}
属性¶
/**
* Connection ID
*
* @var int
*/
protected static $connectionConsecutive = ;
/**
* Active connection ID
*
* @var int
*/
protected $connectionId;
/**
* Descriptor used to connect to a database
*
* @var array
*/
protected $descriptor;
/**
* Dialect instance
*
* @var object
*/
protected $dialect;
/**
* Name of the dialect used
*
* @var string
*/
protected $dialectType;
/**
* Event Manager
*
* @var ManagerInterface|null
*/
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
*
* @var int
*/
protected $transactionLevel = ;
/**
* Whether the database supports transactions with save points
*
* @var bool
*/
protected $transactionsWithSavepoints = false;
/**
* Type of database system the adapter is used for
*
* @var string
*/
protected $type;
方法¶
Phalcon\Db\Adapter 构造函数 向表中添加一列public function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): bool;
public function addPrimaryKey( string $tableName, string $schemaName, IndexInterface $index ): bool;
public function delete( mixed $table, string $whereCondition = null, array $placeholders = [], array $dataTypes = [] ): 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, array $bindParams = [], array $bindTypes = [] ): 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, array $bindParams = [], array $bindTypes = [] ): 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 如果适配器不支持,则返回 NULL
返回用于连接到活动数据库的描述符 返回内部方言实例 使用的方言名称 返回内部事件管理器 返回嵌套事务要用的保存点名称 对象中的活动 SQL 语句,未替换绑定参数 对象中的当前SQL语句 对象中的当前SQL语句 对象中的活动 SQL 变量 适配器所用于的数据库系统类型public 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;
@废弃 将在下一个版本中移除
生成检查模式.表是否存在的SQL 获取表的创建选项public 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
]
);
警告!如果 $whereCondition 是字符串,则不会进行转义。
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
Db\Adapter\AdapterInterface
¶
-
命名空间
Phalcon\Db\Adapter
-
使用
Phalcon\Db\ColumnInterface
Phalcon\Db\DialectInterface
Phalcon\Db\IndexInterface
Phalcon\Db\RawValue
Phalcon\Db\ReferenceInterface
Phalcon\Db\ResultInterface
-
继承
-
实现
Phalcon\Db 适配器的接口
方法¶
向表中添加一列public function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): bool;
public function addPrimaryKey( string $tableName, string $schemaName, IndexInterface $index ): bool;
public function delete( mixed $table, string $whereCondition = null, array $placeholders = [], array $dataTypes = [] ): 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, array $bindParams = [], array $bindTypes = [] ): bool;
public function fetchAll( string $sqlQuery, int $fetchMode = int, array $bindParams = [], array $bindTypes = [] ): 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, array $bindParams = [], array $bindTypes = [] ): 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 如果适配器不支持,则返回 NULL
返回用于连接到活动数据库的描述符 返回内部方言实例 返回所使用方言的名称 返回内部PDO处理器 返回嵌套事务要用的保存点名称 对象中的活动 SQL 语句,未替换绑定参数 对象中的当前SQL语句 对象中的当前SQL语句 对象中的当前SQL语句 返回适配器所使用的数据库系统类型public 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, array $bindParams = [], array $bindTypes = [] ): ResultInterface | bool;
public function setNestedTransactionsWithSavepoints( bool $nestedTransactionsWithSavepoints ): AdapterInterface;
@废弃 将在下一个版本中移除
生成检查模式.表是否存在的SQL 获取表的创建选项public 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
Db\Adapter\Pdo\AbstractPdo
¶
-
命名空间
Phalcon\Db\Adapter\Pdo
-
使用
Phalcon\Db\Adapter\AbstractAdapter
Phalcon\Db\Column
Phalcon\Db\Exception
Phalcon\Db\ResultInterface
Phalcon\Db\Result\PdoResult
Phalcon\Events\ManagerInterface
-
继承
AbstractAdapter
-
实现
Phalcon\Db\Adapter\Pdo 是 Phalcon\Db 类,内部使用 PDO 连接到数据库
use Phalcon\Db\Adapter\Pdo\Mysql;
$config = [
"host" => "localhost",
"dbname" => "blog",
"port" => 3306,
"username" => "sigma",
"password" => "secret",
];
$connection = new Mysql($config);
属性¶
/**
* Last affected rows
*
* @var int
*/
protected $affectedRows = ;
/**
* PDO Handler
*
* @var \PDO
*/
protected $pdo;
方法¶
Phalcon\Db\Adapter\Pdo 的构造函数 返回数据库系统中最后执行的 INSERT/UPDATE/DELETE 影响的行数 在连接中启动一个事务 关闭活动连接并返回成功状态。Phalcon 在请求结束时自动关闭并销毁活动连接 提交连接中的活动事务 此方法在 \Phalcon\Db\Adapter\Pdo 构造函数中自动调用。当需要恢复数据库连接时调用它。
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, array $bindParams = [], array $bindTypes = [] ): 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, array $bindParams = [], array $bindTypes = [] ): ResultInterface | bool;
// Querying data
$resultset = $connection->query(
"SELECTFROM robots WHERE type = 'mechanical'"
);
$resultset = $connection->query(
"SELECTFROM robots WHERE type = ?",
[
"mechanical",
]
);
@参见 https://stackoverflow.com/a/8403150
Db\Adapter\Pdo\Mysql¶
-
命名空间
Phalcon\Db\Adapter\Pdo
-
使用
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
-
继承
PdoAdapter
-
实现
MySQL数据库系统的特定函数
use Phalcon\Db\Adapter\Pdo\Mysql;
$config = [
"host" => "localhost",
"dbname" => "blog",
"port" => 3306,
"username" => "sigma",
"password" => "secret",
];
$connection = new Mysql($config);
属性¶
方法¶
public function addForeignKey( string $tableName, string $schemaName, ReferenceInterface $reference ): bool;
Db\Adapter\Pdo\Postgresql¶
-
命名空间
Phalcon\Db\Adapter\Pdo
-
使用
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
-
继承
PdoAdapter
-
实现
PostgreSQL数据库系统的特定函数
use Phalcon\Db\Adapter\Pdo\Postgresql;
$config = [
"host" => "localhost",
"dbname" => "blog",
"port" => 5432,
"username" => "postgres",
"password" => "secret",
];
$connection = new Postgresql($config);
属性¶
/**
* @var string
*/
protected $dialectType = postgresql;
/**
* @var string
*/
protected $type = pgsql;
方法¶
Phalcon\Db\Adapter\Pdo\Postgresql的构造函数 此方法在Phalcon\Db\Adapter\Pdo构造函数中自动调用。当需要恢复数据库连接时请调用它 创建一个表 返回描述表的 Phalcon\Db\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;
Db\Adapter\Pdo\Sqlite¶
-
命名空间
Phalcon\Db\Adapter\Pdo
-
使用
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
-
继承
PdoAdapter
-
实现
SQLite数据库系统的特定函数
属性¶
/**
* @var string
*/
protected $dialectType = sqlite;
/**
* @var string
*/
protected $type = sqlite;
方法¶
Phalcon\Db\Adapter\Pdo\Sqlite的构造函数 此方法在Phalcon\Db\Adapter\Pdo构造函数中自动调用。当需要恢复数据库连接时请调用它 返回描述表的 Phalcon\Db\Column 对象数组 列出表的索引 列出表的引用 返回让RDBMS使用表定义中声明的默认值的默认值// Inserting a new robot with a valid default value for the column 'year'
$success = $connection->insert(
"robots",
[
"Astro Boy",
$connection->getDefaultValue(),
],
[
"name",
"year",
]
);
@废弃 将在下一个版本中移除
检查数据库系统是否需要为标识列提供显式值 以键值对的形式返回PDO适配器的DSN默认值Db\Adapter\PdoFactory¶
-
命名空间
Phalcon\Db\Adapter
-
使用
Phalcon\Factory\AbstractFactory
Phalcon\Support\Helper\Arr\Get
-
继承
AbstractFactory
-
实现
该文件是Phalcon框架的一部分。
(c) Phalcon团队team@phalcon.io
有关完整的版权和许可信息,请查看随此源代码分发的LICENSE.txt文件。
方法¶
构造函数 从Config对象创建实例的工厂 创建适配器的新实例 返回可用的适配器Db\Column¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
ColumnInterface
允许定义在创建或修改表操作中使用的列
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);
常量¶
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_BINARY = 27;
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_VARBINARY = 28;
const TYPE_VARCHAR = 2;
属性¶
/**
* Column Position
*
* @var string|null
*/
protected $after;
/**
* Column is autoIncrement?
*
* @var bool
*/
protected $autoIncrement = false;
/**
* Bind Type
*
* @var int
*/
protected $bindType = 2;
/**
* Column's comment
*
* @var string|null
*/
protected $comment;
/**
* Default column value
*
* @var mixed|null
*/
protected $defaultValue;
/**
* Position is first
*
* @var bool
*/
protected $first = false;
/**
* The column have some numeric type?
*
* @var bool
*/
protected $isNumeric = false;
/**
* Column's name
*
* @var string
*/
protected $name;
/**
* Column not nullable?
*
* Default SQL definition is NOT NULL.
*
* @var bool
*/
protected $notNull = true;
/**
* Column is part of the primary key?
*
* @var bool
*/
protected $primary = false;
/**
* Integer column number scale
*
* @var int
*/
protected $scale = ;
/**
* Integer column size
*
* @var int|string
*/
protected $size = ;
/**
* 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;
方法¶
Phalcon\Db\Column的构造函数 检查字段是否绝对位于表的位置 返回绑定处理的类型 列的注释 列的默认值 列的名称 整数列的小数位数 整数列的大小 列的数据类型 列的数据类型引用 列的数据类型值 检查列是否有默认值 自增 检查列是否位于表的第一个位置 非空 检查列是否为数字类型 列是主键的一部分吗? 如果数值列是无符号的则返回trueDb\ColumnInterface
¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
Phalcon\Db\Column的接口
方法¶
检查字段是否绝对位于表的位置 返回绑定处理的类型 返回列的默认值 返回列名 返回列的小数位数 返回列的大小 返回列的类型 返回列的类型引用 返回列的类型值 检查列是否有默认值 自增 检查列是否位于表的第一个位置 非空 检查列是否为数字类型 列是主键的一部分吗? 如果数值列是无符号的则返回trueDb\Dialect
¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
DialectInterface
这是每个数据库方言的基类。它实现了将中间代码转换为其相关RDBMS语法的通用方法
属性¶
方法¶
生成创建新保存点的SQL 转义标识符 转义模式 返回带有FOR UPDATE子句修改后的SQLfinal public function getColumnList( array $columnList, string $escapeChar = null, array $bindCounts = [] ): string;
final public function getSqlColumn( mixed $column, string $escapeChar = null, array $bindCounts = [] ): string;
public function getSqlExpression( array $expression, string $escapeChar = null, array $bindCounts = [] ): 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, array $bindCounts = [] ): string;
final protected function getSqlExpressionCase( array $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionCastValue( array $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionConvertValue( array $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionFrom( mixed $expression, string $escapeChar = null ): string;
final protected function getSqlExpressionFunctionCall( array $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionGroupBy( mixed $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionHaving( array $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionJoins( mixed $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionLimit( mixed $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionList( array $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionObject( array $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionOrderBy( mixed $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionQualified( array $expression, string $escapeChar = null ): string;
final protected function getSqlExpressionScalar( array $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionUnaryOperations( array $expression, string $escapeChar = null, array $bindCounts = [] ): string;
final protected function getSqlExpressionWhere( mixed $expression, string $escapeChar = null, array $bindCounts = [] ): 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;
Db\Dialect\Mysql¶
-
命名空间
Phalcon\Db\Dialect
-
使用
Phalcon\Db\Column
Phalcon\Db\ColumnInterface
Phalcon\Db\Dialect
Phalcon\Db\DialectInterface
Phalcon\Db\Exception
Phalcon\Db\IndexInterface
Phalcon\Db\ReferenceInterface
-
继承
Dialect
-
实现
为MySQL RDBMS生成特定数据库的SQL
属性¶
方法¶
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
Db\Dialect\Postgresql¶
-
命名空间
Phalcon\Db\Dialect
-
使用
Phalcon\Db\Column
Phalcon\Db\ColumnInterface
Phalcon\Db\Dialect
Phalcon\Db\DialectInterface
Phalcon\Db\Exception
Phalcon\Db\IndexInterface
Phalcon\Db\ReferenceInterface
-
继承
Dialect
-
实现
为PostgreSQL RDBMS生成特定数据库的SQL
属性¶
方法¶
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;
Db\Dialect\Postgresql¶
-
命名空间
Phalcon\Db\Dialect
-
使用
Phalcon\Db\Column
Phalcon\Db\ColumnInterface
Phalcon\Db\Dialect
Phalcon\Db\DialectInterface
Phalcon\Db\Exception
Phalcon\Db\IndexInterface
Phalcon\Db\ReferenceInterface
-
继承
Dialect
-
实现
为PostgreSQL RDBMS生成特定于数据库的SQL
属性¶
方法¶
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;
Db\DialectInterface
¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
Phalcon\Db方言的接口
方法¶
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, array $bindCounts = [] ): string;
public function modifyColumn( string $tableName, string $schemaName, ColumnInterface $column, ColumnInterface $currentColumn = null ): string;
Db\Enum¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
Phalcon\Db的常量
常量¶
const FETCH_ASSOC;
const FETCH_BOTH;
const FETCH_BOUND;
const FETCH_CLASS;
const FETCH_CLASSTYPE;
const FETCH_COLUMN;
const FETCH_DEFAULT = 0;
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_ORI_NEXT;
const FETCH_PROPS_LATE;
const FETCH_SERIALIZE;
const FETCH_UNIQUE;
Db\Exception¶
-
命名空间
Phalcon\Db
-
使用
-
继承
\Exception
-
实现
Phalcon\Db抛出的异常将使用此类
Db\Index¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
IndexInterface
允许定义用于表的索引。索引是提高数据库性能的一种常见方式。索引允许数据库服务器比没有索引时更快地查找和检索特定行
// 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);
属性¶
/**
* Index columns
*
* @var array
*/
protected $columns;
/**
* Index name
*
* @var string
*/
protected $name;
/**
* Index type
*
* @var string
*/
protected $type;
方法¶
Phalcon\Db\Index构造函数 索引列 索引名称 索引类型Db\IndexInterface
¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
Phalcon\Db\Index的接口
方法¶
获取对应索引的列 获取索引名称 获取索引类型Db\Profiler¶
-
命名空间
Phalcon\Db
-
使用
Phalcon\Db\Profiler\Item
-
继承
-
实现
Phalcon\Db的实例可以在发送到关系型数据库的SQL语句上生成执行分析。分析的信息包括以毫秒为单位的执行时间。这有助于您识别应用程序中的瓶颈。
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";
属性¶
/**
* Active Item
*
* @var Item
*/
protected $activeProfile;
/**
* All the Items in the active profile
*
* @var Item[]
*/
protected $allProfiles;
/**
* Total time spent by all profiles to complete in nanoseconds
*
* @var float
*/
protected $totalNanoseconds = ;
方法¶
返回在分析器中执行的最后一个分析 返回已处理的SQL语句总数 返回所有已处理的分析记录 返回分析所花费的总时间(毫秒) 返回分析所花费的总时间(纳秒) 返回分析所花费的总时间(秒) 重置分析器,清除所有分析记录public function startProfile( string $sqlStatement, array $sqlVariables = [], array $sqlBindTypes = [] ): Profiler;
Db\Profiler\Item¶
-
命名空间
Phalcon\Db\Profiler
-
使用
-
继承
-
实现
此类标识Phalcon\Db\Profiler中的每个分析记录
属性¶
/**
* 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;
方法¶
返回配置文件结束时的时间戳 返回配置文件开始时的时间戳 返回与配置文件相关的SQL绑定类型 返回与配置文件相关的SQL语句 返回与配置文件相关的SQL变量 返回配置文件所花费的总时间(以毫秒为单位) 返回配置文件所花费的总时间(以纳秒为单位) 返回配置文件所花费的总时间(以秒为单位) 返回配置文件结束时的时间戳 返回配置文件开始时的时间戳 返回与配置文件相关的SQL绑定类型 返回与配置文件相关的SQL语句 返回与配置文件相关的SQL变量数据库\原始值¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
此类允许插入/更新原始数据而不进行引号或格式化。
下一个示例演示如何将MySQL的now()函数用作字段值。
$subscriber = new Subscribers();
$subscriber->email = "andres@phalcon.io";
$subscriber->createdAt = new \Phalcon\Db\RawValue("now()");
$subscriber->save();
属性¶
方法¶
Phalcon\Db\RawValue构造函数数据库\外键约束¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
ReferenceInterface
允许在表上定义引用约束
$reference = new \Phalcon\Db\Reference(
"field_fk",
[
"referencedSchema" => "invoicing",
"referencedTable" => "products",
"columns" => [
"producttype",
"product_code",
],
"referencedColumns" => [
"type",
"code",
],
]
);
属性¶
/**
* 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;
方法¶
Phalcon\Db\Reference构造函数 本地引用列 约束名称 ON DELETE ON UPDATE 被引用列 被引用模式 被引用表 模式名称数据库\引用接口
¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
Phalcon\Db\Reference接口
方法¶
获取引用所基于的本地列 获取索引名称 获取被引用的ON DELETE 获取被引用的ON UPDATE 获取被引用列 获取被引用表所在的模式 获取被引用表 获取被引用表所在的模式数据库\结果\PDO结果¶
-
命名空间
Phalcon\Db\Result
-
使用
Phalcon\Db\Adapter\AdapterInterface
Phalcon\Db\Enum
Phalcon\Db\ResultInterface
-
继承
-
实现
ResultInterface
封装结果集内部
$result = $connection->query("SELECTFROM robots ORDER BY name");
$result->setFetchMode(
\Phalcon\Db\Enum::FETCH_NUM
);
while ($robot = $result->fetchArray()) {
print_r($robot);
}
属性¶
/**
* @var array
*/
protected $bindParams;
/**
* @var array
*/
protected $bindTypes;
/**
* @var AdapterInterface
*/
protected $connection;
/**
* Active fetch mode
*
* @var int
*/
protected $fetchMode;
/**
* Internal resultset
*
* @var \PDOStatement
*/
protected $pdoStatement;
/**
* @var mixed
* TODO: Check if this property is used
*/
protected $result;
/**
* @var bool
*/
protected $rowCount = false;
/**
* @var string|null
*/
protected $sqlStatement;
方法¶
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( int $fetchStyle = null, int $cursorOrientation = static-constant-access, int $cursorOffset = int );
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( int $mode = static-constant-access, mixed $fetchArgument = static-constant-access, mixed $constructorArgs = null ): array;
Phalcon\Db\Result\Pdo::setFetchMode()
返回对应于已获取行的字符串数组,如果没有更多行,则返回FALSE。此方法受到使用设置的活动获取标志的影响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
);
数据库\结果接口
¶
-
命名空间
Phalcon\Db
-
使用
-
继承
-
实现
Phalcon\Db\Result对象的接口
方法¶
将内部结果集游标移动到另一个位置,使我们能够获取某个行 允许再次执行语句。某些数据库系统不支持可滚动游标。因此由于游标只能向前移动,我们需要再次执行游标以从头开始获取行 获取对应于已获取行的字符串数组/对象,如果没有更多行,则返回FALSE。此方法受到使用设置的活动获取标志的影响Phalcon\Db\Result\Pdo::setFetchMode()
返回包含结果中的所有记录的数组的数组。此方法受到使用设置的活动获取标志的影响Phalcon\Db\Result\Pdo::setFetchMode()
返回对应于已获取行的字符串数组,如果没有更多行,则返回FALSE。此方法受到使用设置的活动获取标志的影响Phalcon\Db\Result\Pdo::setFetchMode()
获取内部PDO结果对象 获取结果集返回的行数 更改获取模式影响Phalcon\Db\Result\Pdo::fetch()