Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zfegg/zend-db-feature
Zend db features
https://github.com/zfegg/zend-db-feature
Last synced: about 2 months ago
JSON representation
Zend db features
- Host: GitHub
- URL: https://github.com/zfegg/zend-db-feature
- Owner: zfegg
- License: mit
- Created: 2016-04-25T08:06:47.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-01-15T06:13:55.000Z (almost 5 years ago)
- Last Synced: 2024-05-22T08:00:30.159Z (7 months ago)
- Language: PHP
- Size: 19.5 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Zend db features
=========================## Installation / 安装
```
{
"require": {
"zfegg/zend-db-feature": "^1.0"
}
}
```## Usage / 使用说明
### TableGatewayAbstractServiceFactory 使用
`TableGatewayAbstractServiceFactory` 方便及明了的配置数据库和获取 `TableGateway` 对象.
下面介绍使用和配置#### 1. 确认`ServiceManager` 中以下两个抽象工厂类,以`module.config.php`为例子:
```php
use Zfegg\Db\TableGateway\Factory\TableGatewayAbstractServiceFactory;
use Laminas\Db\Adapter\AdapterAbstractServiceFactory;return [
'service_manager' => [
'abstract_factories' => [
TableGatewayAbstractServiceFactory::class,
AdapterAbstractServiceFactory::class,
]
]
];
```#### 2. 数据库配置,`Laminas\Db\Adapter\AdapterAbstractServiceFactory` 实现了多DbAdapter(数据库适配器)的获取.
```php
return [
'db' => [
'adapters' => [
'db' => [ //Default
'driver' => 'pdo_mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'db.name1' => [
'driver' => 'pdo_mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
'db.name2' => [
'driver' => 'pdo_mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
]
]
];
```在 `ServiceManager` 中获取方式:
```php
$serviceManager->get('db');
$serviceManager->get('db.name1');
$serviceManager->get('db.name2');
```#### 3. `TableGatewayAbstractServiceFactory`, 实现了多表的配置:
```php
return [
'tables' => [
'Demo1Table' => [
'table' => 'users', //Table name
//'adapter' => 'db', //Set TableGateway adapter `$container->get($config['adapter'] ?: 'db'])`, Default 'db'
//'schema' => 'test', //Set table schema
],
'Demo2Table' => [
'table' => 'users', //Table name
'invokable' => 'MyApp\\Model\\UserTable', //需要实例返回某个类,类必须是继承 `AbstractTableGateway`
],
Demo3Table::class => [
'table' => 'users', //Table name
'row' => true, //true will call `$table->getResultSetPrototype()->setArrayObjectPrototype(Laminas\Db\RowGateway\RowGateway);`
//'row' => 'MyApp\\Model\\UserEntity', //set custom ArrayObjectPrototype
//'primary' => 'id',
]
],
];
```在 `ServiceManager` 中获取方式:
```php
$serviceManager->get('Demo1Table'); //Instance of Laminas\Db\TableGateway
$serviceManager->get('Demo2Table'); //Instance of MyApp\Model\UserTable
$serviceManager->get(Demo3Table::class); //Instance of Laminas\Db\TableGateway
```#### 4. 使用实例
~~~php
use Zfegg\Db\TableGateway\Factory\TableGatewayAbstractServiceFactory;
use Laminas\Db\Adapter\AdapterAbstractServiceFactory;
use Laminas\ServiceManager\ServiceManager;$container = new ServiceManager(); //Zend ServiceManager v3
$container->configure([
'abstract_factories' => [
TableGatewayAbstractServiceFactory::class,
AdapterAbstractServiceFactory::class,
]
]);
$container->setService('config', [
'tables' => [
'TestTable' => [
'table' => 'user',
//'invokable' => 'MyApp\\Model\\UserTable',
'row' => true, //true will call `$table->getResultSetPrototype()->setArrayObjectPrototype(Laminas\Db\RowGateway\RowGateway);`
//'row' => 'MyApp\\Model\\UserEntity', //set custom ArrayObjectPrototype
'primary' => 'id',
//'schema' => 'test',
//'adapter' => 'db', //Set TableGateway adapter `$container->get($config['adapter'] ?: 'db'])`
]
],
'db' => [
'adapters' => [
'db' => [
'driver' => 'pdo_mysql',
'host' => 'localhost',
'database' => 'test',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
],
]
]
]);var_dump($container->has('TestTable'));
$table = $this->container->get('TestTable'); //TableGateway object//TableGateway CommonCallFeature
$table->find(1); //SELECT `user`.* FROM `user` WHERE `id` = 1//If config ['row' => true]
$rowGateway = $table->create(['fullName' => 'test', 'email' => '[email protected]']);
$rowGateway->save();//Fetch count
//SELECT count(1) AS `Zfegg_Db_Count` FROM `user` WHERE `email` = '[email protected]'
$total = $table->fetchCount(['email' => '[email protected]']);//Fetch to Paginator object
/** @var \Laminas\Paginator\Paginator $paginator */
$paginator = $table->fetchPaginator(['email' => '[email protected]']);//SELECT count(1) AS `Zfegg_Db_Count` FROM `user` WHERE `email` = '[email protected]'
$paginator->getTotalItemCount()';//SELECT `user`.* FROM `user` WHERE `email` = '[email protected]' LIMIT 10 OFFSET 0
$paginator->getCurrentItems()';//DELETE FROM `user` WHERE `id` = '1'
$table->deletePrimary(1);//INSERT INTO `user` (`fullName`, `email`) VALUES ('test', '[email protected]')
$table->save(['fullName' => 'test', 'email' => '[email protected]']);//UPDATE `user` SET `fullName` = 'test', `email` = '[email protected]' WHERE `id` = 1
$table->save(['id' => 1, 'fullName' => 'test', 'email' => '[email protected]']);
~~~## License
See [MIT license File](LICENSE)