dtpd58676 2015-08-19 19:41
浏览 27

使用Doctrine按子类字段排序

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..

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路