推进伪列排序 [英] propel pseudo column sorting
问题描述
基本上,我想创建一个伪列,我将排序。这是我的SQL查询
Basically I want to make a pseudo column by which I'll sort. This is my SQL Query
SELECT I.*, ((I.width*175)/I.height) as relativeWidth
FROM Image I
order by relativeWidth asc
如何在propel没有直接写SQL?
How can I do it in propel without writing direct SQL ? and I don't want to make a view and query it.
推荐答案
您使用Criteria(旧的创建方式where clause)?如果是这样,您可以直接执行:
Are you using Criteria (the old way of creating a where clause)? If so you can simply do:
$c = new Criteria();
$c->addSelectColumn(
'((' . IPeer::WIDTH . '*175)/' . IPeer::HEIGHT . ') AS relativeWidth'
);
$c->addAscendingOrderByColumn('relativeWidth');
$rows = IPeer::doSelect($c);
您还需要覆盖行类(I)中的hydrate捕获额外的列(未测试):
You will also need to override the hydrate() method in your row class (I) in order to capture the extra column (untested):
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来获取新值, 。
Lastly of course you will need a getter for the new value, but that's easy.
如果你使用Query系统,可能有一个类似的方法,虽然我不太熟悉它。
If you are using the Query system there is probably a similar way to do it with that, though I am less familiar with it.
(编辑:添加正确性的返回值。)
( added return value for correctness.)
这篇关于推进伪列排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!