This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added $data to the DoctrineResourceEvent allowing access to original …
…data submitted to the resource method. Implemented an Event Listener to handled embedded toMany collections submitted to the Resource.
- Loading branch information
1 parent
f7965f8
commit 29c23d1
Showing
3 changed files
with
196 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace ZF\Apigility\Doctrine\Server\Event\Listener; | ||
|
||
use Zend\EventManager\ListenerAggregateInterface; | ||
use Zend\EventManager\EventManagerInterface; | ||
use ZF\Apigility\Doctrine\Server\Event\DoctrineResourceEvent; | ||
use Zend\Stdlib\Response; | ||
use ZF\Apigility\Doctrine\Server\Resource\DoctrineResource; | ||
use Zend\Stdlib\Hydrator\HydratorAwareInterface; | ||
use DoctrineModule\Stdlib\Hydrator\DoctrineObject; | ||
|
||
/** | ||
* Class CollectionListener | ||
* | ||
* @package ZF\Apigility\Doctrine\Server\Event\Listener | ||
*/ | ||
class CollectionListener implements ListenerAggregateInterface { | ||
protected $listeners = array(); | ||
|
||
public function attach( EventManagerInterface $events ) { | ||
$this->listeners[] = $events->attach( | ||
DoctrineResourceEvent::EVENT_UPDATE_PRE, | ||
array( $this, 'handleCollections' ) | ||
); | ||
|
||
$this->listeners[] = $events->attach( | ||
DoctrineResourceEvent::EVENT_CREATE_PRE, | ||
array( $this, 'handleCollections' ) | ||
); | ||
} | ||
|
||
public function handleCollections( DoctrineResourceEvent $event ) { | ||
$objectManager = $event->getObjectManager(); | ||
$entity = $event->getEntity(); | ||
$originalData = (array) $event->getData(); | ||
|
||
$metadata = $objectManager->getClassMetadata( get_class( $entity ) ); | ||
|
||
$associations = $metadata->getAssociationNames(); | ||
|
||
foreach ( $associations as $association ) { | ||
// Skip handling associations that arent in the data | ||
if ( $metadata->isCollectionValuedAssociation( $association ) ) { | ||
if ( array_key_exists( $association, $originalData ) | ||
&& ! empty( $originalData[ $association ] ) | ||
&& ( is_array( $originalData[ $association ] ) || $originalData[ $association ] instanceof \Traversable ) | ||
) { | ||
foreach ( $originalData[ $association ] as &$subEntityData ) { | ||
$target = $metadata->getAssociationTargetClass( $association ); | ||
$identifierNames = $metadata->getIdentifierFieldNames( $target ); | ||
if ( empty( $identifierNames ) ) { | ||
continue; // TODO Investigate what we should do here, throw exception? for now handle downstream | ||
} | ||
|
||
$identifierValues = [ ]; | ||
foreach ( $identifierNames as $identifierName ) { | ||
if ( ! isset( $subEntityData[ $identifierName ] ) || empty( $subEntityData[ $identifierName ] ) ) { | ||
continue; | ||
} | ||
$identifierValues[ $identifierName ] = $subEntityData[ $identifierName ]; | ||
} | ||
|
||
$subEntity = false; | ||
if ( count( $identifierValues ) === count( $identifierNames ) ) { | ||
$subEntity = $objectManager->find( $target, $identifierValues ); | ||
} | ||
|
||
if(!$subEntity){ | ||
$subEntity = new $target; | ||
} | ||
|
||
// TODO This could cause a catastrophe as it wouldnt have the appropriate strategies/listeners | ||
// probably should fetch from "hydrator-manager" if possible | ||
$hydrator = new DoctrineObject( $objectManager ); | ||
$hydrator->hydrate($subEntityData,$subEntity); | ||
$objectManager->persist($subEntity); | ||
$objectManager->flush(); | ||
|
||
$subEntityData = $hydrator->extract($subEntity); | ||
|
||
foreach ( $identifierNames as $identifierName ) { | ||
$identifierValues[ $identifierName ] = $subEntityData[ $identifierName ]; | ||
} | ||
$subEntityData = $identifierValues; | ||
} | ||
} | ||
} | ||
} | ||
|
||
$event->setData($originalData); | ||
return $originalData; | ||
} | ||
|
||
public function detach( EventManagerInterface $events ) { | ||
foreach ( $this->listeners as $index => $listener ) { | ||
if ( $events->detach( $listener ) ) { | ||
unset( $this->listeners[ $index ] ); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.