Propel ORM: Order By FIELD

只愿长相守 提交于 2019-12-07 07:11:27

问题


I'm trying to do a query with a custom "order by" with Propel 1.6

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

this does not work:

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

how can i do?


回答1:


you can stack order-by's in order that you want:

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

Try that.

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

in your case, I would establish $con, create a query to get your value list, then use a for loop to assign your $stmt->bindValue(#,#) and then execute.




回答2:


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

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

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


来源:https://stackoverflow.com/questions/34036279/propel-orm-order-by-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!