【发布时间】:2020-10-23 07:40:27
【问题描述】:
我对 Laminas 比较陌生,有些事情对我来说仍然没有意义 - 就我而言 - 在我看来,如何在 laminas 中完成事情非常复杂。就我而言,现在我需要数据库适配器的实例。
项目是这样的:
我有一个 IndexController(和一个工厂)创建(在发布请求的情况下) 邮件类的一个实例 并且该 Mail 类应该在 MailQueueTable 中添加数据。 但是我不知道如何获取MailQueueTable中的DB Adapter
源码如下:
IndexControllerFactory.php
<?php
declare(strict_types=1);
namespace Test\Controller\Factory;
use Interop\Container\ContainerInterface;
use Laminas\ServiceManager\Factory\FactoryInterface;
use Test\Controller\IndexController;
class IndexControllerFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestName, array $options = null)
{
return new IndexController(
$container->get('ApplicationConfig')
);
}
}
IndexController.php
<?php
declare(strict_types=1);
namespace Test\Controller;
use Laminas\Config\Config;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\ViewModel;
use Mail\Model\Mail;
class IndexController extends AbstractActionController
{
private $config;
public function __construct(array $config)
{
$this->config = $config;
}
public function indexAction()
{
$request = $this->getRequest();
if ($request->isPost()) {
$Mail = new Mail();
$Mail->send();
}
}
}
}
Mail.php
<?php
namespace Mail\Model;
use Laminas\View\Model\ViewModel;
use Mail\Model\Table\MailQueueTable;
class Mail {
public function send()
{
$MailQueueTable = new MailQueueTable();
$MailQueueTable->add();
}
}
MailQueueTable.php
<?php
declare(strict_types=1);
namespace Mail\Model\Table;
use Laminas\Db\Adapter\Adapter;
use Mail\Model\Mail;
class MailQueueTable extends AbstractTableGateway
{
protected $adapter;
protected $table = 'mail_queue';
public function __construct(Adapter $adapter)
{
// Here starts the problem...
// As far as I understand, I have to inject
// the DB Adapter in the Construct of the
// AbstractTableGateway Class...
// But no idea how to get it here...
$this->adapter = $adapter;
$this->initialize();
}
public function add()
{
// SQL Insert Statement would be here
}
}
MailQueue 表代码是基于我阅读的教程的构造函数等。如您所见,构造需要适配器。但我现在不知道如何获取适配器。
据我到目前为止所阅读的,我需要在索引控制器工厂中注入数据库适配器,然后从索引控制器中的操作到新创建的邮件实例,然后我必须将它注入到邮件队列表?
我觉得这不是正确的解决方案 - 在使用 Laminas 之前我可以写
global $DB;
我的数据库可用。
【问题讨论】:
标签: php zend-framework3 laminas