【发布时间】:2021-01-19 13:42:31
【问题描述】:
我已经看到很多关于我的问题的帖子,但没有一个能够解决它..
确实,我为我的 FournitureTable 模型做了一个工厂,返回的错误是:
Unable to resolve service "Fourniture\Model\FournitureTable" to a factory; are you certain you provided it during configuration?
这是我的模块的主要结构:
... Fourniture
/config/module.config.php
/src/Controller/FournitureController.php
/Factory/FournitureControllerFactory.php
/Model
/Factory/FournitureTableFactory.php
Fourniture.php
FournitureTable.php
Module.php # Which only contains getConfig() method
FournitureTable.php
<?php
namespace Fourniture\Model;
use RuntimeException;
use Zend\Db\TableGateway\TableGatewayInterface;
use Zend\Db\Sql\Expression;
use Zend\Db\Sql\Select;
class FournitureTable {
private $tableGateway;
public function __construct(TableGatewayInterface $tableGateway){
$this->tableGateway = $tableGateway;
}
public function fetchAll(){
return $this->tableGateway->select();
}
public function getSupplyByCategId($id){
$select = $this->tableGateway->getSql()->select()
->join(['sc' => 'sous_categorie'], 'fournitures.categorie = sc.id', ['nom'],
Select::JOIN_LEFT)
->where(['fournitures.categorie' => (int)$id]);
$result = $this->tableGateway->selectWith($select);
return $result;
}
}
?>
FournitureTableFactory.php
<?php
namespace Fourniture\Model\Factory;
use Fourniture\Model\Fourniture;
use Fourniture\Model\FournitureTable;
use Interop\Container\ContainerInterface;
use Interop\Container\Exception\ContainerException;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Adapter\AdapterInterface;
use Zend\Db\TableGateway\TableGateway;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
class FournitureTableFactory implements FactoryInterface
{
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$dbAdapter = $container->get(AdapterInterface::class);
$resultSetPrototype = new ResultSet();
$resultSetPrototype->setArrayObjectPrototype(new Fourniture());
$tableGateway = new TableGateway('fournitures', $dbAdapter, null, $resultSetPrototype);
return new FournitureTable($tableGateway);
}
}
module.config.php
<?php
//@NOTE : Se référer aux commentaires de /module/Application/config/module.config.php pour le routage, les contrôleurs et les vues
use Fourniture\Model\FournitureTable;
use Fourniture\Controller\FournitureController;
use Fourniture\Factory\FournitureControllerFactory;
use Fourniture\Model\Factory\FournitureTableFactory;
use Zend\Router\Http\Segment;
return [
'controllers' => [
'factories' => [
Fourniture\Controller\FournitureController::class => Fourniture\Factory\FournitureControllerFactory::class,
Fourniture\Model\FournitureTable::class => Fourniture\Model\Factory\FournitureTableFactory::class,
],
],
'view_manager' => [
'template_path_stack' => [
'fourniture' => __DIR__ . '/../view',
__DIR__ . '/../view',
],
],
];
?>
我是 Zend 3 和 StackOverflow 的新手,如果我的解释有点混乱,很抱歉。
如果我的英语不完美也很抱歉,我是法国人。
提前致谢!
【问题讨论】:
标签: php zend-framework model