Created
September 29, 2015 16:02
-
-
Save brabijan/1bd8e25756df1f8cdb2d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<?php | |
namespace Model\Events; | |
use Doctrine\ORM\Event\PreFlushEventArgs; | |
use Kdyby\Events\Subscriber; | |
use Nette; | |
class ConvertBlankToNullSubscriber extends Nette\Object implements Subscriber | |
{ | |
public function getSubscribedEvents() | |
{ | |
return array('preFlush'); | |
} | |
public function preFlush(PreFlushEventArgs $args) | |
{ | |
$em = $args->getEntityManager(); | |
$uow = $em->getUnitOfWork(); | |
foreach (array_merge($uow->getScheduledEntityInsertions(), $uow->getScheduledEntityUpdates()) as $entity) { | |
$metadata = $em->getMetadataFactory()->getMetadataFor($entity::class); | |
foreach($metadata->getFieldNames() as $field) { | |
if($metadata->getTypeOfField($field) === "string" and $entity->$field === "") { | |
$entity->$field = null; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment