1

我正在尝试设计一个学说查询,但我是学说的新手,但是在我的另一篇文章的帮助下,我想出了一个在我的 Mysql 中运行时有效的查询。但我希望它转换 Doctrine (2.3) 中的查询可以有人帮助我。

MySQL 查询:

SELECT * FROM user WHERE 
(`user_name` like '%TOM%' OR `user_name` like '%AN%' and `login_datetime` BETWEEN '2013-01-01 00:00:00' and '2013-02-31 23:59:59') OR
NOT ( --NOR
   (`user_name` like '%PHP%' OR `user_name` like '%BA%' and `login_datetime` BETWEEN '2013-02-01 00:00:00' and '2013-03-31 23:59:59') OR
   (`user_name` like '%SUN%' OR `user_name` like '%MOON%' and `login_datetime` BETWEEN '2013-03-01 00:00:00' and '2013-04-31 23:59:59')
) OR
NOT ( --NAND
   (`user_name` like '%RAJ%' OR `user_name` like '%MUTH%' and `login_datetime` BETWEEN '2013-04-01 00:00:00' and '2013-06-31 23:59:59') AND
   (`user_name` like '%BAG%' OR `user_name` like '%LAP%' and `login_datetime` BETWEEN '2013-05-01 00:00:00' and '2013-07-31 23:59:59')
)

--链接参考:对于上面的MySql Query。

我的教义尝试:参考链接:

很难理解教义查询,因为它在查询之间自动创建了 () 黄铜,所以它一直给我一些错误的结果。请帮助我。

4

1 回答 1

2

很难理解教义查询,因为它在查询之间自动创建了 () 黄铜,所以它一直给我一些错误的结果。

当您使用时,expr它通常将表达式包装在(). 我认为这就是您遇到困惑的地方。类似于以下的东西应该可以工作(这没有经过测试,所以你可能需要调整一下):

$qry = $this->manager()->createQueryBuilder()
        ->from($this->entity, 'e')
        ->select('e');


// (`user_name` like '%TOM%' OR `user_name` like '%AN%' and `login_datetime` BETWEEN '2013-01-01 00:00:00' and '2013-02-31 23:59:59')
$expr1 = $qry->expr()->andX(
    $qry->expr()->orX(
       $qry->expr()->like('e.user_name', '%TOM%'), 
       $qry->expr()->like('e.user_name', '%AN%')
    ),
    $qry->expr()->between('e.login_datetime', '2013-02-01 00:00:00', '2013-02-31 23:59:59')
);

//(`user_name` like '%PHP%' OR `user_name` like '%BA%' and `login_datetime` BETWEEN '2013-02-01 00:00:00' and '2013-03-31 23:59:59')

$expr2a = $qry->expr()->andX(
    $qry->expr()->orX(
       $qry->expr()->like('e.user_name', '%PHP%'), 
       $qry->expr()->like('e.user_name', '%BA%')
    ),
    $qry->expr()->between('e.login_datetime', ''2013-02-01 00:00:00'', '2013-03-31 23:59:59')
);

// (`user_name` like '%SUN%' OR `user_name` like '%MOON%' and `login_datetime` BETWEEN '2013-03-01 00:00:00' and '2013-04-31 23:59:59')
$expr2b = $qry->expr()->andX(
    $qry->expr()->orX(
       $qry->expr()->like('e.user_name', '%SUN%'), 
       $qry->expr()->like('e.user_name', '%MOON%')
    ),
    $qry->expr()->between('e.login_datetime', '2013-03-01 00:00:00', '2013-04-31 23:59:59')
);

// combine expr2a and expr2b with OR as $expr2

$expr2 = $qry->expr()->orX($expr2a, $expr2b);


// (`user_name` like '%RAJ%' OR `user_name` like '%MUTH%' and `login_datetime` BETWEEN '2013-04-01 00:00:00' and '2013-06-31 23:59:59')

$expr3a = $qry->expr()->andX(
    $qry->expr()->orX(
       $qry->expr()->like('e.user_name', '%RAJ%'), 
       $qry->expr()->like('e.user_name', '%MUTH%')
    ),
    $qry->expr()->between('e.login_datetime', ''2013-04-01 00:00:00'', '2013-06-31 23:59:59')
);

// (`user_name` like '%BAG%' OR `user_name` like '%LAP%' and `login_datetime` BETWEEN '2013-05-01 00:00:00' and '2013-07-31 23:59:59')
$expr3b = $qry->expr()->andX(
    $qry->expr()->orX(
       $qry->expr()->like('e.user_name', '%BAG%'), 
       $qry->expr()->like('e.user_name', '%LAP%')
    ),
    $qry->expr()->between('e.login_datetime', '2013-05-01 00:00:00', '2013-07-31 23:59:59')
);

// combine expr2a and expr2b with OR as $expr2

$expr3 = $qry->expr()->andX($expr3a, $expr3b);


// final query essentially WHERE expr1 OR NOT(expr2) OR NOT(expr3)
$qry->where($expr1)
    ->or($qry->expr()->not($expr2))
    ->or($qry->expr()->not($expr3));
于 2013-06-11T15:18:16.307 回答