Created
July 17, 2016 06:43
-
-
Save pboethig/2d5103baa57d6d675f1764ecfec4b187 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 | |
/** | |
* 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