首页 > 解决方案 > 是否可以使用 Criteria 功能在 Doctrine 中加入?

问题描述

public function findActiveEvents($start, $end)
{
    $expr = Criteria::expr();
    $criteria = Criteria::create();
    $criteria->where(
           $expr->andX($expr->gte('start', $start), $expr->lte('end', $end)
    ));

    return $this->matching($criteria);
}

假设我的事件实体有一个类别,而类别有很多事件,我将如何过滤这些?

标签: symfonyjoindoctrinecriteriadql

解决方案


如果您想收集类别对象上的非活动事件,您可以使用标准类

class Category{
    protected $events; // (oneToMany)
    // ...
    protected getEvents() { // default method
        return $this->events;
    }
    protected getActiveEvents() { 
        $expr = Criteria::expr();
        $criteria = Criteria::create();
        $criteria->where(
               $expr->andX($expr->gte('start', $start), $expr->lte('end', $end)
        ));
        return $this->events->matching($criteria);
    }
}

如何在 Symfony 2 和 Doctrine 中过滤实体对象内的数据


推荐阅读