推进ORM:按字段排序

时间:2015-12-02 06:31:02

标签: propel

我尝试使用自定义"命令进行查询"与Propel 1.6

select * from myObject ORDER BY FIELD(id, 3, 11, 7, 1)

这不起作用:

myObjectQuery::create()
    ->orderById($someIds)
    ->find()

我该怎么办?

3 个答案:

答案 0 :(得分:0)

您可以按顺序堆叠order-by:

myObjectQuery::create()
    ->orderByField1
    ->orderbyField3('desc')
    ->orderbyField2
    ->find()

试试。

更新2:

$con = Propel::getConnection();
$query = 'SELECT COUNT(t1.user) AS users, t1.choice AS lft, t2.choice AS rgt
  FROM choice t1 iNNER JOIN choice t2 ON (t1.user = t2.user)
  WHERE t1.choice IN (?, ?) AND t2.choice IN (?, ?)
  GROUP BY t1.choice, t2.choice';
$stmt = $con->prepare($query);
$stmt->bindValue(1, 'foo');
$stmt->bindValue(2, 'bar');
$stmt->bindValue(3, 'baz');
$stmt->bindValue(4, 'foz');
$res = $stmt->execute();

在你的情况下,我会建立$ con,创建一个查询来获取你的值列表,然后使用for循环来分配你的$stmt->bindValue(#,#),然后执行。

答案 1 :(得分:0)

$ids = array(3, 11, 7, 1);

$cids = implode(',', $ids);

myObjectQuery::create()
    ->addAscendingOrderByColumn("FIELD (tableName.id, {$cids})")
    ->find()

答案 2 :(得分:-2)

$ids = array(3, 11, 7, 1);

$cids = implode(',', $ids);

myObjectQuery::create()
    ->orderBy("FIELD(id, {$cids})")
    ->find()