Last active
October 6, 2015 20:38
-
-
Save b-b3rn4rd/3050294 to your computer and use it in GitHub Desktop.
Custom Doctrine Repository
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 | |
namespace My\Doctrine; | |
use Doctrine\ORM\EntityRepository; | |
/** | |
* Custom Repository extending basic EntityRepository functionality | |
* | |
* @author Bernard Baltrusaitis <[email protected]> | |
*/ | |
class Repository extends EntityRepository | |
{ | |
/** | |
* Prepare attributes for entity | |
* replace foreign keys with entity instances | |
* | |
* @param array $attributes entity attributes | |
* @return array modified attributes values | |
*/ | |
public function prepareAttributes(array $attributes) | |
{ | |
foreach ($attributes as $fieldName => &$fieldValue) { | |
if (!$this->getClassMetadata()->hasAssociation($fieldName)) { | |
continue; | |
} | |
$association = $this->getClassMetadata() | |
->getAssociationMapping($fieldName); | |
$fieldValue = $this->getEntityManager() | |
->getReference($association['targetEntity'], $fieldValue); | |
unset($fieldValue); | |
} | |
return $attributes; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment