1

我有一个查询,其中包含以下内容:

$this->db->where(array(
                 'jobs.open_intentions' => 1,
                 'jobs.open'            => 1, 
                 'jobs.pending'         => 0,
                 'jobs.awarded'         => 0));

效果很好,但我不喜欢那种格式,实际上我更愿意像这样写

$this->db->where('jobs.open_intentions', 1) 
         ->where('jobs.open', 1) 
         ->where('jobs.pending', 0)
         ->where('jobs.awarded', 0);

我知道挑剔,但无论如何,我的问题是,有什么理由我应该选择一种方法而不是另一种方法,还是只是偏好问题?

4

3 回答 3

1

从语义上讲,后者(多个wheres)对我来说更像是多个AND- 从句。对于前者,意图不清楚(不查看 API 文档)。

于 2012-08-29T02:20:12.197 回答
1

虽然后面的方法是常用的,但为了简单和清晰的代码,你也可以这样做

$where  =    array(
             'jobs.open_intentions' => 1,
             'jobs.open'            => 1, 
             'jobs.pending'         => 0,
             'jobs.awarded'         => 0
             );
$this->db->where($where);
于 2012-08-29T09:12:20.237 回答
0

There is one advantage i see in the later one which is you can use different where clauses e.g. or_where, where_in, or_where_in with clean code which is pretty difficult in the earlier one. But if you are using a single operator then both are same.

于 2012-08-30T09:51:28.790 回答