Skip to content

Instantly share code, notes, and snippets.

@brabijan
Created September 29, 2015 16:02
Show Gist options
  • Save brabijan/1bd8e25756df1f8cdb2d to your computer and use it in GitHub Desktop.
Save brabijan/1bd8e25756df1f8cdb2d to your computer and use it in GitHub Desktop.
<?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