跳转到内容

数据映射器


注意

这些组件受到了以下项目的重大影响:Aura PHPAtlas PHP

注意

DataMapper 的完整实现尚未完成。但有一些组件可以在任何项目中使用,例如ConnectionQuery/Select

概览

按照Martin Fowler的调用企业应用架构设计模式的表生成模型的最简单方法是:

注意

Mapper 层负责在数据库与对象之间移动数据,并保持它们相互独立,同时也不依赖于 Mapper 本身。

The Phalcon\DataMapper命名空间包含用于访问数据源的组件,其中使用了数据映射器.

PDO

连接

此实现所需的一个组件是 PDO 连接器。Phalcon 提供了一个包装 PHP PDO 实现的类,Phalcon\DataMapper\Pdo\Connection使维护连接变得更加简单。

连接到一个数据源

连接到数据库需要 DSN 字符串以及具有访问权限账户的用户名和密码。

DSN 格式如下:

引擎 DSN
MySQL mysql:host=<host>;dbname=<database name>;charset=<charset>;port=<port>
Postgresql pgsql:host=<host>;dbname=<database name>
Sqlite sqlite:<file>

只需要将<>中的值替换为你的环境对应的实际值即可。对于charsetport是可选的参数。Mysql对于Sqlite可以使用memory作为<file>但此时数据库不会持久化存储。如果提供一个合适的文件路径,则会为Sqlite.

<?php

use Phalcon\DataMapper\Pdo\Connection;

$host     = '127.0.0.1';
$database = 'phalon_test';
$charset  = 'utf8mb4';
$port     = 3306;
$username = 'phalcon';
$password = 'secret';

$dsn = sprintf(
    "mysql:host=%s;dbname=%s;charset=%s;port=%s",
    $host,
    $database,
    $charset,
    $port
);

$connection = new Connection($dsn, $username, $password);

$sql = '
    SELECT 
        inv_id, 
        inv_title 
    FROM 
        co_invoices 
    WHERE 
        inv_cst_id = :cst_id
';

$bind = [
    'cst_id' => 1
];

$result = $connection->fetchAll($statement, $bind);

方法

public function __construct(
    string $dsn,
    string $username = null,
    string $password = null,
    array $options = [],
    array $queries = [],
    ProfilerInterface $profiler = null
)
构造对象。$dsn, $username$password用于连接数据源。$options支持额外的PDO参数设置。$queries数组包含了一组当连接建立后要执行的查询。$profiler是一个可选的对象,需实现ProfilerInterface接口,用于对连接进行性能分析。

public function __debugInfo():  array
此方法的作用是隐藏堆栈跟踪中的敏感信息(如用户名、密码)。

public function beginTransaction(): bool
开始一个事务。如果启用了性能分析器,操作将被记录。

public function commit(): bool
提交当前事务。如果启用了性能分析器,操作将被记录。

abstract public function connect(): void;
连接到数据库。

abstract public function disconnect(): void;
从数据库断开连接。

public function errorCode(): string | null
获取最近的错误代码。

public function errorInfo(): array
获取最近的错误信息。

public function exec(string $statement): int
执行一条 SQL 语句并返回受影响的行数。如果启用了性能分析器,操作将被记录。

public function fetchAffected(string $statement, array $values = []): int
执行一个语句并返回受影响的行数。

public function fetchAll(string $statement, array $values = []): array
从数据库获取一连串的行;这些行以关联数组的形式返回。

public function fetchAssoc(string $statement, array $values = []): array
从数据库中获取一个关联数组的行;这些行作为关联数组返回,并且行的数组以每行的第一个列为键。

如果多行具有相同的第一个列值,则具有该值的最后一行将覆盖之前的行。此方法资源消耗更大,应尽可能避免使用。

public function fetchColumn(
    string $statement,
    array $values = [],
    int $column = 0
): array
将一列数据作为顺序数组(默认为第一列)进行获取。

public function fetchGroup(
    string $statement,
    array $values = [],
    int $flags = \PDO::FETCH_ASSOC
): array 
从数据库中获取多个记录并作为关联数组返回。第一列将作为索引键。默认标志位为PDO::FETCH_ASSOC | PDO::FETCH_GROUP

public function fetchObject(
    string $statement,
    array $values = [],
    string $className = "stdClass",
    array $arguments = []
): object 
从数据库中获取一行并作为对象返回,其中列的值映射到对象属性。

由于 PDO 在调用构造函数之前会注入属性值,因此如果你的对象构造函数中存在任何可能的默认初始化,将会覆盖由 PDO 注入的值。fetchObject返回的默认对象是\stdClass

