Skip to content

Instantly share code, notes, and snippets.

@yethee
Created March 15, 2011 19:41
Show Gist options
  • Save yethee/871301 to your computer and use it in GitHub Desktop.
Save yethee/871301 to your computer and use it in GitHub Desktop.
<?php
abstract class DoctrineRepository extends \Doctrine\ORM\EntityRepository
{
public function add($entity)
{
$this->checkEntity($entity);
$this->_em->persist($entity);
}
public function remove($entity)
{
$this->checkEntity($entity);
$this->_em->remove($entity);
}
private function checkEntity($entity)
{
if (!is_object($entity)) {
throw new \UnexpectedValueException(sprintf(
'An object was expected, given %s.', gettype($entity)));
}
if (!$entity instanceOf $this->_class->name) {
throw new \UnexpectedValueException(sprintf(
'%s is not an instance of %s.', get_class($entity), $this->getEntityName()));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment