我的数据库(mysql)表使用时间戳列,每当我想在查询中返回它们时,我都希望它们被查询为“UNIX_TIMESTAMP( columns )”。
如何在zend框架中轻松地修改查询来实现这一点?
例如,当前代码为:
select = $this->select();
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);这最终会变成:
select * from tablename where user_id = 42;我想要自动找到时间戳列并将结果查询更改为:
select user_id,name,unix_timestamp(created) where user_id = 42;我知道我可以使用MySQL视图来实现这一点,但我宁愿避免这样做。
谢谢。
RR
发布于 2012-04-05 05:27:49
您应该能够使用$ select ->from()对象在select中指定您想要的字段。
你应该会得到类似这样的结果。
$select = $this->select();
$select->from(
array('t' => 'tablename'),
array('user_id', 'name', 'UNIX_TIMESTAMP(created)')
);
$select->where('user_id = ?',$user_id);
return $this->fetchAll($select);如果要运行函数中没有括号的表达式,请使用Zend_Db_Expr()方法正确转义查询。
https://stackoverflow.com/questions/10019012
复制相似问题