在 addAscendingOrderByColumn 的 propel 中将 Varchar 转换为 Integer

发布于 2024-12-13 23:27:15 字数 679 浏览 3 评论 0原文

我有一种方法可以从基于 propel 的 symfony-1.1 项目的数据库中获取数据。

现在,用例将整数存储到 varchar 字段中,这会导致顺序错误,例如 {1, 17, 5},而不是数字顺序我在期待,即 {1, 5, 17}

我知道一种方法是重新设计我的 schema.yml,但这不是一个选择。我想知道是否有一种方法可以将所述 varchar 字段转换为整数而不损害 propel-approach。

这是排序函数:

public static function getFooData($column = 'FooPeer::ID', $orderBy = 'asc') {
    //FIXME: Sort varchar fields as integer, needed for FooPeer::REQUESTS

    $c = new Criteria();

    if ($orderBy == 'asc') {
        $c->addAscendingOrderByColumn($column);
    } else {
        $c->addDescendingOrderByColumn($column);
    }

    return FooPeer::doSelect($c);
}

I have a method in order to get data out of a database for a propel-based symfony-1.1 project.

Now the use-case arrived to store an integer into a varchar field, which results in a wrong order, e.g. {1, 17, 5}, and not the numeric one I was expecting, i.e. {1, 5, 17}.

I know that one way would be to redesign my schema.yml, but this is not an option. I was wondering if there is a way to cast said varchar field as an integer without harming the propel-approach.

This is the sorting function:

public static function getFooData($column = 'FooPeer::ID', $orderBy = 'asc') {
    //FIXME: Sort varchar fields as integer, needed for FooPeer::REQUESTS

    $c = new Criteria();

    if ($orderBy == 'asc') {
        $c->addAscendingOrderByColumn($column);
    } else {
        $c->addDescendingOrderByColumn($column);
    }

    return FooPeer::doSelect($c);
}

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

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

发布评论

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

评论(2

请爱~陌生人 2024-12-20 23:27:15

怎么样:

$c->addAscendingOrderByColumn('CAST('.$column.' AS UNSIGNED)');

What about:

$c->addAscendingOrderByColumn('CAST('.$column.' AS UNSIGNED)');
予囚 2024-12-20 23:27:15

出于兴趣,您还可以为此编写一个视图,并在视图而不是表的顶部构建模型。假设您使用 Propel 写入表,该解决方案要求平台支持可写视图(我不确定它们都支持,但也许这个假设已经过时了)。

当你不确定如何在 Propel 中做某事,或者真的很尴尬时,这通常是一种很好/快速的技术。它救了我好几次,尽管它不是每个纯粹主义者都喜欢的。

Just for interest, you could also have written a view for this, and build your model on top of the view rather than the table. Assuming you're writing to the table with Propel, this solution requires the platform to support writable views (I'm not sure they all do, but perhaps that assumption is out of date).

This is often a good/quick technique where you're not sure how to do something in Propel, or where it is really awkward. It's saved me a few times, even though it's not every purist's cup of tea.

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