We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Order in find function

Hello all,

I have a little question, i want to use the find function of the model, but i have one little problem. When i try to order by a related model then i become this error here

    Unknown model or alias 'Students\\Model\\Commentcurrent' (1), when preparing: SELECT                [Students\\Model\\Blogpost].* FROM [Students\\Model\\Blogpost]

The code that i use looks like that at the moment

    //Blogpost.php
    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource('mBlogpost');
        $this->hasOne('id', 'Students\Model\Commentcurrent','tableid', array('alias' => 'Commentcurrent'));
    }
    public function getArticles($limit) {
        $artciles = self::find(array('title LIKE :lang: AND site = :site: AND visible = :visible:',
            'bind' => array(
            'lang'      => '%{'.$lang.'}%',
            'visible'   => 1,
            'site'      => 'local.dev',
            ),
            'limit' => $limit,
            'order' => 'Students\Model\Commentcurrent.numcomments desc'

        ));
    }

    //Commentcurrent.php
    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSource('mCommentcurrent');
        $this->hasOne('tableid', 'Students\Model\Blogpost', 'id', array('alias' => 'Blogpost'));
    }

Thank you for your help :)

Your query is incorrect!!

Your query is on mBlogpost table but your order condition is on Commentcurrent table.

You should use join query with modelManager and PHQL