Skip to content

Instantly share code, notes, and snippets.

@pboethig
Created July 17, 2016 06:43
Show Gist options
  • Save pboethig/2d5103baa57d6d675f1764ecfec4b187 to your computer and use it in GitHub Desktop.
Save pboethig/2d5103baa57d6d675f1764ecfec4b187 to your computer and use it in GitHub Desktop.
<?php
/**
* Created by PhpStorm.
* User: root
* Date: 17.07.16
* Time: 07:49
*/
namespace Check\BlogBundle\Repositories;
use Check\BlogBundle\Entity\IEntity;
use Doctrine\ORM\EntityRepository;
class EntityRepositoryAbstract extends EntityRepository implements IEntityRepository
{
public function save(IEntity $entity, $data)
{
$properties = $this->getProperties($entity);
foreach ($properties as $propertyName)
{
if (property_exists($data, $propertyName))
{
$setter = 'set' . ucfirst($propertyName);
$data->{$setter}($data->{$propertyName});
}
}
$response = $this->_em->persist($entity);
$this->_em->flush();
return $response;
}
/**
* @param IEntity $entity
* @return mixed
*/
public function getProperties(IEntity $entity)
{
$metadata = $this->_em->getClassMetadata(get_class($entity));
$properties = [];
foreach ($metadata->fieldMappings as $name=>$value)
{
$properties[] = $name;
}
return $properties;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment