原则:如何才能在查询中仅在必要时才包含 where 子句?
我想使用单个查询来检索:
- 任何类别的项目(未应用过滤器);
- 仅单个类别的项目(仅限于特定类别);
为此,我应该能够编写一个 Doctrine 查询,仅当满足特定条件(例如,部分 URL 存在)时才包含 where 子句,否则,where 子句不包含在查询中。
当然,我尝试使用 If 语句,但由于学说查询是链接的,因此会引发错误。
所以我想解决方案可能是某种(对我来说未知的)以非链接形式编写学说查询的方法(不让每一行以“->”开头,并且查询的每一行都以分号“;”结尾)
我想这样就可以使用 IF 语句。
或者,也许这个问题已经有一些非常简单的解决方案?
感谢您的回复!
I want to use a single query to retrieve:
- items of any categories (no filter applied);
- only items of a single category (limited to a particular category);
For that purpose I should be able to write a Doctrine query which would include a where clause only when certain condition is met (eg. part of URL existing), otherwise, where clause is not included in the query.
Of course, i tried with using the If statement, but since doctrine query is chained, the error is thrown.
So i guess the solution might be some (to me unknown) way of writing doctrine queries in an unchained form (by not having each row started with "->" and also having each row of a query ending with semicolon ";")
That way the usage of IF statement would be possible i guess.
Or, maybe there's already some extremely simple solution to this matter?
Thanks for your reply!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不熟悉 Codeigniter 但你不能写这样的东西吗?
I am unfamiliar with Codeigniter but can't you write something like this?
在您的模型中,将
where
的条件作为函数中的参数传递。在下面的示例中,我假设函数名称为
filter_query()
并将where
条件作为参数传递。在上面的示例中,我使用了 Codeigniter 的 Active Record 类。
In your model pass the condition for
where
as a parameter in a function.In below example i am assuming the function name to be
filter_query()
and passingwhere
condition as a parameter.In above example i have used Codeigniter's Active Record Class.