Got a basic Discrimination map going on in Doctrine
We have a base "Post" Entity, which maps various other Post types via a Category like scenario. This was done because each category has a wildly different unique data structure to it, but shares a few columns of similarity to every post.
My problem is that a specific couple of pages want to display the results of only a few categories, and some of the ordering on a specific post type is messing up.
The problem is, that the way they want this to organize, is by the common dateAdded
field in the base Post entity, but for a specific post type to INSTEAD use a different field ONLY AVAILABLE to it via a field in it's own schema. This field is called releasedDate
So I tried this out:
public function postsByType($types = [], $limit = null, $onlyPublished = true)
{
$qb = $this->getEntityManager()->createQueryBuilder();
$qb->select('p')->from($this->getEntityName(), 'p');
// Query for types. Without types, gets all types
$instances = $qb->expr()->orX();
foreach($types as $key=>$advtype){
$instances->add("p INSTANCE OF :type_$key");
$qb->setParameter("type_$key", $this->getEntityManager()->getClassMetadata('CommonBundle\Entity\\'.$advtype));
}
if($instances->count())
$qb->andWhere($instances);
if($onlyPublished)
$qb->andWhere('p.published = :published')->setParameter('published', true);
$qb->addOrderBy('p.releaseDate', 'DESC')
->addOrderBy('p.dateAdded', 'DESC');
if($limit)
$qb->setMaxResults($limit);
$query = $qb->getQuery();
$results = $query->getResult();
return $results;
}
But naturally this produces the error:
[Semantical Error] line 0, col 211 near 'releaseDate': Error: Class CommonBundle\Entity\Post has no field or association named releaseDate
Aside from fetching all posts and running a custom quick sort through them and returning back the results requested, I'm not entirely sure what else to do..