php zend framework3 在控制器中如何访问Model?

官方教程是:在控制器中的构造函数中写上需要访问的Model参数。
现在有一个控制器:CommentController ,构造函数如下:

private $commentService;
private $questionService;
    
public function __construct(CommentInterface $commentService,QuestionInterface $questionService)
{
    $this->commentService = $commentService;
    $this->questionService = $questionService;
}

然后工厂类 CommentControllerFactory 里面实例化:

public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
    return new CommentController($container->get(CommentInterface::class),$container->get(QuestionInterface::class));
}

Model 如下

use Zend\Db\Adapter\AdapterInterface;

class Comment implements CommentInterface
{
    private $db;
    
    public function __construct(AdapterInterface $db)
    {
        $this->db = $db;
    }
    
    ...
}

现在我想直接在控制器里面访问Test Model

class Comment {
    ...
    
    public function testAction(){
        $this->testService = new Test();
    }
}

Test Model 如下:

use Zend\Db\Adapter\AdapterInterface;

class Test
{
    private $db;
    
    public function __construct(AdapterInterface $db)
    {
        $this->db = $db;
    }
}

请问应该如何实现?还是说框架机制不允许,在官网上没有找到答案

阅读 1.6k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进