php - Dependency injection with custom Doctrine 2 hydrator -
php - Dependency injection with custom Doctrine 2 hydrator -
i'm setting custom hydrator in doctrine 2 in symfony 2 project, needs requires service. documentation custom hydrators shows how provide hydrator class, there's no way inject dependencies.
for example:
$em->getconfiguration()->addcustomhydrationmode('customhydrator', 'myproject\hydrators\customhydrator');
i suspect doctrine initialising hydrators , such dependencies need passed through other doctrine classes first.
is there way provide custom "hydration factory" or similar doctrine allow injection of additional dependencies? custom hydrators seem limited without capability.
answer: denis v
i got working follows. can't post actual code i've set dummy placeholders can see how fits together.
src/acme/examplebundle/resources/config/services.yml
services: doctrine.orm.entity_manager.abstract: class: acme\examplebundle\entity\doctrineentitymanager factory_class: acme\examplebundle\entity\doctrineentitymanager factory_method: create abstract: true calls: - [ setmydependency, [@acme.my_custom_service]]
src/acme/examplebundle/entity/doctrineentitymanager.php
namespace acme\examplebundle\entity; utilize acme\examplebundle\hydrator\myhydrator; utilize doctrine\common\eventmanager; utilize doctrine\dbal\connection; utilize doctrine\orm\configuration; utilize doctrine\orm\entitymanager baseentitymanager; utilize doctrine\orm\ormexception; utilize doctrine\orm\query; class doctrineentitymanager extends baseentitymanager { protected $mydependency; /** * note: must redefined doctrine's own entity manager has own class name hardcoded in. */ public static function create($conn, configuration $config, eventmanager $eventmanager = null) { if (!$config->getmetadatadriverimpl()) { throw ormexception::missingmappingdriverimpl(); } switch (true) { case (is_array($conn)): $conn = \doctrine\dbal\drivermanager::getconnection( $conn, $config, ($eventmanager ?: new eventmanager()) ); break; case ($conn instanceof connection): if ($eventmanager !== null && $conn->geteventmanager() !== $eventmanager) { throw ormexception::mismatchedeventmanager(); } break; default: throw new \invalidargumentexception("invalid argument: " . $conn); } homecoming new self($conn, $config, $conn->geteventmanager()); } public function setmydependency($mycustomservice) { $this->mydependency = $mycustomservice; } public function newhydrator($hydrationmode) { if ($hydrationmode == 'myhydrationmode') { homecoming new myhydrator($this, $this->mydependency); } homecoming parent::newhydrator($hydrationmode); } }
src/acme/examplebundle/hydrator/myhydrator.php
namespace acme\examplebundle\hydrator; utilize doctrine\orm\entitymanager; utilize doctrine\orm\internal\hydration\objecthydrator; class myhydrator extends objecthydrator { protected $mydependency; public __construct(entitymanager $em, $mydependency) { parent::__construct($em); $this->mydependency = $mydependency; } protected function hydratealldata() { /* hydration stuff dependency here */ } }
try adding in config.yml
doctrine: orm: hydrators: customhydrator: myproject\hydrators\customhydrator
update
as cannot inject hydrator itself, can instead create custom entitymanager (that suggested yourself).
it can done way:
services: name_of_your_custom_manager: class: %doctrine.orm.entity_manager.class% factory_service: doctrine factory_method: getmanager arguments: ["name_of_your_custom_manager"] calls: - [ setcustomdependency, ["@acme_bundle.custom_dependency"] ]
php symfony2 orm doctrine2 doctrine
Comments
Post a Comment