定制 kappa/deaw 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

kappa/deaw

Composer 安装命令:

composer require kappa/deaw

包简介

Tiny wrapper for better and more comfortable works with Dibi

README 文档

README

Build Status

Tiny wrapper for better and more comfortable works with dibi

Content

Requirements

Full list of dependencies you can get from Composer config file

Installation

The best way to install Kappa\Deaw is using Composer

$ composer require kappa/deaw:@dev

Before your first usage you must have registered dibi with required settings.

extensions:
  dibi: Dibi\Bridges\Nette\DibiExtension22
  
dibi:
  host: 127.0.0.1
  username: root
  password: 
  database: test
  driver: mysql

When you have dibi registered you can add Kappa\Deaw extension without any extra configuration (all configuration will be used from dibi package).

extensions:
   dibi: Dibi\Bridges\Nette\DibiExtension22
   - \Kappa\Deaw\DI\DeawExtension
   
 dibi:
   host: 127.0.0.1
   username: root
   password: 
   database: test
   driver: mysql

How to use

The basic principe of this package is combine domain queries(cz) and dibi way. This package provides a query objects which can be used for fetching or executing queries and which is distributed into custom classes.

Usage this package is very easy. Kappa\Deaw provides one base class Kappa\Deaw\DataAccess for make works with dibi more comfortable. With this class you can make all fetches, executes and you can work with transactions.

Firstly, you can inject this class into your model

class Users {
    
    private $dataAccess;
    
    public function __construct(\Kappa\Deaw\DataAccess $dataAccess) {
        $this->dataAccess = $dataAccess;
    }
}

prepare the basic fetch query object which must implements \Kappa\Deaw\Query\Queryable interface or for easy query objects you can use abstract class \Kappa\Deaw\Query\QueryObject

class FetchAdminUsers extends QueryObject { // or implments Queryable 
    public function doQuery(QueryBuilder $builder) {
        return $builder->createQuery()->select('*')
            ->from('user')
            ->where('role = ?', 'admin');
    }
}

and now we can combine into model

class Users {
    
    private $dataAccess;
    
    public function __construct(\Kappa\Deaw\DataAccess $dataAccess) {
        $this->dataAccess = $dataAccess;
    }
    
    public function getAdmins() {
        return $this->dataAccess->fetch(new FetchAdminUsers());
    }
}

and it's all!

Fetches

The basic fetch principe is explained above. You can use three methods for fetching data.

  • fetch - for fetch all records (alternative to fetchAll from dibi)
  • fetchOne - for fetch only one record (alternative to fetch from dibi)
  • fetchSingle - for fetch single value (alternative to fetchSingle from dibi)

Execute

You can also run executable query object for insert, update or remove data. For example:

class AddNewAdminUser extends QueryObject
{
    private $name;
    
    public function __construct($name) {
        $this->name = $name;
    }
    
    public function doQuery(QueryBuilder $builder) {
        $builder->createQuery()->insert('users', [
            'name' => $this->name,
            'role' => 'admin'
        ]);
    }
}

and model

class Users {
    
    private $dataAccess;
    
    public function __construct(\Kappa\Deaw\DataAccess $dataAccess) {
        $this->dataAccess = $dataAccess;
    }
    
    public function addAdmin($name) {
        return $this->dataAccess->execute(new AddNewAdminUser($name));
    }
}

Transactions

Also, you can use very easy transactions wrapper for typical dibi transactions.

We use AddNewAdminUser query object from previous example for example:

class Users {
    
    private $dataAccess;
    
    public function __construct(\Kappa\Deaw\DataAccess $dataAccess) {
        $this->dataAccess = $dataAccess;
    }
    
    public function addAdmins() {
        $transaction = $this->dataAccess->createTransaction();
        try {
            $this->dataAccess->execute(new AddNewAdminUser('foo'));
            $this->dataAccess->execute(new AddNewAdminUser('bar'));
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollback();
        }
    }
}

And of course you use savepoints (when is supported) as nested transactions

class Users {
    
    private $dataAccess;
    
    public function __construct(\Kappa\Deaw\DataAccess $dataAccess) {
        $this->dataAccess = $dataAccess;
    }
    
    public function addAdmins() {
        $transaction = $this->dataAccess->createTransaction();
        try {
            $this->dataAccess->execute(new AddNewAdminUser('foo'));
            $this->dataAccess->execute(new AddNewAdminUser('bar'));
            $nestedTransactions = $this->dataAccess->createTransaction();
            try {
                $this->dataAccess->execute(new AddNewAdminUser('foo_bar'));
                $nestedTransactions->commit(); // savepoint release is not required
            } catch (\Exception $e) {
                $nestedTransactions->rollback();
            }  
            $transaction->commit();
        } catch (\Exception $e) {
            $transaction->rollback();       
        }      
    }
}

Query objects

The basic query object provide postFetch method which is called after each fetch. This method can be used for parsing data before return or for somethings else...

TIP: This method can be used for parsing to-many relations from string representation into array, for example:

SQL:

CREATE TABLE `articles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `user_id` int(11) NOT NULL,
  `title` varchar(255) COLLATE utf8_czech_ci NOT NULL,
  PRIMARY KEY (`id`),
  KEY `user_id` (`user_id`),
  CONSTRAINT `articles_ibfk_1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

INSERT INTO `articles` (`id`, `user_id`, `title`) VALUES
(1,	1,	'Foo_article_1'),
(2,	1,	'Foo_article_2'),
(3,	2,	'Bar_article_1'),
(4,	2,	'Bar_article_2');

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_czech_ci NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_czech_ci;

INSERT INTO `users` (`id`, `name`) VALUES
(1,	'Foo'),
(2,	'Bar');
class FetchToMany implements Queryable {
    public function doQuery(QueryBuilder $builder) {
        return $builder->createQuery()
            ->select('users.id, users.name, GROUP_CONCAT(articles.title SEPARATOR ',') as articles')
            ->leftJoin('articles')->on('articles.user_id = users.id')
            ->from('users');
    }
    
    public function postFetch($data) {
        foreach ($data as $key => $row) {
            $data[$key]['articles'] = explode(',', $data[$key]['articles'];
        }
        
        return $data;
    }
}

and results will be

    [
        [
            'id' => '1',
            'name' => 'Foo',
            'articles' => [
                'Foo_article_1',
                'Foo_article_2'
            ]
        ],
        [
            'id' => '2',
            'name' => 'Bar',
            'articles' => [
                'Bar_article_1',
                'Bar_article_2'
            ]
        ]
    ]

Development

Tests

When you can run test you must crate own config files. Please copy file tests/config/config.sample.neon, rename to config.neon (remove sample word) and update credentials in this file. Same do with tests/config/database.sample.ini

And now you can run test ./vendor/bin/tests -c ./tests/php-unix.ini ./tests

kappa/deaw 适用场景与选型建议

kappa/deaw 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 316 次下载、GitHub Stars 达 0, 最近一次更新时间为 2015 年 06 月 22 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「wrapper」 「dibi」 「better access」 「easy access」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 kappa/deaw 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 kappa/deaw 我们能提供哪些服务?
定制开发 / 二次开发

基于 kappa/deaw 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-22