Created
July 17, 2016 07:33
-
-
Save pboethig/edf1ed7009ed4b687f0a30433a6c1fc5 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 | |
| * @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