public function fetchObjects(
    string $statement,
    array $values = [],
    string $className = "stdClass",
    array $arguments = []
): array {
从数据库中获取一个顺序数组的行;每一行作为对象返回,列的值被映射到对象属性。

由于 PDO 在调用构造函数之前会注入属性值,因此如果你的对象构造函数中存在任何可能的默认初始化,将会覆盖由 PDO 注入的值。fetchObject返回的默认对象是\stdClass

public function fetchOne(string $statement, array $values = []): array
从数据库中获取一行并作为关联数组返回。

public function fetchPairs(string $statement, array $values = []): array
将行作为一个键值对的关联数组来获取(第一列是键,第二列是值)。

public function fetchValue(string $statement, array $values = [])
获取第一个值(即第一行的第一列)。

public function getAdapter(): \PDO
返回内部的 PDO(如果存在的话)。

public function getAttribute(int $attribute): var
检索数据库连接属性。

public static function getAvailableDrivers(): array
返回可用 PDO 驱动程序的数组(如果没有可用驱动程序则返回空数组)。

public function getDriverName(): string
返回驱动名称

public function getProfiler(): <ProfilerInterface>
返回 Profiler 实例。

public function getQuoteNames(string $driver = ""): array
根据驱动获取引用参数

public function inTransaction(): bool
当前是否有活跃的事务?如果启用了性能分析器,操作将被记录。如果启用了性能分析器,操作将被记录。

public function isConnected(): bool
PDO 连接是否处于活动状态?

public function lastInsertId(string $name = null): string
返回最后插入的自增序列值。如果启用了性能分析器,操作将被记录。

public function perform(
    string $statement,
    array $values = []
): \PDOStatement
执行带绑定值的查询,并返回结果 PDOStatement;数组 $values 将通过quote()它们的相应占位符将在查询字符串中替换。如果启用了性能分析器,操作将被记录。

public function prepare(
    string $statement,
    array $options = []
): \PDOStatement
准备一个 SQL 语句用于执行。

public function query(string $statement, ...$fetch): <\PDOStatement> | bool
查询数据库并返回一个 PDOStatement。如果启用了性能分析器,操作将被记录。

public function quote(mixed $value, int $type = \PDO::PARAM_STR): string
对一个值进行引用处理,以便用于 SQL 语句中。这与PDO::quote()不同之处在于它会将数组转换为逗号分隔的带引用的字符串。默认类型为PDO::PARAM_STR

public function rollBack(): bool
回滚当前事务,并恢复自动提交模式。如果启用了性能分析器,操作将被记录。

public function setAttribute(int $attribute, mixed $value): bool
设置数据库连接属性。

public function setProfiler(ProfilerInterface $profiler)
设置 Profiler 实例。

protected function fetchData(
    string $method,
    array $arguments,
    string $statement,
    array $values = []
): array
基于传递的方法获取 PDO 数据的帮助方法

protected function performBind(
    \PDOStatement $statement,
    mixed $name,
    mixed $arguments
): void
使用正确的PDO::PARAM_*类型来绑定值。

连接 - 装饰模式

连接定位器

高流量的应用程序可能使用多个数据库服务器。例如,可以为主写操作配置高性能数据库服务器,而读取操作则使用基于内存的小型数据库服务器。

The Phalcon\DataMapper\ConnectionLocator允许你定义多个Phalcon\DataMapper\Pdo\Connection对象用于读取和写入。所有这些对象都是懒加载的,只有在必要时才会实例化。

实例化

创建Phalcon\DataMapper\ConnectionLocator最简单的方法是实例化它并传入一个Phalcon\DataMapper\Pdo\Connection对象。此外,构造函数还可以选择性地接收两个数组,一个用于写入连接,另一个用于读取连接。第一个连接始终是主连接。一旦master容器消耗更多内存。

$host     = '127.0.0.1';
$database = 'phalon_test';
$charset  = 'utf8mb4';
$port     = 3306;
$username = 'phalcon';
$password = 'secret';

$dsn = sprintf(
    "mysql:host=%s;dbname=%s;charset=%s;port=%s",
    $host,
    $database,
    $charset,
    $port
);

$connection = new Connection($dsn, $username, $password);

$locator = new ConnectionLocator($connection);

方法

public function __construct(
    ConnectionInterface $master,
    array $read = [],
    array $write = []
)
构造函数。

public function getMaster():  ConnectionInterface
返回默认的连接对象。

public function getRead(string $name = ""):  ConnectionInterface
按名称返回一个读连接;若未指定名称,则随机选取一个连接;若没有读连接,则返回默认连接。

public function getWrite(string $name = ""):  ConnectionInterface
按名称返回一个写连接;若未指定名称,则随机选取一个连接;若没有写连接,则返回默认连接。

public function setMaster(ConnectionInterface $callableObject):  ConnectionLocatorInterface
设置默认连接工厂。

public function setRead(
    string $name,
    callable $callableObject
):  ConnectionLocatorInterface
按名称设置一个读连接工厂。

public function setWrite(
    string $name,
    callable $callableObject
): ConnectionLocatorInterface
按名称设置一个写连接工厂。

protected function getConnection(
    string $type,
    string $name = ""
):  ConnectionInterface
按名称返回一个连接。

配置

上的同名属性Phalcon\DataMapper\ConnectionLocator创建完成,你可以添加任意多的读或写连接。可以在定位器创建期间或运行时添加。

运行时

首先,使用主连接创建Phalcon\DataMapper\ConnectionLocator对象。主连接是在没有定义读或写连接时使用的默认连接。

<?php

$locator = new ConnectionLocator(
    function () use ($options) {
        return new Connection(
            'mysql:host=10.4.6.1;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    }
);

现在你可以根据需求添加任意多个读服务器或写服务器。

<?php

// Write: master
$locator->addRead(
    'master',
    function () {
        return new Connection(
            'mysql:host=10.4.4.1;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    }
);

// Read: slave01
$locator->addRead(
    'slave01',
    function () {
        return new Connection(
            'mysql:host=10.4.8.1;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    }
);

// Read: slave02
$locator->addRead(
    'slave02',
    function () {
        return new Connection(
            'mysql:host=10.4.8.2;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    }
);

// Read: slave03
$locator->addRead(
    'slave03',
    function () {
        return new Connection(
            'mysql:host=10.4.8.3;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    }
);

构造时

也可以在定位器构造时就完成所有配置。这在将定位器设置为 DI 容器中的服务时特别有用。

<?php

// Set up write connections
$write = [
    'master' => function () {
        return new Connection(
            'mysql:host=10.4.4.1;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    }
];

// Set up read connections
$read = [
    'slave01' => function () {
        return new Connection(
            'mysql:host=10.4.8.1;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    },
    'slave02' => function () {
        return new Connection(
            'mysql:host=10.4.8.2;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    },
    'slave03' => function () {
        return new Connection(
            'mysql:host=10.4.8.3;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    }
];

$locator = new ConnectionLocator(
    function () use ($options) {
        return new Connection(
            'mysql:host=10.4.6.1;dbname=phalcon_db;charset=utf8mb4;port=3306',
            'username', 
            'password'
        );
    },
    $read,
    $write
);

获取连接

从定位器中获取连接时,如果尚未实例化,则会自动创建并返回。

你可以通过传递注册时使用的名称来获取特定的读或写连接。getRead()getWrite()方法。

分析器

The Phalcon\DataMapper\Profiler\Profiler是一个允许你对数据库连接进行性能分析的组件。这意味着它可以记录哪些查询被执行过、来源于代码库的何处,以及其执行耗时是多少。Phalcon\DataMapper\Profiler\Profiler接收一个对象来将所有收集到的信息记录到文件中。默认情况下,Phalcon\Logger\Logger使用的是Phalcon\DataMapper\Profiler\MemoryLogger

The Phalcon\DataMapper\Profiler\Profiler可以通过调用setActive()方法激活。该方法接收一个布尔标志,同时也作为停用方法使用。只有在分析器处于活动状态时才会记录数据。

<?php

use Phalcon\DataMapper\Pdo\Connection;
use Phalcon\DataMapper\Profiler\MemoryLogger;
use Phalcon\DataMapper\Profiler\Profiler;

$host     = '127.0.0.1';
$database = 'phalon_test';
$charset  = 'utf8mb4';
$port     = 3306;
$username = 'phalcon';
$password = 'secret';

$dsn = sprintf(
    "mysql:host=%s;dbname=%s;charset=%s;port=%s",
    $host,
    $database,
    $charset,
    $port
);

$profiler   = new Profiler(new MemoryLogger());
$connection = new Connection(
    $dsn, 
    $username, 
    $password,
    [
        PDO::ATTR_EMULATE_PREPARES => true, // PDO options
    ],
    [
        'SET NAMES utf8mb4', // startup queries
    ],
    $profiler
);

// Same profiler as the one we created above
$profiler = $connection->getProfiler();
$profiler->setActive(true)
以及获取存储的数据:

<?php

$data = $connection->getProfiler()->getLogger()->getMessages();

var_dump($messages);

消息默认按照以下模式进行记录:

"{method} ({duration}s): {statement} {backtrace}"

您可以使用setLogFormat()来自定义消息格式

<?php

$connection
    ->getProfiler()
    ->setLogFormat("{duration}: {method} {statement}{values}")

分析器上的可用参数包括:

参数 描述
{backtrace} 查询执行位置的调用堆栈
{duration} 查询的执行时间
{finish} 分析结束时的微秒时间戳
{method} 调用了连接的方法
{start} 分析开始时的微秒时间戳
{statement} 执行的查询
{values} 传递给查询的任何值

注意

参数必须用花括号包裹起来{}

Query

工厂

The Phalcon\DataMapper\Query命名空间提供了一个便捷的工厂类,这使得无论是生成select, insert, update还是`delete语句都能快速简便地创建查询对象。该Phalcon\DataMapper\Query\QueryFactory的方法接收一个Phalcon\DataMapper\Pdo\Connection,将生成的对象与该连接绑定。

方法

public function __construct(string selectClass = "")
QueryFactory 构造函数。还可以选择性接收一个可以在Select语句中使用的类名。默认情况下,它使用的是Phalcon\DataMapper\Query\Select.

public function newBind(): Bind
创建一个新的 Bind 对象

public function newDelete(Connection $connection): Delete
创建一个新的 Delete 对象

public function newInsert(Connection $connection): Insert
创建一个新的 Insert 对象

public function newSelect(Connection $connection): Select
创建一个新的 Select 对象

public function newUpdate(Connection $connection): Update
创建一个新的 Update 对象

示例

<?php

use Phalcon\DataMapper\Pdo\Connection;
use Phalcon\DataMapper\Query\QueryFactory;

$host     = '127.0.0.1';
$database = 'phalon_test';
$charset  = 'utf8mb4';
$port     = 3306;
$username = 'phalcon';
$password = 'secret';

$dsn = sprintf(
    "mysql:host=%s;dbname=%s;charset=%s;port=%s",
    $host,
    $database,
    $charset,
    $port
);

$connection = new Connection($dsn, $username, $password);
$factory    = new QueryFactory();
$select     = $factory->newSelect($connection);

删除

方法

public function __construct(Connection $connection, Bind $bind)
Delete 构造函数。

public function andWhere(
    string $condition, 
    mixed $value = null, 
    int $type = -1
): Delete
设置一个ANDWHERE条件

public function appendWhere(
    string $condition, 
    mixed $value = null, 
    int $type = -1
): Delete
追加到最近的WHERE子句

public function bindInline(mixed $value, int $type = -1): string
内联绑定一个值

public function bindValue(string key, mixed $value, int $type = -1): Delete
绑定一个值 - 如有需要自动检测类型

public function bindValues(array values): Delete
绑定一组值

public function from(string table): Delete
在查询中添加表

public function getBindValues(): array
返回所有已绑定的值

public function getStatement(): string
@return 字符串

public function limit(int $limit): Delete
设置表单的LIMIT子句

public function offset(int $offset): Delete
设置表单的OFFSET子句

public function orderBy(var $orderBy): Delete
设置表单的ORDER BY

public function orWhere(
    string $condition, 
    mixed $value = null, 
    int $type = -1
): Delete
设置一个ORWHERE条件

public function perform()
在连接中执行一条语句

public function quoteIdentifier(
    string $name, 
    int $type = \PDO::PARAM_STR
): string 
引号标识符

public function reset(): Delete
重置内部数组

public function resetColumns(): Delete
重置列

public function resetFlags(): Delete
重置标志

public function resetFrom(): Delete
重置 from

public function resetGroupBy(): Delete
重置 group by

public function resetHaving(): Delete
重置 having

public function resetLimit(): Delete
重置 limit 和 offset

public function resetOrderBy(): Delete
重置 order by

public function resetWhere(): Delete
重置 where

public function returning(array $columns): Delete
添加RETURNING子句

public function setFlag(string $flag, bool $enable = true): void
设置查询标志,例如 "DISTINCT"

public function where(
    string $condition, 
    mixed $value = null, 
    int $type = -1
): Delete
设置一个WHERE条件

public function whereEquals(array $columnsValues): Delete
sw

protected function addCondition(
    string $store, 
    string $andor, 
    string $condition, 
    mixed $value = null, 
    int $type = -1
): void 
追加一个条件

protected function appendCondition(
    string $store, 
    string $condition, 
    mixed $value = null, 
    int $type = -1
): void 
连接一个条件

protected function buildBy(string $type): string
构造一个BY列表

protected function buildCondition(string $type): string
构造条件字符串

protected function buildFlags()
构造标志语句

protected function buildLimitEarly(): string
构造早期的LIMIT子句 - MS SQLServer

protected function buildLimit(): string
构造LIMIT子句

protected function buildLimitCommon(): string
构造LIMIT子句(适用于所有驱动程序)

protected function buildLimitSqlsrv(): string
构造LIMIT子句(适用于 MSSQLServer)

protected function buildReturning(): string
构造RETURNING子句

protected function indent(array $collection, string $glue = ""): string
缩进一个集合

protected function processValue(string $store, mixed $data): void
处理一个值(数组或字符串)并将其与存储合并

激活

要实例化一个Phalcon\DataMapper\Query\Delete查询构造器,你可以使用Phalcon\DataMapper\Query\QueryFactory结合一个Phalcon\DataMapper\Pdo\Connection.

<?php

use Phalcon\DataMapper\Pdo\Connection;
use Phalcon\DataMapper\Query\QueryFactory;

$host     = '127.0.0.1';
$database = 'phalon_test';
$charset  = 'utf8mb4';
$port     = 3306;
$username = 'phalcon';
$password = 'secret';

$dsn = sprintf(
    "mysql:host=%s;dbname=%s;charset=%s;port=%s",
    $host,
    $database,
    $charset,
    $port
);

$connection = new Connection($dsn, $username, $password);
$factory    = new QueryFactory();
$delete     = $factory->newDelete($connection);

构建

The from()方法用于指定要删除数据的表。

$delete
    ->from('co_invoices')
; 

$delete->perform();
// DELETE 
// FROM co_invoices
WHERE

The where()方法用于为DELETE语句指定条件。

$delete
    ->from('co_invoices')
    ->where('inv_cst_id = ', 1)
; 

$delete->perform();

// DELETE
// FROM co_invoices
// WHERE inv_cst_id = 1
ORDER BY

某些数据库(尤其是MySQL)支持在删除操作中使用ORDER BY。你可以使用orderBy()方法来指定它。

$delete
    ->from('co_invoices')
    ->where('inv_cst_id = ', 1)
    ->orderBy('inv_id')
; 

$delete->perform();

// DELETE
// FROM co_invoices
// WHERE inv_cst_id = 1
// ORDER BY inv_id
LIMIT/OFFSET

某些数据库(MySQL、SQLite)支持LIMIT和/或OFFSET子句。你可以使用limit()offset()方法来指定它们。

$delete
    ->from('co_invoices')
    ->where('inv_cst_id = ', 1)
    ->orderBy('inv_id')
    ->limit(10)
    ->offset(40)
; 

$delete->perform();

// DELETE
// FROM co_invoices
// WHERE inv_cst_id = 1
// ORDER BY inv_id
// LIMIT 10 OFFSET 40
RETURNING

一些数据库(尤其是PostgreSQL)支持RETURNING子句。你可以使用returning()方法来指定它。

$delete
    ->from('co_invoices')
    ->where('inv_cst_id = ', 1)
    ->orderBy('inv_id')
    ->limit(10)
    ->offset(40)
    ->returning(['inv_id', 'inv_cst_id'])
; 

$delete->perform();

// DELETE
// FROM co_invoices
// WHERE inv_cst_id = 1
// ORDER BY inv_id
// LIMIT 10 OFFSET 40
// RETURNING inv_id, inv_cst_id
标志

你可以使用setFlag()方法设置你的数据库服务器识别的标志。例如,你可以设置一个MySQLLOW_PRIORITY标志,如下所示:

$delete
    ->from('co_invoices')
    ->where('inv_cst_id = ', 1)
    ->orderBy('inv_id')
    ->limit(10)
    ->offset(40)
    ->returning(['inv_id', 'inv_cst_id'])
    ->setFlag('LOW_PRIORITY')
; 

$delete->perform();

// DELETE LOW_PRIORITY
// FROM co_invoices
// WHERE inv_cst_id = 1
// ORDER BY inv_id
// LIMIT 10 OFFSET 40
// RETURNING inv_id, inv_cst_id

插入

方法

public function __construct(Connection $connection, Bind $bind)
Insert 构造函数。

public function bindInline(mixed $value, int $type = -1): string
内联绑定一个值

public function bindValue(string $key, mixed $value, int $type = -1): Insert
绑定一个值 - 如有需要自动检测类型

public function bindValues(array $values): Insert
绑定一组值

public function column(string $column, mixed $value = null, int $type = -1): Insert
设置一个列用于INSERT查询

public function columns(array $columns): Insert
批量设置列和值用于INSERT

public function getBindValues(): array
返回所有已绑定的值

public function getLastInsertId(string $name = null): string
返回最后一次插入记录的 ID

public function getStatement(): string
返回生成的语句

public function into(string $table): Insert
在查询中添加表

public function perform()
在连接中执行一条语句

public function quoteIdentifier(string $name, int $type = \PDO::PARAM_STR): string {
引号标识符

public function reset(): Insert
重置内部数组

public function resetColumns(): Insert
重置...columns

public function resetFlags(): Insert
重置...flags

public function resetFrom(): Insert
重置...from

public function resetGroupBy(): Insert
重置...group by

public function resetHaving(): Insert
重置...having

public function resetLimit(): Insert
重置...limitoffset

public function resetOrderBy(): Insert
重置...order by

public function resetWhere(): Insert
重置...where

public function returning(array $columns): Insert
添加RETURNING子句

public function set(string $column, mixed $value = null): Insert
设置一个column = value条件

public function setFlag(string $flag, bool $enable = true): void
设置查询的标志位,比如DISTINCT

protected function buildFlags()
构造标志语句

protected function buildReturning(): string
构造RETURNING子句

protected function indent(array $collection, string $glue = ""): string
缩进一个集合

激活

要实例化一个Phalcon\DataMapper\Query\Insert查询构造器,你可以使用Phalcon\DataMapper\Query\QueryFactory结合一个Phalcon\DataMapper\Pdo\Connection.

<?php

use Phalcon\DataMapper\Pdo\Connection;
use Phalcon\DataMapper\Query\QueryFactory;

$host     = '127.0.0.1';
$database = 'phalon_test';
$charset  = 'utf8mb4';
$port     = 3306;
$username = 'phalcon';
$password = 'secret';

$dsn = sprintf(
    "mysql:host=%s;dbname=%s;charset=%s;port=%s",
    $host,
    $database,
    $charset,
    $port
);

$connection = new Connection($dsn, $username, $password);
$factory    = new QueryFactory();
$insert     = $factory->newInsert($connection);

构建

The into()方法用于指定要插入数据的表。

$insert
->into('co_invoices')
;

$insert->perform();
// INSERT INTO co_invoices

您可以使用column()方法用于指定列及其绑定的值。最后一个可选参数是由PDO使用的绑定类型。对于string, integer, floatnull值此会自动设定。

$insert
    ->into('co_invoices')
    ->column('inv_total', 100.12)
;

$insert->perform();
// INSERT INTO co_invoices (inv_total) VALUES (:inv_total)

The columns()方法返回对象本身,从而提供了流畅接口:

$insert
    ->into('co_invoices')
    ->column('inv_cst_id', 2)
    ->column('inv_total', 100.12);
    ->column('inv_status_flag', 0, PDO::PARAM_BOOL)
;

$insert->perform();
// INSERT INTO co_invoices (
//      inv_cst_id,
//      inv_total,
//      inv_status_flag
// ) VALUES (
//      :inv_cst_id,
//      :inv_total,
//      :inv_status_flag
// )

您还可以使用columns()方法,它接收一个元素数组。如果键是一个字符串,则将其视为字段名,其值将作为字段的值。或者,对于具有数字键的数组元素,该元素的值将成为字段名。string it is considered the field name, and its value will be the value of the field. Alternatively, for an array element with a numeric key, the value of that element will be the field name.

$insert
    ->into('co_invoices')
    ->columns(
        [
            'inv_cst_id' => 2, 
            'inv_total'  => 100.12
        ]
    )
;

$insert->perform();
// INSERT INTO co_invoices (
//      inv_cst_id,
//      inv_total
// ) VALUES (
//      :inv_cst_id,
//      :inv_total
// )

注意

使用columns()方法时,不能定义每个字段/值对的PDO类型。

变量

绑定值会自动被加上引号并进行转义。然而,有些情况下我们需要为字段设置特定值而不希望它被转义。一个常见的例子是利用赋值给日期字段的关键字NOW()。为此我们可以使用set()方法设置容器。

$insert
    ->into('co_invoices')
    ->column('inv_total', 100.12)
    ->set('inv_created_date', 'NOW()')
;

$insert->perform();
// INSERT INTO co_invoices (
//      inv_total,
//      inv_created_date
// ) VALUES (
//      :inv_total,
//      NOW()
// )
语句

对象可以通过调用getStatement()方法设置容器。

$insert
    ->into('co_invoices')
    ->column('inv_total', 100.12)
    ->set('inv_created_date', 'NOW()')
;

echo $insert->getStatement();
// INSERT INTO co_invoices (
//      inv_total,
//      inv_created_date
// ) VALUES (
//      :inv_total,
//      NOW()
// )
返回

一些数据库(特别是PostgreSQL)支持RETURNING子句。你可以使用returning()方法来实现此功能,传递一个包含需要返回字段的数组。

$insert
    ->into('co_invoices')
    ->columns(
        [
            'inv_cst_id', 
            'inv_total' => 100.12
        ]
    )
    ->set('inv_id', null)
    ->set('inv_status_flag', 1)
    ->set('inv_created_date', 'NOW()')
    ->columns(
        [
            'inv_cst_id' => 1
        ]
    )
    ->returning(
        [
            'inv_id', 
            'inv_cst_id'
        ]
    )
    ->returning(
        [
            'inv_total'
        ]
    )
    ->set('inv_created_date', 'NOW()')
;

$insert->perform();
// INSERT INTO co_invoices (
//      inv_cst_id, 
//      inv_total, 
//      inv_id, 
//      inv_status_flag, 
//      inv_created_date
// ) VALUES (
//      :inv_cst_id, 
//      :inv_total, 
//      NULL, 
//      1, 
//      NOW()
// ) 
// RETURNING inv_id, inv_cst_id, inv_total
标志

你可以使用setFlag()方法设置你的数据库服务器识别的标志。例如,你可以设置一个MySQLLOW_PRIORITY标志,如下所示:

$insert
    ->into('co_invoices')
    ->column('inv_total', 100.12)
    ->set('inv_created_date', 'NOW()')
    ->setFlag('LOW_PRIORITY')
;

$insert->perform();
// INSERT LOW_PRIORITY INTO co_invoices (
//      inv_total,
//      inv_created_date
// ) VALUES (
//      :inv_total,
//      NOW()
// )

Select

激活

要实例化一个Phalcon\DataMapper\Query\Select查询构造器,你可以使用Phalcon\DataMapper\Query\QueryFactory结合一个Phalcon\DataMapper\Pdo\Connection.

<?php

use Phalcon\DataMapper\Pdo\Connection;
use Phalcon\DataMapper\Query\QueryFactory;

$host     = '127.0.0.1';
$database = 'phalon_test';
$charset  = 'utf8mb4';
$port     = 3306;
$username = 'phalcon';
$password = 'secret';

$dsn = sprintf(
    "mysql:host=%s;dbname=%s;charset=%s;port=%s",
    $host,
    $database,
    $charset,
    $port
);

$connection = new Connection($dsn, $username, $password);
$factory    = new QueryFactory();
$select     = $factory->newSelect($connection);

执行

The Phalcon\DataMapper\Query\Select构建器充当了Phalcon\DataMapper\Pdo\Connection对象的代理。因此,在构建查询之后,下列方法将会可用:

  • fetchAffected()
  • fetchAll()
  • fetchAssoc()
  • fetchCol()
  • fetchGroup()
  • fetchObject()
  • fetchObjects()
  • fetchOne()
  • fetchPairs()
  • fetchValue()
$records = $select
    ->from('co_invoices')
    ->columns(['inv_id', 'inv_title'])
    ->where('inv_cst_id = 1')
    ->fetchAssoc()
;

var_dump($records);
// [
//      ['inv_id' => 1, 'inv_title' => 'Invoice 1'],
//      ['inv_id' => 2, 'inv_title' => 'Invoice 2'],
// ]

构建

要向Select中添加列,请使用columns()方法并将列作为数组传入。如果一个键定义为了字符串,它将作为列的别名使用。

列名(Column Names)

<?php

$columns = [
    'inv_id', 
    'inv_cst_id', 
    'inv_status_flag', 
    'inv_title', 
    'inv_total', 
    'inv_created_at',
];

$select->columns($columns);

// SELECT
//      inv_id,
//      inv_cst_id,
//      inv_status_flag,
//      inv_title,
//      inv_total,
//      inv_created_at

别名

<?php

$columns = [
    'id'         => 'inv_id', 
    'customerId' => 'inv_cst_id', 
    'status'     => 'inv_status_flag', 
    'title'      => 'inv_title', 
    'total'      => 'inv_total', 
    'createdAt'  => 'inv_created_at',
];

$select->columns($columns);

// SELECT 
//      id, 
//      customerId, 
//      status, 
//      title, 
//      total, 
//      createdAt

计数

<?php

$columns = [
    'customerId' => 'inv_cst_id', 
    'totalCount' => 'COUNT(inv_total)'
];

$select->columns($columns);

// SELECT 
//      customerId, 
//      COUNT(inv_total) AS totalCount
FROM

添加一个FROM子句,请使用from()方法:

直接输出

<?php

$select
    ->from('co_invoices')
;

// SELECT * FROM co_invoices

别名

<?php

$select
    ->from('co_invoices AS i')
;

// SELECT * FROM co_invoices i
JOIN

方法。

LEFT

<?php

$select
    ->from('co_invoices')
    ->join($select::JOIN_LEFT, 'co_customers', 'inv_cst_id = cst_id')
;

// SELECT * FROM co_invoices 
//  LEFT JOIN co_customers ON inv_cst_id = cst_id

RIGHT

<?php

$select
    ->from('co_invoices')
    ->join($select::JOIN_RIGHT, 'co_customers', 'inv_cst_id = cst_id')
;

// SELECT * FROM co_invoices 
//  RIGHT JOIN co_customers ON inv_cst_id = cst_id

INNER

<?php

$select
    ->from('co_invoices')
    ->join($select::JOIN_INNER, 'co_customers', 'inv_cst_id = cst_id')
;

// SELECT * FROM co_invoices 
//  INNER JOIN co_customers ON inv_cst_id = cst_id

NATURAL

<?php

$select
    ->from('co_invoices AS i')
    ->join($select::JOIN_NATURAL, 'co_customers', 'inv_cst_id = cst_id')
;

// SELECT * FROM co_invoices 
//  NATURAL JOIN co_customers ON inv_cst_id = cst_id

With Bind

<?php

$status = 1;
$select
    ->from('co_invoices')
    ->join(
        $select::JOIN_LEFT, 
        'co_customers', 
        'inv_cst_id = cst_id AND cst_status_flag = ',
        $status
    )
    ->appendJoin(' AND cst_name LIKE ', '%john%')
;

// SELECT * FROM co_invoices 
//  LEFT JOIN co_customers ON inv_cst_id = cst_id 
//      AND cst_status_flag = :__1__
//      AND cst_name LIKE :__2__
WHERE

添加WHERE条件,请使用where()方法。后续对where()的调用会隐式地AND连接后续的条件。

单个(Single)

<?php

$invoiceId = 1;
$select
    ->from('co_invoices')
    ->where('inv_id > ', $invoiceId)
;

// SELECT * FROM co_invoices 
//  WHERE inv_id > :__1__

andWhere

<?php

$customerIds = [1, 2, 3];
$status      = 1;
$totalValue  = 100;
$select
    ->from('co_invoices')
    ->where('inv_id > 1')
    ->andWhere('inv_total > :total')
    ->andWhere('inv_cst_id IN ', $customerIds)
    ->appendWhere(' AND inv_status_flag = ' . $select->bindInline($status))
    ->bindValue('total', $totalValue)
;

// SELECT * FROM co_invoices 
//  WHERE inv_id > 1
//      AND inv_total > :total 
//      AND inv_cst_id IN (:__1__, :__2__, :__3__) 
//      AND inv_status_flag = :__4__

orWhere

<?php

$status      = 1;
$totalValue  = 100;
$select
    ->from('co_invoices')
    ->appendWhere('inv_total > ', $totalValue)
    ->orWhere("inv_status_flag = :status")
    ->bindValue('status', $status)
;

// SELECT * FROM co_invoices 
//  WHERE inv_total > :__1__ "
//      OR inv_status_flag = :status

whereEquals

还有额外的whereEquals()快捷方法可以根据一组键值对为您添加一系列相等条件:AND equality conditions for you based on an array of key-value pairs:

  • 给定数组值,条件将是IN ().
  • 如果给定空数组,条件将是FALSE(这意味着查询不会返回任何结果)。
  • 给定一个null值,条件将是IS NULL.
  • 对于所有其他值,条件将是=.
  • 如果您传递了一个没有值的键,该键将作为原始未转义的条件使用。
<?php

$invoiceIds = [1, 2, 3];
$select
    ->from('co_invoices')
    ->whereEquals(
        [
            'inv_id'     => $invoiceIds,
            'inv_cst_id' => null,
            'inv_title'  => 'ACME',
            'inv_created_at = NOW()',
        ]
    )
;

// SELECT * FROM co_invoices 
//  WHERE inv_id IN (:__1__, :__2__, :__3__)
//      AND inv_cst_id IS NULL 
//      AND inv_title = :__4__ 
//      AND inv_created_at = NOW()
GROUP BY

添加GROUP BY表达式,使用groupBy()方法并将每个表达式作为可变参数传入。

<?php

$select
    ->from('co_invoices')
    ->groupBy('inv_cst_id')
    ->groupBy('inv_status_flag')
;

// SELECT * FROM co_invoices 
//  GROUP BY inv_cst_id, inv_status_flag
HAVING

The HAVING方法与它们对应的WHERE方法:

  • having()andHaving() AND a HAVING条件
  • orHaving() ORHAVING条件
  • appendHaving()将追加到最近的HAVING条件
ORDER BY

添加ORDER BY表达式,使用orderBy()方法并将每个表达式作为数组中的一个元素传入。

<?php

$select
    ->from('co_invoices')
    ->orderBy(
        [
            'inv_cst_id',
            'UPPER(inv_title) DESC',
        ]
    )
;

// SELECT * FROM co_invoices 
//  ORDER BY inv_cst_id, UPPER(inv_title) DESC
LIMIT, OFFSET,分页(Pagination)

设置一个LIMITOFFSET,请使用limit()offset()方法。

<?php

$select
    ->from('co_invoices')
    ->limit(10)
;

// SELECT * FROM co_invoices 
//  LIMIT 10

$select
    ->from('co_invoices')
    ->limit(10)
    ->offset(50)
;

// SELECT * FROM co_invoices 
//  LIMIT 10 OFFSET 50

分页

方法。或者,您可以使用page()perPage()方法:

<?php

$select
    ->from('co_invoices')
    ->page(5)
    ->perPage(10)
;

// SELECT * FROM co_invoices 
//  LIMIT 10 OFFSET 5
DISTINCT

你可以设置DISTINCT子句如下:

<?php

$select
    ->distinct()
    ->from('co_invoices')
    ->columns(
        [
            'inv_id', 
            'inc_cst_id'
        ]
    )
;

// SELECT DISTINCT inv_id, inc_cst_id
// FROM co_invoices

注意

该方法接受一个可选的布尔参数,用于启用(true) 或禁用(false)该标志。

FOR UPDATE

你可以设置FOR UPDATE子句如下:

<?php

$select
    ->from('co_invoices')
    ->forUpdate()
;

// SELECT * FROM co_invoices FOR UPDATE

$select
    ->from('co_invoices')
    ->forUpdate()
    ->forUpdate(false)
;

// SELECT * FROM co_invoices

注意

该方法接受一个可选的布尔参数,用于启用(true) 或禁用(false)该标志。

标志

你可以使用setFlag()方法设置你的数据库服务器识别的标志。例如,你可以设置一个MySQLHIGH_PRIORITY标志,如下所示:

<?php

$select
    ->from('co_invoices')
    ->setFlag('HIGH_PRIORITY')
;

// SELECT HIGH_PRIORITY * FROM co_invoices
UNION

UNIONUNION ALL当前Select使用后续语句进行查询时,调用以下任意一个union*()方法:

<?php

$select
    ->from('co_invoices')
    ->where('inv_id = 1')
    ->union()
    ->from('co_invoices')
    ->where('inv_id = 2')
    ->union()
    ->from('co_invoices')
    ->where('inv_id = 3')
;

// SELECT * FROM co_invoices WHERE inv_id = 1
// UNION 
// SELECT * FROM co_invoices WHERE inv_id = 2
// UNION
// SELECT * FROM co_invoices WHERE inv_id = 3

$select
    ->from('co_invoices')
    ->where('inv_id = 1')
    ->unionAll()
    ->from('co_invoices')
    ->where('inv_id = 2')
;

// SELECT * FROM co_invoices WHERE inv_id = 1
// UNION ALL 
// SELECT * FROM co_invoices WHERE inv_id = 2 

重置

The Select类提供了reset()方法,它允许你将对象重置为其原始状态并重新使用它(例如:重新发送一条语句以获取COUNT(*)不带LIMIT的情况下,计算分页总行数)。

另外,下列方法允许你重置查询中的特定部分:

  • resetColumns()- 重置columns
  • resetFrom()- 重置from
  • resetWhere()- 重置where
  • resetGroupBy()- 重置group by
  • resetHaving()- 重置having
  • resetOrderBy()- 重置order by
  • resetLimit()- 重置limitoffset
  • resetFlags()- 重置flags

子查询对象

如果你想创建一个子查询,请调用subSelect()方法。当你完成子查询构建后,使用asAlias()方法为其赋予一个别名;该对象本身可以在所需的条件或表达式中使用。当在FROM条件中使用时,你需要调用getStatement()方法,以返回正确的SQL语句给Select对象中的相关方法手动

<?php

$select
    ->from(
        $select
            ->subSelect()
            ->columns("inv_id")
            ->from('co_invoices')
            ->asAlias('inv')
            ->getStatement()
    )
;

// SELECT *
// FROM (SELECT inv_id FROM co_invoices) AS inv 

当我们需要传递参数时,可以直接将它们添加到子查询中。

<?php

$invoiceId  = 1;
$maxInvoice = 100;
$select
    ->from(
        $select
            ->subSelect()
            ->columns('inv_id')
            ->from('co_invoices')
            ->where('inv_id > ', $invoiceId)
            ->asAlias('inv')
            ->getStatement()
    )
    ->where('inv_id <', $maxInvoice)
;

// SELECT *
// FROM (SELECT inv_id FROM co_invoices WHERE inv_id > __1__) AS inv
// WHERE inv_id < __2__ 

子查询也可以在JOINWHERE条件中使用,如下所示:

<?php

$select
    ->from('co_invoices')
    ->join(
        'LEFT'
        $select
            ->subSelect()
            ...
            ->asAlias('subAlias')
            ->getStatement()
    )
;

对于WHERE特别是,你不需要使用getStatement()

<?php

$customerId = 1;
$total      = 100.0
$select
    ->columns(
        [
            'inv_id',
            'inv_total'
        ]   
    )
    ->from('co_invoices')
    ->where(
        'inv_id IN '
        $select
            ->subSelect()
            ->columns(
                [
                    'cst_inv_id',
                ]
            )
            ->from('co_customers')
            ->where('inv_total > ', $total)
    )
;

// SELECT inv_id, inv_total
// FROM co_invoices
// WHERE inv_id IN (SELECT cst_inv_id FROM co_customers WHERE inv_total > __1__) 

更新

方法

public function andWhere(
    string condition,
    mixed $value = null,
    int $type = -1
): Update

设置一个ANDWHERE条件

public function appendWhere(
    string $condition,
    mixed $value = null,
    int $type = -1
): Update

追加到最近的WHERE子句

public function bindInline(mixed $value, int $type = -1): string

内联绑定一个值

public function bindValue(
    string $key,
    mixed $value,
    int $type = -1
): Update

绑定一个值 - 如有需要自动检测类型

public function bindValues(array $values): Update

绑定一组值

public function column(
    string $column, 
    mixed $value = null, 
    int $type = -1
): Update

设置一个列用于UPDATE查询

public function columns(array $columns): Update

批量设置列和值用于UPDATE

public function from(string $table): Update

向查询中添加一张表

public function getBindValues(): array

返回所有已绑定的值

public function getStatement(): string

返回SQL语句

public function hasColumns(): bool

查询是否有列

public function limit(int $limit): Update

设置表单的LIMIT子句

public function offset(int $offset): Update

设置表单的OFFSET子句

public function orderBy(mixed $orderBy): Update

设置表单的ORDER BY

public function orWhere(
    string $condition,
    mixed $value = null,
    int $type = -1
): Update

设置一个ORWHERE条件

public function perform()

在连接中执行一条语句

public function quoteIdentifier(
    string $name,
    int $type = \PDO::PARAM_STR
): string

引号标识符

public function returning(array $columns): Update

添加RETURNING子句

public function reset(): void

重置内部存储

public function resetColumns(): void

重置列

public function resetFlags(): void

重置标志

public function resetFrom(): void

重置 from

public function resetGroupBy(): void

重置 group by

public function resetHaving(): void

重置 having

public function resetLimit(): void

重置 limit 和 offset

public function resetOrderBy(): void

重置 order by

public function resetWhere(): void

重置 where

public function set(string $column, mixed $value = null): Update

设置列 = 值条件

public function setFlag(string $flag, bool $enable = true): void

设置查询标志,例如 "DISTINCT"

public function where(
    string $condition,
    mixed $value = null,
    int $type = -1
): Update

设置一个WHERE条件

public function whereEquals(array $columnsValues): Update

设置一个WHERE等值条件

protected function addCondition(
    string $store,
    string $andor,
    string $condition,
    mixed $value = null,
    int $type = -1
): void

追加一个条件

protected function appendCondition(
    string $store,
    string $condition,
    mixed $value = null,
    int $type = -1
): void

连接一个条件

protected function buildBy(string $type): string

构造一个BY列表

protected function buildCondition(string $type): string

构造条件字符串

protected function buildFlags()

构造标志语句

protected function buildLimitEarly(): string

构造早期的LIMIT子句 - MS SQLServer

protected function buildLimit(): string

构造LIMIT子句

protected function buildLimitCommon(): string

构造LIMIT子句(适用于所有驱动程序)

protected function buildLimitSqlsrv(): string

构造LIMIT子句(适用于 MSSQLServer)

protected function buildReturning(): string

构造RETURNING子句

protected function indent(array collection, string glue = ""): string

缩进一个集合

protected function processValue(string $store, mixed $data): void

处理一个值(数组或字符串)并将其与存储合并

激活

要实例化一个Phalcon\DataMapper\Query\Update查询构造器,你可以使用Phalcon\DataMapper\Query\QueryFactory结合一个Phalcon\DataMapper\Pdo\Connection.

<?php

use Phalcon\DataMapper\Pdo\Connection;
use Phalcon\DataMapper\Query\QueryFactory;

$host     = '127.0.0.1';
$database = 'phalon_test';
$charset  = 'utf8mb4';
$port     = 3306;
$username = 'phalcon';
$password = 'secret';

$dsn = sprintf(
    "mysql:host=%s;dbname=%s;charset=%s;port=%s",
    $host,
    $database,
    $charset,
    $port
);

$connection = new Connection($dsn, $username, $password);
$factory    = new QueryFactory();
$insert     = $factory->newUpdate($connection);

构建

The table()方法用于指定要插入数据的表。

$update
    ->table('co_invoices');

$update->perform();
// UPDATE co_invoices

您可以使用column()方法用于设置特定列的新值。

$update
    ->table('co_invoices');
    ->column('inv_cst_id', 2)
    ->column('inv_total', 100.12);
    ->column('inv_status_flag', 0, PDO::PARAM_BOOL)
;

$update->perform();
// UPDATE co_invoices
// SET  inv_cst_id = :inv_cst_id,
//      inv_total = :inv_total,
//      inv_status_flag = :inv_status_flag

注意

PDO参数类型将会自动为strings, integers, floatsnulls设置。你可以使用column()的第三个参数来设置所需类型。

除了多次调用column()方法之外,你始终可以调用columns()并传入一个数组,其中数组键是字段名,数组值是你想要更新的对应值。

$update
    ->table('co_invoices');
    ->columns(
        [
            'inv_cst_id'      => 2,
            'inv_total'       => 100.12,
            'inv_status_flag' => 0,
        ]
    )
;

$update->perform();
// UPDATE co_invoices
// SET  inv_cst_id = :inv_cst_id,
//      inv_total = :inv_total,
//      inv_status_flag = :inv_status_flag

注意

使用columns()方法时,你无法设置每个参数的类型。

WHERE

The WHERE的方法与UPDATE完全相同。Select

ORDER BY

某些数据库(尤其是MySQL)支持在删除操作中使用ORDER BY。你可以使用orderBy()方法来指定它。

$update
    ->table('co_invoices');
    ->columns(
        [
            'inv_cst_id'      => 2,
            'inv_total'       => 100.12,
            'inv_status_flag' => 0,
        ]
    )
    ->where('inv_cst_id = ', 1)
    ->orderBy('inv_id')
; 

$update->perform();

// UPDATE co_invoices
// SET  inv_cst_id = :inv_cst_id,
//      inv_total = :inv_total,
//      inv_status_flag = :inv_status_flag
// WHERE inv_cst_id = 1
// ORDER BY inv_id
LIMIT/OFFSET

某些数据库(MySQL、SQLite)支持LIMIT和/或OFFSET子句。你可以使用limit()offset()方法来指定它们。

$update
    ->table('co_invoices');
    ->columns(
        [
            'inv_cst_id'      => 2,
            'inv_total'       => 100.12,
            'inv_status_flag' => 0,
        ]
    )
    ->where('inv_cst_id = ', 1)
    ->orderBy('inv_id')
    ->limit(10)
    ->offset(40)
; 

$update->perform();

// UPDATE co_invoices
// SET  inv_cst_id = :inv_cst_id,
//      inv_total = :inv_total,
//      inv_status_flag = :inv_status_flag
// WHERE inv_cst_id = 1
// ORDER BY inv_id
// LIMIT 10 OFFSET 40
RETURNING

一些数据库(特别是PostgreSQL)支持RETURNING子句。你可以使用returning()方法来实现此功能,传递一个包含需要返回字段的数组。

$update
    ->table('co_invoices');
    ->columns(
        [
            'inv_cst_id'      => 2,
            'inv_total'       => 100.12,
            'inv_status_flag' => 0,
        ]
    )
    ->where('inv_cst_id = ', 1)
    ->orderBy('inv_id')
    ->limit(10)
    ->offset(40)
    ->returning(['inv_id', 'inv_cst_id'])
; 

$update->perform();

// UPDATE co_invoices
// SET  inv_cst_id = :inv_cst_id,
//      inv_total = :inv_total,
//      inv_status_flag = :inv_status_flag
// WHERE inv_cst_id = 1
// ORDER BY inv_id
// RETURNING inv_id, inv_cst_id
标志

你可以使用setFlag()方法设置你的数据库服务器识别的标志。例如,你可以设置一个MySQLLOW_PRIORITY标志,如下所示:

$update
    ->table('co_invoices');
    ->columns(
        [
            'inv_cst_id'      => 2,
            'inv_total'       => 100.12,
            'inv_status_flag' => 0,
        ]
    )
    ->where('inv_cst_id = ', 1)
    ->orderBy('inv_id')
    ->limit(10)
    ->offset(40)
    ->returning(['inv_id', 'inv_cst_id'])
    ->setFlag('LOW_PRIORITY')
; 

$update->perform();

// UPDATE LOW_PRIORITY co_invoices
// SET  inv_cst_id = :inv_cst_id,
//      inv_total = :inv_total,
//      inv_status_flag = :inv_status_flag
// WHERE inv_cst_id = 1
// ORDER BY inv_id
// RETURNING inv_id, inv_cst_id
无噪 Logo
无噪文档
25 年 6 月翻译
版本号 5.9
文档源↗