💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、星火、月之暗面及文生图 广告
### 模型 **模型实体类** 处理数据数组,返回对象类型 > module/Album/src/Model/Album.php ```php namespace Album\Model; class Album { public $id; public $artist; public $title; public function exchangeArray(array $data) { $this->id = !empty($data['id']) ? $data['id'] : null; $this->artist = !empty($data['artist']) ? $data['artist'] : null; $this->title = !empty($data['title']) ? $data['title'] : null; } } ``` **数据表类** 数据库增查删改操作 > module/Album/src/Model/AlbumTable.php ```php namespace Album\Model; use RuntimeException; use Zend\Db\TableGateway\TableGatewayInterface; class AlbumTable { private $tableGateway; public function __construct(TableGatewayInterface $tableGateway) { $this->tableGateway = $tableGateway; } public function fetchAll() { return $this->tableGateway->select(); } public function getAlbum($id) { $id = (int) $id; $rowset = $this->tableGateway->select(['id' => $id]); $row = $rowset->current(); if (! $row) { throw new RuntimeException(sprintf( 'Could not find row with identifier %d', $id )); } return $row; } public function saveAlbum(Album $album) { $data = [ 'artist' => $album->artist, 'title' => $album->title, ]; $id = (int) $album->id; if ($id === 0) { $this->tableGateway->insert($data); return; } if (! $this->getAlbum($id)) { throw new RuntimeException(sprintf( 'Cannot update album with identifier %d; does not exist', $id )); } $this->tableGateway->update($data, ['id' => $id]); } public function deleteAlbum($id) { $this->tableGateway->delete(['id' => (int) $id]); } } ``` **模块管理器** 添加服务和控制器配置 > module/Album/src/Module.php ```php namespace Album; // Add these import statements: use Zend\Db\Adapter\AdapterInterface; use Zend\Db\ResultSet\ResultSet; use Zend\Db\TableGateway\TableGateway; use Zend\ModuleManager\Feature\ConfigProviderInterface; class Module implements ConfigProviderInterface { // getConfig() method is here // Add this method: public function getServiceConfig() { return [ 'factories' => [ Model\AlbumTable::class => function($container) { $tableGateway = $container->get(Model\AlbumTableGateway::class); return new Model\AlbumTable($tableGateway); }, Model\AlbumTableGateway::class => function ($container) { $dbAdapter = $container->get(AdapterInterface::class); $resultSetPrototype = new ResultSet(); $resultSetPrototype->setArrayObjectPrototype(new Model\Album()); return new TableGateway('album', $dbAdapter, null, $resultSetPrototype); }, ], ]; } // Add this method: public function getControllerConfig() { return [ 'factories' => [ Controller\AlbumController::class => function($container) { return new Controller\AlbumController( $container->get(Model\AlbumTable::class) ); }, ], ]; } } ``` **修改控制器** 导入数据表对象 > module/Album/src/Controller/AlbumController.php ```php namespace Album\Controller; // Add the following import: use Album\Model\AlbumTable; use Zend\Mvc\Controller\AbstractActionController; use Zend\View\Model\ViewModel; class AlbumController extends AbstractActionController { // Add this property: private $table; // Add this constructor: public function __construct(AlbumTable $table) { $this->table = $table; } public function indexAction() { return new ViewModel([ 'albums' => $this->table->fetchAll(), ]); } } ``` **模块配置** 移除控制器配置 > module/Album/config/module.config.php ```php namespace Album; // Remove this: use Zend\ServiceManager\Factory\InvokableFactory; return [ // And remove the entire "controllers" section here: 'controllers' => [ 'factories' => [ Controller\AlbumController::class => InvokableFactory::class, ], ], /* ... */ ]; ```