SELECT ORDER BY

Hi!

I need a SELECT ... IN ... ORDER BY-query. In MySQL I can do this: SELECT * FROM mytable WHERE id IN (1,2,3,4) ORDER BY FIELD(id,3,2,1,4); But how can I achieve this with Phalcon? (a short code-example would be very helpful) The order is a result from a query to an external indexing engine. Thanks a lot Aljoscha



7.8k
edited May '16

Hi AljoschaPeters if you 've done register model manager in app/config/service.php in di and create model table in database example

app/config/service.php

$di->setShared('modelsManager', function () {
     return new Phalcon\Mvc\Model\Manager();
});

app/model/User.php

class User extends \Phalcon\Mvc\Model
{

    /**
     *
     * @var integer
     */
    public $user_id;

    /**
     *
     * @var string
     */
    public $user_name;

    /**
     *
     * @var string
     */
    public $user_pass;

in your controller you can use

class TestController extends ControllerBase
{
    public funtion getAction()
    {
        $phql = "SELECT * FROM User WHERE user_id (1,2,3) ORDER BY user_id(3,2,1)";
        $rs      = $this->modelsManager->executeQuery($phql);
        print_r($rs);
    }

for more information https://docs.phalconphp.com/en/latest/reference/models.html

I prefer to use query builder though.