Propel 1.5 如何使用 OR 条件进行连接

发布于 2024-12-06 19:26:28 字数 357 浏览 0 评论 0原文

我需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

胡大本事 2024-12-13 19:26:28
Table1Query::create()
  ->leftJoinTable2()
  ->leftJoinTable3()
  ->useTable2Query()
    ->leftJoinTable4()
  ->endUse()
  ->condition('cond1', Table4::USER_ID . ' = ' . Table2::ID)
  ->condition('cond2', Table4::USER_ID . ' = ' . Table3::AUTHOR_ID)
  ->combine(array('cond1', 'cond2'), Criteria::LOGICAL_OR, 'onClause')
  ->setJoinCondition('Table4', 'onClause')
  ->find();

useTable2Query() 是必要的,因为您的信息似乎暗示 Table4Table2 相关,而不是与 Table1 相关,因此直接将 Table4 连接到 Table1 将导致一系列致命的 Propel 错误。 “使用”功能弥合了这种关系。

Table1Query::create()
  ->leftJoinTable2()
  ->leftJoinTable3()
  ->useTable2Query()
    ->leftJoinTable4()
  ->endUse()
  ->condition('cond1', Table4::USER_ID . ' = ' . Table2::ID)
  ->condition('cond2', Table4::USER_ID . ' = ' . Table3::AUTHOR_ID)
  ->combine(array('cond1', 'cond2'), Criteria::LOGICAL_OR, 'onClause')
  ->setJoinCondition('Table4', 'onClause')
  ->find();

useTable2Query() is necessary because your information seems to imply that Table4 is related to Table2 and not to Table1, and so joining Table4 directly to Table1 will result in a series of fatal Propel errors. The "use" functionality bridges that relationship.

塔塔猫 2024-12-13 19:26:28

如果我没记错的话,前两个连接(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文