推进伪列排序

发布于 2024-12-11 06:22:58 字数 205 浏览 0 评论 0原文

基本上我想创建一个伪列来进行排序。这是我的 SQL 查询,

SELECT I.*, ((I.width*175)/I.height) as relativeWidth
FROM Image I
order by relativeWidth asc

如何在 propel 中执行此操作而不直接编写 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

How can I do it in propel without writing direct SQL ? and I don't want to make a view and query it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

人心善变 2024-12-18 06:22:58

您是否使用 Criteria(创建 where 子句的旧方法)?如果是这样,您可以简单地执行以下操作:

$c = new Criteria();
$c->addSelectColumn(
    '((' . IPeer::WIDTH . '*175)/' . IPeer::HEIGHT . ') AS relativeWidth'
);
$c->addAscendingOrderByColumn('relativeWidth');
$rows = IPeer::doSelect($c);

您还需要重写行类(I)中的 Hydro() 方法,以捕获额外的列(未经测试):

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,但是这很容易。

如果您使用查询系统,可能有类似的方法可以做到这一点,尽管我不太熟悉它。

(编辑:添加返回值以确保正确性。)

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);

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;
}

Lastly of course you will need a getter for the new value, but that's easy.

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.

(Edit: added return value for correctness.)

轻许诺言 2024-12-18 06:22:58

这些要求在 Propel 1.5+ 版本中是可能的。 强烈建议您升级,因为它完全向后兼容,并包含许多新功能和修复。

您只需要了解新的 ActiveQuery api,而不是旧的良好标准。这样你就可以使用“虚拟列”来解决你的问题,只需看这里:
http://www.propelorm.org/reference/model-criteria.html#adding_columns

如果您在 symfony 中使用 Propel,只需从 https://github.com/propelorm/sfPropelORMPlugin 并按照 README 文件使其正常工作。

祝你好运!

Such requirements are possible in version 1.5+ of Propel. You are strongly encouraged to upgrade since it is fully backwards-compatible, with a lot of new features and fixes.

You just need to learn about the new ActiveQuery api instead of the ol' good Criteria. That way you can solve your problem using "virtual columns", just look here:
http://www.propelorm.org/reference/model-criteria.html#adding_columns

If you are using Propel inside symfony, just install the sfPropelORMPlugin from https://github.com/propelorm/sfPropelORMPlugin and follow the README file to get it working.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文