Search code examples
symfony1integersql-order-bypropelvarchar

Cast Varchar as Integer in propel for addAscendingOrderByColumn


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

Solution

  • What about:

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