doctrine查询WHERE u.status = 1 AND(u.type = 1 OR u.type = 2)

时间:2014-01-31 18:41:32

标签: php codeigniter doctrine-orm

我正在尝试构建搜索表单。目前我填写的数据如下:

 private function _get_results() {
    $search_data = $this->input->post('type');
    if ($search_data) {
        $search_data = implode(' OR u.type = ', $search_data);
        $search_array[] = array(
            'key' => 'u.type',
            'value' => $search_data,
            'operand' => 'eq');
    if (count($search_array)) {
        $results = $this->accounts_model->get_search_results($search_array);
    }

这是我的型号代码。

function get_search_results(
$params = array(), 
$single_result = false, 
$order_by = array('order_by' => 'u.id', 'direction' => 'DESC')
) {
    $qb = $this->doctrine->em->createQueryBuilder();
    $qb->select('u');
    $qb->from($this->_entity, 'u');
    $qb->where($qb->expr()->eq('u.status', 1));

    foreach ($params as $param) {
        $qb->andWhere(
                $qb->expr()->$param['operand']($param['key'], $param['value'])
        );
    }
    $qb->orderBy($order_by['order_by'], $order_by['direction']);
    $qb->setFirstResult(0);
    $qb->setMaxResults(20);
    echo $qb->getQuery()->getDql() . '<br/>';
    die;
    $result = $qb->getQuery()->getResult();
    return $result;
}

echo $qb->getQuery()->getDql() . '<br/>';行返回此结果:

SELECT u FROM Entities\Account u WHERE u.status = 1 AND (u.type = 1 OR u.type = 2) ORDER BY u.id DESC

有没有办法避免使用implode()来获得相同的结果:...AND (u.type = 1 OR u.type = 2)

我正在使用codeigniter btw。

2 个答案:

答案 0 :(得分:0)

这是IN语句的来源,并且queryBuilder支持它可以传入一组值的内置。试试这个:

$qb->andWhere('u.type IN (:types)')->setParameter('types', $search_data);

答案 1 :(得分:0)

感到愚蠢回答我自己的问题,但这是我的解决方案。 在控制器中:

private function _get_results() {
    $counter = 0;
    $search_data = $this->input->post('type');
    if ($search_data) {
        $search_array[$counter] = array(
            'operand_main' => 'orX');
        foreach($search_data as $key=>$data){
             $search_array[$counter]['expr'][] = 
                array(
                    'key' => 'u.type',
                    'value' => $data,
                    'operand' => '='

            );
        }
        $counter++;
    }
    if (count($search_array)) {
        $results = $this->accounts_model->get_search_results($search_array);
    }
}

在模型中:

function get_search_results(
    $params = array(), 
    $single_result = false, 
    $order_by = array('order_by' => 'u.id', 'direction' => 'DESC')
    ){
    $qb = $this->doctrine->em->createQueryBuilder();
    $qb->select('u');
    $qb->from($this->_entity, 'u');
    $qb->where($qb->expr()->eq('u.status', 1));
    foreach ($params as $key => $main_param) {
        $Expr = $qb->expr()->$main_param['operand_main']();
        foreach ($main_param['expr'] as $param) {
            $Expr->add($param['key'] . ' ' . $param['operand'] . ' ' . $param['value']);
            }
        $qb->andWhere($Expr);
    }
    $qb->orderBy($order_by['order_by'], $order_by['direction']);
    $qb->setFirstResult(0);
    $qb->setMaxResults(20);
    echo $qb->getQuery()->getDql() . '<br/>';
    die;
    $result = $qb->getQuery()->getResult();
    return $result;
}

echo $qb->getQuery()->getDql() . '<br/>';行返回:

SELECT u FROM Entities\Account u WHERE u.status = 1 AND (u.type = 1 OR u.type = 2) ORDER BY u.id DESC

希望这对其他人有用。