Propel 1.5 如何使用 OR 条件进行连接
我需要使用 Propel 构建标准生成这样的 SQL:
"SELECT *
FROM `table1`
LEFT JOIN table2 ON ( table1.OBJECT_ID = table2.ID )
LEFT JOIN table3 ON ( table1.OBJECT_ID = table3.ID )
LEFT JOIN table4 ON ( table4.USER_ID = table2.ID
OR table4.USER_ID = table3.AUTHOR_ID )"
Is it possible to make join with or condition?或者也许还有其他方式? 推进1.5
I need to generate such SQL using Propel build criteria:
"SELECT *
FROM `table1`
LEFT JOIN table2 ON ( table1.OBJECT_ID = table2.ID )
LEFT JOIN table3 ON ( table1.OBJECT_ID = table3.ID )
LEFT JOIN table4 ON ( table4.USER_ID = table2.ID
OR table4.USER_ID = table3.AUTHOR_ID )"
Is it possible to make join with or condition? Or maybe some other ways?
Propel 1.5
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
useTable2Query()
是必要的,因为您的信息似乎暗示Table4
与Table2
相关,而不是与Table1
相关,因此直接将Table4
连接到Table1
将导致一系列致命的 Propel 错误。 “使用”功能弥合了这种关系。useTable2Query()
is necessary because your information seems to imply thatTable4
is related toTable2
and not toTable1
, and so joiningTable4
directly toTable1
will result in a series of fatal Propel errors. The "use" functionality bridges that relationship.如果我没记错的话,前两个连接(table2、table3)很简单。只需在您的架构中将 table1.OBJECT_ID 设置为非必需,左连接就会自动使用。
不能立即确定 OR 连接。如果您遇到困难,一种方法是在原始查询中使用上述内容,然后从结果集中“水合”对象。另一种方法(对于在 ORM 中难以表达的复杂查询非常有用)是为上述内容创建一个数据库视图,然后在该视图的架构中添加一个新表。这确实有点作弊,但对于我在大型 symfony 项目中所做的一些非常复杂的主从细节事情来说,它非常棒 - 而且它也使查询调试变得非常容易。
The first two joins (table2, table3) are easy, if I recall correctly. Just make table1.OBJECT_ID non-required in your schema, and the left join will be used automatically.
Not immediately sure about the OR join. If you get stuck, one way to do it is to use the above in a raw query, and then "hydrate" objects from the resultset. Another way (very good for complex queries that are a pain to express in an ORM) is to create a database view for the above, and then add a new table in your schema for the view. It's cheating a bit for sure, but for some really complex master-detail things I did in a large symfony project, it was great - and it made query debugging really easy as well.