netzmacht / contao-query-builder
1.0.0-rc1
2016-01-20 10:35 UTC
Requires
- php: ~5.4|~7.0
- aura/sqlquery: ~2
- contao/core: >=3.2,<3.6-dev
Requires (Dev)
This package is auto-updated.
Last update: 2022-02-01 12:54:44 UTC
README
此扩展提供了基于aura/sqlquery的查询构建器。
安装
您可以使用Composer安装此库。它需要至少PHP 5.5和Contao 3.2。
$ php composer.phar require netzmacht/contao-query-builder:~1.0
文档
请参阅aura/sqlquery的文档以了解基本用法。
Contao集成增加了使用c-c-a/dependency-container轻松执行创建的语句和DI集成。
基本用法
<?php $factory = $GLOBALS['container']['query-builder.factory']; // Creates insert query implementing interface Netzmacht\Contao\QueryBuilder\Query\Insert $insert = $factory->newInsert(); // Creates insert query implementing interface Netzmacht\Contao\QueryBuilder\Query\Update $update = $factory->newUpdate(); // Creates insert query implementing interface Netzmacht\Contao\QueryBuilder\Query\Select $select = $factory->newSelect(); // Creates insert query implementing interface Netzmacht\Contao\QueryBuilder\Query\Delete $delete = $factory->newDelete(); // Executing the statement $result = $statement->execute();
扩展功能
尽管此扩展基于aura/sqlquery,但它提供了一些额外功能。
查询条件
如果您有复杂的where条件,您可以传递一个回调函数,该函数生成一个单独的条件对象。
<?php // Generates "category = 2 AND (date > ? OR highlighted = 1)" $query ->where('category = 2') ->where( function (Netzmacht\Contao\QueryBuilder\Condition $condition) { $condition->orWhere('date > ?', time()); $condition->orWhere('highlighted = 1'); } );
Where in语句
由于Contao不使用PDO作为驱动程序,您必须手动创建whereIn语句。因此,为查询和条件提供了whereIn
和orWhereIn
。
<?php // Generates "category = 2 AND (date > ? OR highlighted = 1)" $query ->whereIn('category', [2, 3]) ->whereIn('author', [3, 4, 2]);
差异
尽管您可以使用命名绑定值,但生成的语句只包含?
占位符,因为Contao只支持这些。