I am learning from this Question but facing issues with many operations in between:
doctrine 2 query builder and join tables
以下是我的问题。我也在前一个问题上寻求帮助。感谢那些帮助我解决问题的人。
我在Doctrine 2.3中创建了一个大查询,我不熟悉这些操作。但是我在很多人的帮助下学到了。目前我正面临3个表之间的内部联合问题。
我的联合:
SELECT *
FROM user AS u
LEFT JOIN source AS s ON u.user_source_fk=s.source_id
LEFT JOIN area AS a ON s.source_node_fk = a.area_id;
以上查询是我在Doctrine中尝试转换的内容。
我试图像这样给出查询,但它不起作用:
SELECT a FROM Ipf\Model\User u LEFT JOIN Ipf\Model\Sources ON u.user_source_fk=s.source_id LEFT JOIN Ipf\Model\Area aa ON s.source_node_fk = aa.area_id;
我的原始查询将如何:
SELECT *
FROM user AS u
LEFT JOIN source AS s ON u.user_source_fk=s.source_id
LEFT JOIN area AS a ON s.source_node_fk = a.area_id;
+
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')
)
我想使用与操作员内部联合的上述查询对查询执行内部联合Refer: Multiple Query in Doctrine with NAND,NOR,NOT,AND Operators
如何加入表并执行查询?
$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));
如何通过JOIN操作修改以实现上述查询?
有人可以帮助我解决我的问题。在教义中对我来说是噩梦......
答案 0 :(得分:1)
您查询的复杂部分实际上是WHERE
,因此您应该只需添加连接即可。
$qry = $this->manager()->createQueryBuilder()
->select(array('e', 's', 'a'))
->from($this->entity, 'e')
->leftJoin('e.sources', 's')
->leftJoin('s.node', 'a');
然后你会完成其余的查询逻辑。
需要提及的一件事是,在DQL中,您处理实体及其属性而不是表和列。
因此,在我的示例中,e.sources
需要是UserSource
实体上User
实体/集合的映射属性名称。同样,s.node
必须是Area
上UserSource
实体/集合的映射属性名称。