【发布时间】:2011-10-22 16:22:42
【问题描述】:
基本上我想创建一个伪列,我将按照它进行排序。这是我的 SQL 查询
SELECT I.*, ((I.width*175)/I.height) as relativeWidth
FROM Image I
order by relativeWidth asc
如何在不编写直接 SQL 的情况下在推进中做到这一点?而且我不想创建视图和查询它。
【问题讨论】:
标签: php database sorting propel
基本上我想创建一个伪列,我将按照它进行排序。这是我的 SQL 查询
SELECT I.*, ((I.width*175)/I.height) as relativeWidth
FROM Image I
order by relativeWidth asc
如何在不编写直接 SQL 的情况下在推进中做到这一点?而且我不想创建视图和查询它。
【问题讨论】:
标签: php database sorting propel
您是否使用 Criteria(创建 where 子句的旧方法)?如果是这样,您可以这样做:
$c = new Criteria();
$c->addSelectColumn(
'((' . IPeer::WIDTH . '*175)/' . IPeer::HEIGHT . ') AS relativeWidth'
);
$c->addAscendingOrderByColumn('relativeWidth');
$rows = IPeer::doSelect($c);
您还需要覆盖行类 (I) 中的 hydrate() 方法以捕获额外的列(未经测试):
public function hydrate($row, $startcol = 0, $rehydrate = false)
{
$startcol = parent::hydrate($row, $startcol, false);
$this->relativeWidth = ($row[$startcol] !== null) ? (float) $row[$startcol] : null;
$this->resetModified();
$this->setNew(false);
if ($rehydrate) {
$this->ensureConsistency();
}
return $startcol + 1;
}
当然,最后你需要一个获取新值的 getter,但这很简单。
如果您使用的是查询系统,可能有类似的方法可以做到这一点,尽管我不太熟悉。
(编辑:为正确性添加了返回值。)
【讨论】:
这些要求在 Propel 1.5+ 版本中是可能的。 强烈建议您升级,因为它完全向后兼容,具有许多新功能和修复。
您只需要了解新的 ActiveQuery api 而不是 ol' good Criteria。这样您就可以使用“虚拟列”解决您的问题,只需看这里: http://www.propelorm.org/reference/model-criteria.html#adding_columns
如果您在 symfony 中使用 Propel,只需从 https://github.com/propelorm/sfPropelORMPlugin 安装 sfPropelORMPlugin 并按照 README 文件进行操作。
祝你好运!
【讨论】: