Skip to content

Instantly share code, notes, and snippets.

@belackriv
Created March 7, 2018 13:30
Show Gist options
  • Save belackriv/b1161696d6f8d6f6f4d1fc45f5f76478 to your computer and use it in GitHub Desktop.
Save belackriv/b1161696d6f8d6f6f4d1fc45f5f76478 to your computer and use it in GitHub Desktop.
Query Unit Of Work
<?php
namespace App\Library\Mixin;
trait QueryUnitOfWorkMixin
{
public function queryUow($className, array $properties)
{
foreach($this->getEntityManager()->getUnitOfWork()->getScheduledEntityInsertions() as $entity){
if(is_a($entity, $className)){
$matches = true;
foreach($properties as $propertName => $value){
$getMethodName = 'get'.ucfirst($propertName);
if(is_a($value, \DateTime::class)){
if($entity->$getMethodName() != $value){
$matches = false;
}
}else{
if($entity->$getMethodName() !== $value){
$matches = false;
}
}
}
if($matches){
return $entity;
}
}
}
return null;
}
public function getUowInsertions($className)
{
$entities = [];
foreach($this->getEntityManager()->getUnitOfWork()->getScheduledEntityInsertions() as $entity){
if(is_a($entity, $className)){
$entities[] = $entity;
}
}
return $entities;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment