【问题标题】:propel pseudo column sorting推进伪列排序
【发布时间】: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


    【解决方案1】:

    您是否使用 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,但这很简单。

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

    (编辑:为正确性添加了返回值。)

    【讨论】:

    • 附录:如果您所做的只是排序,则不需要水合物/吸气剂。如果你也想访问它。
    • 请避免使用 Peer 的东西,Propel 1.6.3 有一个用于查询的智能 API。
    • 呵呵,我试试!我在 1.2.x 版本中开始使用 Propel :)
    【解决方案2】:

    这些要求在 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 文件进行操作。

    祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 2014-08-17
      • 2015-04-28
      • 1970-01-01
      相关资源
      最近更新 更多