netzmacht/contao-query-builder

此包已被废弃,不再维护。作者建议使用doctrine/dbal包。

基于Aura/SQL_Query的Contao查询构建器

安装: 41

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 2

分支: 0

类型:contao-module

1.0.0-rc1 2016-01-20 10:35 UTC

This package is auto-updated.

Last update: 2022-02-01 12:54:44 UTC


README

Build Status Version License Downloads Contao Community Alliance coding standard

此扩展提供了基于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语句。因此,为查询和条件提供了whereInorWhereIn

<?php

// Generates "category = 2 AND (date > ? OR highlighted = 1)"
$query
    ->whereIn('category', [2, 3])
    ->whereIn('author', [3, 4, 2]);
    

差异

尽管您可以使用命名绑定值,但生成的语句只包含?占位符,因为Contao只支持这些。