gnugat/query-bus 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

gnugat/query-bus

最新稳定版本:v2.0.1

Composer 安装命令:

composer require gnugat/query-bus

包简介

A PHP library for Interrogatory Messages.

README 文档

README

A PHP library for Interrogatory Messages.

Interrogatory Messages are passed to the QueryBus service which returns the result of the first QueryMatcher that supports it.

Installation

QueryBus can be installed using Composer:

composer require "gnugat/query-bus:~2.0"

Simple conversion

Let's take the following table:

CREATE TABLE article (
    id int,
    title VARCHAR(255),
    content TEXT
);

If we want to execute the following query:

SELECT id, title, content FROM article WHERE id = 42;

Then we have first to create a Query, which is a simple DTO:

<?php

require __DIR__.'/vendor/autoload.php';

use Gnugat\QueryBus\QueryBus;

class GetArticle
{
    public $id;

    public function __construct($id)
    {
        if (null === $id) {
            throw new \InvalidArgumentException('Required parameter ID is missing');
        }
        $this->id = (int) $id;
    }
}

Note: Queries can contain simple validation, for example to check for null values.

Then we have to create a QueryMatcher:

// ...

use Gnugat\QueryBus\QueryMatcher;

class GetArticleMatcher implements QueryMatcher
{
    private $connection;

    public function __construct($connection)
    {
        $this->connection = $connection;
        pg_prepare($this->connection, 'get_article', 'SELECT id, title, content FROM articles WHERE id = $1');
    }

    public function supports($query)
    {
        return $query instanceof GetArticle;
    }

    public function match($query)
    {
        $result = pg_execute($this->connection, 'get_article', array($query->id));

        return pg_fetch_array($result, NULL, PGSQL_ASSOC);
    }
}

Next we need to register it in QueryBus:

// ...

use Gnugat\QueryBus\QueryBus;

$connection = pg_pconnect('dbname=blog');
$queryBus = new QueryBus();
$queryBus->add(new GetArticleMatcher($connection));

Finally we can actually execute the query:

// ...

$articles = $queryBus->match(new GetArticle(42));

Further documentation

You can see the current and past versions using one of the following:

You can find more documentation at the following links:

统计信息

  • 总下载量: 89
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 0
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 3
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-05-26