Yii2 ActiveDataProvider使用关系中的count进行排序

时间:2016-01-28 07:10:43

标签: php activerecord yii2

Yii2 ActiveDataProvider使用关系中的count进行排序?

我有3张桌子,下面是: Table Schema

我在表inventory_device_type中显示数据,这里是代码:

控制器:

<?php
namespace app\controllers\inventory;

use Yii;
use yii\base\Action;

use app\models\tables\InventoryDeviceType;

class DeviceInventory extends Action
{
    public function run()
    {
        $model = new InventoryDeviceType();
        $dataProvider = $model->search(Yii::$app->request->post());

        return $this->controller->render('device-inventory',[
                'dataProvider' => $dataProvider,
            ]);
    }
}

型号:

<?php
namespace app\models\tables;

use yii\db\ActiveRecord;
use yii\data\ActiveDataProvider;
use yii\web\NotFoundHttpException;

class InventoryDeviceType extends ActiveRecord
{
    public static function tableName()
    {
        return 'inventory_device_type';
    }

    public function rules()
    {
        return [
            [['name','id_device_vendor'],'required'],
            ['name','unique']
        ];
    }

    public function attributeLabels()
    {
        return [
            'id_device_vendor' => 'Device Vendor',
        ];
    }

    /** queries **/

    public function findData($id)
    {
        if(($data = self::findOne($id)) == null){
            throw new NotFoundHttpException;
        }else{
            return $data;
        }
    }

    public function getNormal()
    {
        return $this->getList()->where(['inventory_device_list.device_condition' => 'normal'])->count();
    }

    public function getBroken()
    {
        return $this->getList()->where(['inventory_device_list.device_condition' => 'broken'])->count();
    }

    public function getSold()
    {
        return $this->getList()->where(['inventory_device_list.device_condition' => 'sold'])->count();
    }

    public function getTotal()
    {
        return $this->getList()->where(['!=','inventory_device_list.device_condition',''])->count();
    }

    public static function getVendorList($sort=[])
    {
        return InventoryDeviceVendor::find()->orderBy($sort)->all();
    }

    public function search($params, $spesific=[],$sort=[])
    {
        $query = self::find();
        $query->where($spesific);

        $query->joinWith(['list']);

        $dataProvider = new ActiveDataProvider([
                'query' => $query,
                'sort' => ['defaultOrder' => $sort]
            ]);

        $dataProvider->sort->attributes['vendor_name'] = [
            'asc' => ['inventory_device_vendor.name' => SORT_ASC],
            'desc' => ['inventory_device_vendor.name' => SORT_DESC],
        ];

        /** sort on page device-inventory **/

        $dataProvider->sort->attributes['device_vendor'] = [
            'asc' => ['inventory_device_vendor.name' => SORT_ASC],
            'desc' => ['inventory_device_vendor.name' => SORT_DESC],
        ];

        /** sort on page device-inventory end **/

        return $dataProvider;
    }

    /** queries end **/

    /** relation **/

    public function getListLeft()
    {
        return InventoryDeviceList::find();
    }

    public function getList()
    {
        return $this->hasMany(InventoryDeviceList::className(),['id_device_type' => 'id']);
    }

    public function getVendorLeft()
    {
        return InventoryDeviceVendor::find();
    }

    public function getVendor()
    {
        return $this->hasOne(InventoryDeviceVendor::className(),['id' => 'id_device_vendor']);
    }

    /** relation end **/



}

以下是观点:

<?= 

            GridView::widget([
                'dataProvider' => $dataProvider,
                'layout' => '<div class="table-responsive">{items}</div> {summary} {pager}',
                'columns' => [
                    [
                        'class' => 'yii\grid\SerialColumn',
                        'headerOptions' => ['width' => 50]
                    ],
                    'name',
                    [
                        'attribute' => 'device_vendor',
                        'class' => 'yii\grid\DataColumn',
                        'value' => function($data){
                            return $data->vendor->name;
                        }
                    ],
                    [
                        'attribute' => 'normal',
                        'class' => 'yii\grid\DataColumn',
                        'format' => 'html',
                        'value' => function($data){
                            return Html::a($data->getNormal(),'#',['class' => 'label label-success']);
                        },
                    ],
                    [
                        'attribute' => 'sold',
                        'class' => 'yii\grid\DataColumn',
                        'format' => 'html',
                        'value' => function($data){
                            return Html::a($data->getSold(),'#',['class' => 'label label-warning']);
                        },
                    ],
                    [
                        'attribute' => 'broken',
                        'class' => 'yii\grid\DataColumn',
                        'format' => 'html',
                        'value' => function($data){
                            return Html::a($data->getBroken(),'#',['class' => 'label label-danger']);
                        },
                    ],
                    [
                        'attribute' => 'Total',
                        'class' => 'yii\grid\DataColumn',
                        'format' => 'html',
                        'value' => function($data){
                            return Html::a($data->getTotal(),'#',['class' => 'label label-primary']);
                        },
                    ],

                ],
                ]);

                ?>

以及如何对它进行排序,如正常,破碎和销售? 这是:结果视图: enter image description here

标题正常,已售出,已损坏且无法排序,如果不使用此代码(如设备供应商),我可按关系排序:

$dataProvider->sort->attributes['device_vendor'] = [
        'asc' => ['inventory_device_vendor.name' => SORT_ASC],
        'desc' => ['inventory_device_vendor.name' => SORT_DESC],
    ];

0 个答案:

没有答案