Skip to content

Instantly share code, notes, and snippets.

@b-b3rn4rd
Last active October 6, 2015 20:38
Show Gist options
  • Save b-b3rn4rd/3050294 to your computer and use it in GitHub Desktop.
Save b-b3rn4rd/3050294 to your computer and use it in GitHub Desktop.
Custom Doctrine Repository
<?php
namespace My\Doctrine;
use Doctrine\ORM\EntityRepository;
/**
* Custom Repository extending basic EntityRepository functionality
*
* @author Bernard Baltrusaitis <bernard@runawaylover.info>
*/
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