Hello everyone. I’m trying to create service with factories.
I have many entities and I do not want to create factory for each of them. So I create an abstract factory.
But when I try to access to my mapper, I get this error :
Service with name “Commun\Model\Repository\RefUtilisateurRepository” could not be created. Reason: Could not resolve value for parameter “hydrator” of type Laminas\Hydrator\HydratorInterface in class Commun\Model\Repository\RefUtilisateurRepository (requested as Commun\Model\Repository\RefUtilisateurRepository)
Here is my code :
My abstract factory
<?php
/**
* @module Commun
* @subpackage Model/Repository
* @author Samuel NANGUI <nanguisamuel@gmail.com>
* @copyright Copyright (c) 2020 Nslabs
*/
namespace Commun\Factory\Repository;
use Interop\Container\ContainerInterface;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Hydrator\ReflectionHydrator;
use Laminas\ServiceManager\Factory\AbstractFactoryInterface;
class NslabsAbstractRepositoryFactory implements AbstractFactoryInterface
{
/**
* @param ContainerInterface $container
* @param string $requestedName
* @param null|array $options
* @return $requestedName Entity
*/
public function canCreate(ContainerInterface $container, $requestedName)
{
return ((substr($requestedName, -10) === 'Repository') && class_exists($requestedName));
}
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$explode = explode("\\", $requestedName);
$entity=substr($explode[3],0,strlen($explode[3])-10);
$entityPath = "Commun\Model\Entity\\";
$absoluteEntityPath = $entityPath.$entity;
return new $requestedName(
$container->get(AdapterInterface::class),
new ReflectionHydrator(),
new $absoluteEntityPath()
);
}
}
My repository :
<?php
/**
* @module Commun
* @subpackage Model/Repository
* @author Allround automation by Repository Creator
* @created on 05-05-2021 17:05:48
* @copyright Copyright (c) 2020 Nslabs by Samuel NANGUI <nanguisamuel@gmail.com>
*/
namespace Commun\Model\Repository;
use Commun\Model\AbstractTable;
use Commun\Model\Entity\RefUtilisateur;
use Commun\Model\Interfaces\RefUtilisateurInterface;
use Laminas\Hydrator\HydratorInterface;
use Laminas\Db\Adapter\AdapterInterface;
use Laminas\Db\Sql\Sql;
class RefUtilisateurRepository extends AbstractTable implements RefUtilisateurInterface
{
/**
*@var RefUtilisateurInterface
*/
protected $tablename="RefUtilisateur";
public function __construct(AdapterInterface $db,HydratorInterface $hydrator, RefUtilisateur $refutilisateur){
parent::__construct($db,$hydrator,$refutilisateur,$this->tablename);
}
}
My module.config.php
<?php
/**
* @module Commun
* @subpackage Module
* @author Samuel NANGUI <nanguisamuel@gmail.com>
* @copyright Copyright (c) 2020 Nslabs
*/
namespace Commun;
return [
'service_manager' => [
/* We will use aliases to access repository */
'aliases' => [
'TSTATCONNEXION' => Model\Repository\StatConnexionRepository::class,
'TSPECIALITEPROFESSIONEL' => Model\Repository\SpecialiteProfessionelRepository::class,
'TROLEUTILISATEUR' => Model\Repository\RoleUtilisateurRepository::class,
'TREPARTITION' => Model\Repository\RepartitionRepository::class,
'TRENDEZVOUS' => Model\Repository\RendezVousRepository::class,
'TREFUTILISATEUR' => Model\Repository\RefUtilisateurRepository::class,
'TREFSTATUTREVERSEMENT' => Model\Repository\RefStatutReversementRepository::class,
'TREFSTATUTRAPPROCHEMENTRELEVE' => Model\Repository\RefStatutRapprochementReleveRepository::class,
'TREFSTATUTPAIEMENT' => Model\Repository\RefStatutPaiementRepository::class,
'TREFSTATUTECHANGE' => Model\Repository\RefStatutEchangeRepository::class,
'TREFSTATUTDETAIL' => Model\Repository\RefStatutDetailRepository::class,
'TREFSTATUTCONFIRMATIONRDV' => Model\Repository\RefStatutConfirmationRdvRepository::class,
'TREFSTATUTCOMPTE' => Model\Repository\RefStatutCompteRepository::class,
'TREFSTATUTBROADCAST' => Model\Repository\RefStatutBroadcastRepository::class,
'TREFSTATUTACTIVITE' => Model\Repository\RefStatutActiviteRepository::class,
'TREFSPECIALITE' => Model\Repository\RefSpecialiteRepository::class,
'TREFSEXE' => Model\Repository\RefSexeRepository::class,
'TREFSECTEUR' => Model\Repository\RefSecteurRepository::class,
'TREFROLE' => Model\Repository\RefRoleRepository::class,
'TREFPAYS' => Model\Repository\RefPaysRepository::class,
'TREFOPERATEUR' => Model\Repository\RefOperateurRepository::class,
'TREFMINUTE' => Model\Repository\RefMinuteRepository::class,
'TREFMETIER' => Model\Repository\RefMetierRepository::class,
'TREFJOUR' => Model\Repository\RefJourRepository::class,
'TREFHEURE' => Model\Repository\RefHeureRepository::class,
'TREFDOC' => Model\Repository\RefDocRepository::class,
'TPAIEMENT' => Model\Repository\PaiementRepository::class,
'THORAIRESPROFESSIONNEL' => Model\Repository\HorairesProfessionnelRepository::class,
'TGROUPEPAIEMENT' => Model\Repository\GroupePaiementRepository::class,
'TDOC' => Model\Repository\DocRepository::class,
'TDETAILBROADCAST' => Model\Repository\DetailBroadcastRepository::class,
'TBROADCASTRDV' => Model\Repository\BroadcastRdvRepository::class,
'translator' => 'MvcTranslator',
],
]
I need to say that I have already did it in previous project and everything worked fine. I don’t knwon what’s going on.
Any help will be appreciated. Thanks