Skip to content

Instantly share code, notes, and snippets.

@makasim
Created December 2, 2011 11:25
Show Gist options
  • Save makasim/1422902 to your computer and use it in GitHub Desktop.
Save makasim/1422902 to your computer and use it in GitHub Desktop.
[doctrine2] quering entities with STI (Single table inheritance) pattern
<?php
/** @ORM\Table(name="file")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="function", type="smallint")
* @ORM\DiscriminatorMap({
* "1" = "\..\File\CvFile",
* "2" = "\..\File\CvWebFile",
* "3" = "\..\File\KbisFile"
* })
*/
class File extends BaseFile
{
// ..
}
// let's say we have relation member mant-to-many file
// how to select only CvWebFile???
class MemberFileRepository extends EntityRepository
{
public function findOneCvWebByMember(Member $member)
{
$q = $this->createQueryBuilder('mf')
->join('mf.file', 'f')
->where('mf.member = :member')
->andWhere('f INSTANCE OF \..\File\CvWebFile')
->setParameter('member', $member)
->getQuery();
return $q->getOneOrNullResult();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment