【发布时间】:2011-08-01 07:34:16
【问题描述】:
我正在使用 TinyMVC 框架,它允许“以编程方式构建 MySQL 查询,很像活动记录。”示例嵌入此处(relevant TinyMVC documentation):
class Members_Model extends TinyMVC_Model
{
function get_members()
{
$this->db->select('foo,bar,baz'); // set selected columns
$this->db->from('mytable'); // set from what table(s)
$this->db->where('foo','test'); // where foo='test'
$this->db->orwhere('foo=? and bar=?',array('test','test2')) // where foo='test' and bar='test2'
$this->db->join('jointable','mytable','jointable.foo=mytable.foo'); // join tables on (optional) condition
$this->db->in('mycolumn',$elements,$islist,$prefix) // IN clause: column, elements (comma-separated or array), $list=boolean is list or array, $prefix: AND|OR
$this->db->orderby('ordercolumn'); // order by column(s)
$this->db->groupby('groupbycolumn'); // group by column(s)
$this->db->limit($limit,$offset); // query limit, optional offset
$this->db->query();
while($row = $this->db->next()) {
$rows[] = $row;
}
return $rows;
}
}
这与直接编写 SQL 查询有何不同或更好:
SELECT foo, bar, baz
FROM mytable
WHERE...
【问题讨论】:
-
好吧,我来自 .Net 世界,不应该发表评论,但这很奇怪。我必须写很多次
$this->db->。应该更流畅。 -
绝对不是为了可读性。
标签: php mysql sql frameworks