Ar Query

I am doing a relatively simple ActiveRecord query




        $sql = Suppliers::find()

             ->with('address')

             ->orderBy(['shortname' => 'ASC']);



But it will not order the results, what am i doing wrong? I have also tried:




        $sql = Suppliers::find()

             ->with('address')

             ->orderBy('shortname ASC');



Any ideas?

‘ASC’ is not a valid argument, you have to use SORT_ASC constant.


 $sql = Suppliers::find()

             ->with('address')

             ->orderBy(['shortname' => SORT_ASC]);

Thanks just tried that and still not sorting, this is the query in yii debug:




SELECT "suppliers".* FROM "suppliers" LEFT JOIN "address" ON "suppliers"."addressid" = "address"."id" LIMIT 20



How can there be a left join and a limit whit that code? You need to show more about what you are doing.

Not sure what you need to see, this is the index action in my controller




        $sql = Suppliers::find()

             ->with('address')

             ->orderBy(['shortname' => SORT_ASC]);


        $dataProvider = new ActiveDataProvider([

            'query' => $sql,

            'pagination' => [

                'pageSize' => 15,

            ],

        ]);

        


        $searchModel = new SuppliersSearch();

        $dataProvider = $searchModel->search($_GET);


        return $this->render('index', [

                'dataProvider' => $dataProvider,

                'searchModel' => $searchModel,

            ]);



This is in my Model




    /**

     * @return \yii\db\ActiveQuery

     */

    public function getAddress()

    {

        return $this->hasOne(ARAddress::className(), ['id' => 'addressid']);

    }



This is the view




    echo GridView::widget([

    	'options' => ['class' => 'table-responsive'],

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

        'summary' => '',

        'columns' => [

            ['class' => 'yii\grid\DataColumn',

               'header' => 'Shortname',

               'value' => function ($data) {

                return Html::a($data->shortname, '/yii/basic/web/supplier/view?id='.$data->shortname, [

                    'class' => 'view-link',

                    ]);

              },

               'format' => 'html',

               'filter' => '<input type="text" class="form-control" name="SuppliersSearch[shortname]">',

            ],        

            'suppliername',

            'address.address1',

            'address.address2',

            'address.postcode',

            ['class' => 'yii\grid\DataColumn',

               'header' => 'Min Order',

               'value' => function ($data) {

                return '£'.number_format($data->minordervalue,2);

              },

               'format' => 'raw',

            ],

            'contact',

        ],

    ]);



I cannot work out why it says limit 20, but my dataProvider I am telling it to paginate, and to only show 15 items!

You’re overwriting the $dataProvider variable with $searchModel->search()!

Hmm, what have I done wrong? Do I need to change it to this:




       $searchModel = new ARSuppliersSearch();

       $searchModel = $searchModel->search($_GET);



After changing that, I cannot search.

Fixed it, realised i needed to add my ActiveDataProvider pagination and query to the SearchModel and remove the existing one from the Controller.

Case Closed.