我对 Laminas 比较陌生,有些事情对我来说仍然没有意义 - 就 - 在我看来,如何在 laminas 中完成事情非常复杂。就我而言,现在我需要数据库适配器的实例。
项目是这样的:
我有一个 IndexController(和一个工厂),它创建(在 Post 请求的情况下)一个 Mail 类的实例,并且该 Mail 类应该在 MailQueueTable 中添加数据。但我不知道如何在 MailQueueTable 中获取 DB Adapter
源代码如下:
索引控制器工厂.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')
);
}
}
索引控制器.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();
}
}
}
}
邮件.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 表代码是基于我阅读的教程的构造函数等。如您所见,构造需要适配器。但我现在不知道如何获得适配器。
据我到目前为止所阅读的,我需要在索引控制器工厂中注入数据库适配器,然后从索引控制器中的操作到新创建的邮件实例,然后我必须将它注入到 MailQueue 表中?
我不觉得这是正确的解决方案 - 在使用 Laminas 之前我可以写
global $DB;
我有我的数据库可用。