{"id":19300017,"url":"https://github.com/zfegg/zend-db-feature","last_synced_at":"2026-05-14T22:03:53.130Z","repository":{"id":57090483,"uuid":"57023311","full_name":"zfegg/zend-db-feature","owner":"zfegg","description":"Zend db features","archived":false,"fork":false,"pushed_at":"2020-01-15T06:13:55.000Z","size":20,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-05T22:42:04.365Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/zfegg.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-25T08:06:47.000Z","updated_at":"2020-01-14T03:04:06.000Z","dependencies_parsed_at":"2022-08-20T16:01:01.224Z","dependency_job_id":null,"html_url":"https://github.com/zfegg/zend-db-feature","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfegg%2Fzend-db-feature","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfegg%2Fzend-db-feature/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfegg%2Fzend-db-feature/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/zfegg%2Fzend-db-feature/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/zfegg","download_url":"https://codeload.github.com/zfegg/zend-db-feature/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240401347,"owners_count":19795533,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-09T23:13:26.162Z","updated_at":"2026-05-14T22:03:48.096Z","avatar_url":"https://github.com/zfegg.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Zend db features\n=========================\n\n## Installation / 安装\n\n```\n{\n    \"require\": {\n        \"zfegg/zend-db-feature\": \"^1.0\"\n    }\n}\n```\n\n## Usage / 使用说明\n\n### TableGatewayAbstractServiceFactory 使用\n\n`TableGatewayAbstractServiceFactory` 方便及明了的配置数据库和获取 `TableGateway` 对象.\n下面介绍使用和配置\n\n\n#### 1. 确认`ServiceManager` 中以下两个抽象工厂类，以`module.config.php`为例子：\n \n```php\nuse Zfegg\\Db\\TableGateway\\Factory\\TableGatewayAbstractServiceFactory;\nuse Laminas\\Db\\Adapter\\AdapterAbstractServiceFactory;\n\nreturn [\n    'service_manager' =\u003e [\n        'abstract_factories' =\u003e [\n            TableGatewayAbstractServiceFactory::class,\n            AdapterAbstractServiceFactory::class,\n        ]\n    ]\n];\n```\n\n#### 2. 数据库配置,`Laminas\\Db\\Adapter\\AdapterAbstractServiceFactory` 实现了多DbAdapter(数据库适配器)的获取.\n\n```php\nreturn [\n    'db' =\u003e [\n        'adapters' =\u003e [\n            'db' =\u003e [  //Default\n              'driver'   =\u003e 'pdo_mysql',\n              'host'     =\u003e 'localhost',\n              'database' =\u003e 'test',\n              'username' =\u003e 'root',\n              'password' =\u003e '',\n              'charset'  =\u003e 'utf8',\n            ],\n            'db.name1' =\u003e [\n              'driver'   =\u003e 'pdo_mysql',\n              'host'     =\u003e 'localhost',\n              'database' =\u003e 'test',\n              'username' =\u003e 'root',\n              'password' =\u003e '',\n              'charset'  =\u003e 'utf8',\n            ],\n            'db.name2' =\u003e [\n              'driver'   =\u003e 'pdo_mysql',\n              'host'     =\u003e 'localhost',\n              'database' =\u003e 'test',\n              'username' =\u003e 'root',\n              'password' =\u003e '',\n              'charset'  =\u003e 'utf8',\n            ],\n        ]\n    ]\n];\n```\n\n在 `ServiceManager` 中获取方式:\n\n```php\n$serviceManager-\u003eget('db');\n$serviceManager-\u003eget('db.name1');\n$serviceManager-\u003eget('db.name2');\n```\n\n#### 3. `TableGatewayAbstractServiceFactory`, 实现了多表的配置:\n\n```php\nreturn [\n  'tables' =\u003e [\n      'Demo1Table' =\u003e [\n          'table' =\u003e 'users',  //Table name\n          //'adapter' =\u003e 'db', //Set TableGateway adapter `$container-\u003eget($config['adapter'] ?: 'db'])`, Default 'db'\n          //'schema' =\u003e 'test', //Set table schema\n      ],\n      'Demo2Table' =\u003e [\n         'table' =\u003e 'users',  //Table name\n         'invokable' =\u003e 'MyApp\\\\Model\\\\UserTable', //需要实例返回某个类，类必须是继承 `AbstractTableGateway`\n      ],\n      Demo3Table::class =\u003e [\n          'table' =\u003e 'users',  //Table name\n          'row' =\u003e true, //true will call `$table-\u003egetResultSetPrototype()-\u003esetArrayObjectPrototype(Laminas\\Db\\RowGateway\\RowGateway);`\n          //'row' =\u003e 'MyApp\\\\Model\\\\UserEntity', //set custom ArrayObjectPrototype\n          //'primary' =\u003e 'id',\n      ]\n  ],\n];\n```\n\n在 `ServiceManager` 中获取方式:\n\n```php\n$serviceManager-\u003eget('Demo1Table'); //Instance of Laminas\\Db\\TableGateway\n$serviceManager-\u003eget('Demo2Table'); //Instance of MyApp\\Model\\UserTable\n$serviceManager-\u003eget(Demo3Table::class); //Instance of Laminas\\Db\\TableGateway\n```\n\n#### 4. 使用实例\n\n~~~php\nuse Zfegg\\Db\\TableGateway\\Factory\\TableGatewayAbstractServiceFactory;\nuse Laminas\\Db\\Adapter\\AdapterAbstractServiceFactory;\nuse Laminas\\ServiceManager\\ServiceManager;\n\n$container = new ServiceManager(); //Zend ServiceManager v3\n$container-\u003econfigure([\n  'abstract_factories' =\u003e [\n      TableGatewayAbstractServiceFactory::class,\n      AdapterAbstractServiceFactory::class,\n  ]\n]);\n$container-\u003esetService('config', [\n  'tables' =\u003e [\n      'TestTable' =\u003e [\n          'table' =\u003e 'user',\n          //'invokable' =\u003e 'MyApp\\\\Model\\\\UserTable',\n          'row' =\u003e true, //true will call `$table-\u003egetResultSetPrototype()-\u003esetArrayObjectPrototype(Laminas\\Db\\RowGateway\\RowGateway);`\n          //'row' =\u003e 'MyApp\\\\Model\\\\UserEntity', //set custom ArrayObjectPrototype\n          'primary' =\u003e 'id',\n          //'schema' =\u003e 'test',\n          //'adapter' =\u003e 'db', //Set TableGateway adapter `$container-\u003eget($config['adapter'] ?: 'db'])`\n      ]\n  ],\n  'db' =\u003e [\n      'adapters' =\u003e [\n          'db' =\u003e [\n              'driver'   =\u003e 'pdo_mysql',\n              'host'     =\u003e 'localhost',\n              'database' =\u003e 'test',\n              'username' =\u003e 'root',\n              'password' =\u003e '',\n              'charset'  =\u003e 'utf8',\n          ],\n      ]\n  ]\n]);\n\nvar_dump($container-\u003ehas('TestTable'));\n$table = $this-\u003econtainer-\u003eget('TestTable'); //TableGateway object\n\n//TableGateway CommonCallFeature\n$table-\u003efind(1);  //SELECT `user`.* FROM `user` WHERE `id` = 1\n\n//If config ['row' =\u003e true]\n$rowGateway = $table-\u003ecreate(['fullName' =\u003e 'test', 'email' =\u003e 'test@test.com']);\n$rowGateway-\u003esave();\n\n//Fetch count\n//SELECT count(1) AS `Zfegg_Db_Count` FROM `user` WHERE `email` = 'test@test.com'\n$total = $table-\u003efetchCount(['email' =\u003e 'test@test.com']);\n\n//Fetch to Paginator object\n/** @var \\Laminas\\Paginator\\Paginator $paginator */\n$paginator = $table-\u003efetchPaginator(['email' =\u003e 'test@test.com']);\n\n//SELECT count(1) AS `Zfegg_Db_Count` FROM `user` WHERE `email` = 'test@test.com'\n$paginator-\u003egetTotalItemCount()';\n\n//SELECT `user`.* FROM `user` WHERE `email` = 'test@test.com' LIMIT 10 OFFSET 0\n$paginator-\u003egetCurrentItems()';\n\n//DELETE FROM `user` WHERE `id` = '1'\n$table-\u003edeletePrimary(1);\n\n//INSERT INTO `user` (`fullName`, `email`) VALUES ('test', 'test@test.com')\n$table-\u003esave(['fullName' =\u003e 'test', 'email' =\u003e 'test@test.com']);\n\n//UPDATE `user` SET `fullName` = 'test', `email` = 'test@test.com' WHERE `id` = 1\n$table-\u003esave(['id' =\u003e 1, 'fullName' =\u003e 'test', 'email' =\u003e 'test@test.com']);\n~~~\n\n## License\n\nSee [MIT license File](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfegg%2Fzend-db-feature","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fzfegg%2Fzend-db-feature","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fzfegg%2Fzend-db-feature/lists"}