我想在一个控制器中使用两个模型。
Controlle: protected $ table;
/**
* Execute the request
*
* @param MvcEvent $e
* @return mixed
*/
protected $commentTable;
// Add this constructor:
public function __construct(PostTable $table,CommentTable $commTable)
{
$this->table = $table;
$this->commentTable = $commTable;
}
厂:
class PostControllerFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName, array $options = null){
$model = $container->get(PostTable::class);
return new PostController($model);
}
}
但我收到了错误:
:__ construct()必须是Post \ Model \ CommentTable的实例,没有给定,
如何在一个控制器中使用两个表?
答案 0 :(得分:2)
在PostTable
构造函数中,您只想传递CommentTable
而不是PostController
,认为在为控制器创建工厂时需要存在这些内容。所以你应该这样做
class PostControllerFactory implements FactoryInterface {
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) {
$postTable = $container->get(PostTable::class);
$commentTable = $container->get(CommentTable::class);
return new PostController($postTable, $commentTable);
}
}