Skip to content

Instantly share code, notes, and snippets.

@pboethig
Created July 17, 2016 07:33
Show Gist options
  • Select an option

  • Save pboethig/edf1ed7009ed4b687f0a30433a6c1fc5 to your computer and use it in GitHub Desktop.

Select an option

Save pboethig/edf1ed7009ed4b687f0a30433a6c1fc5 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
* @package Check\BlogBundle\Repositories
*/
class EntityRepositoryAbstract extends EntityRepository implements IEntityRepository
{
/**
* @param IEntity $entity
* @param array $data
* @return array
*/
public function save(IEntity $entity, $data = array())
{
try
{
$properties = $this->getProperties($entity);
foreach ($properties as $propertyName)
{
if (property_exists($data, $propertyName))
{
$setter = 'set' . ucfirst($propertyName);
$entity->{$setter}($data->{$propertyName});
}
}
$response = $this->_em->persist($entity);
$this->_em->flush();
}
catch (\Exception $e)
{
return array('error' => $e->getMessage(), 'code'=>$e->getCode());
}
return array('success'=>array('id'=>$entity->getId()));
}
/**
* @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