gpt4 book ai didi

zend-db - zend expressive - zend 数据库

转载 作者:行者123 更新时间:2023-12-01 14:16:22 24 4
gpt4 key购买 nike

我现在正在尝试使用 zend expressive 并研究如何处理数据库。我在看this ,但不清楚。我使用 composer 安装 zend-db,它提到在 dependencies.global.php 中添加一行,然后在工厂类中使用 container 来获取适配器,但没有讨论如何在实际操作类中访问它所以我不知道发生了什么,因为适配器对象超出了其他类的范围。

有没有人有从头到尾真正能够连接和查询 sql 的良好且清晰的示例?

最佳答案

尝试通过工厂注入(inject)你的数据库类,按照骨架应用程序的例子,你可以这样做:

HomePageFactory.php

public function __invoke(ContainerInterface $container)
{
$router = $container->get(RouterInterface::class);
$template = $container->has(TemplateRendererInterface::class) ? $container->get(TemplateRendererInterface::class) : null;

$adapter = $container->get( Adapter::class );
$usersTable = $container->get( Table\UsersTable::class );


return new HomePageAction($router, $template,$adapter,$usersTable);
}

HomePageAction.php

class HomePageAction implements ServerMiddlewareInterface
{
/**
* @var Router\RouterInterface
*/
private $router;

/**
* @var null|Template\TemplateRendererInterface
*/
private $template;

/**
* @var Adapter
*/
private $dbAdapter;

/**
* @var UsersTable
*/
private $usersTable;

/**
* HomePageAction constructor.
* @param Router\RouterInterface $router
* @param Template\TemplateRendererInterface|null $template
* @param Adapter $adapter
* @param UsersTable $usersTable
*/
public function __construct( Router\RouterInterface $router, Template\TemplateRendererInterface $template = null, Adapter $adapter, Table\UsersTable $usersTable )
{
$this->router = $router;
$this->template = $template;
$this->dbAdapter = $adapter;
$this->usersTable = $usersTable;

}

在你的配置供应商那里,你有你的表,你必须将依赖项配置为工厂 EX:

'factories'  => [

Table\UsersTable::class => function($container) {
$dbAdapter = $container->get( AdapterInterface::class );
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype( new Model\Users() );
$tableGateway = new TableGateway('users', $dbAdapter, null, $resultSetPrototype);
return new Table\UsersTable($tableGateway);
},

],

关于zend-db - zend expressive - zend 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43287258/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com