YII2 SqlDataProvider不起作用关系表值

时间:2015-10-20 15:23:50

标签: php gridview yii2 csqldataprovider

这是我的模特车手:

<?php

namespace backend\models;

use Yii;

class Riders extends \yii\db\ActiveRecord
{
public static function tableName()
{
    return 'riders';
}

public function rules()
{
    return [
        [['cagories_category_id', 'rider_firstname', 'rider_no_tlpn', 'rider_ucinumber', 'countries_id', 'rider_province', 'rider_city', 'rider_dateofbirth', 'rider_gender'], 'required'],
        [['user_id', 'countries_id'], 'integer'],
        [['rider_dateofbirth', 'cagories_category_id'], 'safe'],
        [['rider_gender', 'rider_status'], 'string'],
        [['rider_firstname', 'rider_lastname', 'rider_nickname', 'rider_province', 'rider_city'], 'string', 'max' => 45],
        [['rider_email', 'rider_sponsor', 'rider_birthcertificate_url', 'rider_parental_consent_url'], 'string', 'max' => 100],
        [['rider_no_tlpn'], 'string', 'max' => 15],
        [['rider_ucinumber'], 'string', 'max' => 11]
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'rider_id' => 'rider_id',
        'cagories_category_id' => 'Category Name',
        'user_id' => 'User Team',
        'rider_firstname' => 'Rider Firstname',
        'rider_lastname' => 'Rider Lastname',
        'rider_nickname' => 'Rider Nickname',
        'rider_email' => 'Rider Email',
        'rider_no_tlpn' => 'Rider No Tlpn',
        'rider_ucinumber' => 'Rider Ucinumber',
        'countries_id' => 'Country Name',
        'rider_province' => 'Rider Province',
        'rider_city' => 'Rider City',
        'rider_sponsor' => 'Rider Sponsor',
        'rider_dateofbirth' => 'Rider Dateofbirth',
        'rider_gender' => 'Rider Gender',
        'rider_birthcertificate_url' => 'Rider Birthcertificate Url',
        'rider_parental_consent_url' => 'Rider Parental Consent Url',
        'rider_status' => 'Rider Status',
    ];
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getRegistrations()
{
    return $this->hasMany(Registrations::className(), ['riders_rider_id' => 'rider_id']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getCagoriesCategory()
{
    return $this->hasOne(Categories::className(), ['category_id' => 'cagories_category_id']);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getUser()
{
    return $this->hasOne(User::className(), ['id' => 'user_id']) -> from(user::tableName() . 'ud');
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getUserDesc()
{
    return $this->hasOne(UserDesc::className(), ['desc_id' => 'user_id']) -> from(['ud' => userDesc::tableName()]);
}

/**
 * @return \yii\db\ActiveQuery
 */
public function getCountries()
{
    return $this->hasOne(Countries::className(), ['id' => 'countries_id']);
}

}

这是我的Controller actionIndex:

$searchModel = new RidersSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);

$totalCount = Yii::$app->db->createCommand('SELECT COUNT(*) FROM riders WHERE user_id = :user_id',
    [':user_id' => Yii::$app->user->identity->id])->queryScalar();

$dataProvider = new SqlDataProvider([
    'sql' => 'SELECT * FROM riders WHERE user_id = :user_id',
    'params' => [':user_id' => Yii::$app->user->identity->id],
    'totalCount' => $totalCount,
    'key' => 'rider_id',
    'pagination' => [
        'pageSize' => 10,
    ],
    'sort' => [
        'attributes' => [
            'cagories_category_id',
            'rider_id',
            'rider_firstname',
            'rider_email:email',
            'rider_no_tlpn',
        ]
    ]
]);

$models = $dataProvider->getModels();

return $this->render('index', [
    'searchModel' => $searchModel,
    'dataProvider' => $dataProvider,
]);

这是我的观点索引:

<?= GridView::widget([
    'dataProvider' => $dataProvider,
    // 'filterModel' => $searchModel,
    'columns' => [
        ['class' => 'yii\grid\SerialColumn'],
        [
            'label' => 'Category Name',
            'attribute'=>'cagories_category_id',
            'value' => 'cagoriesCategory.category_name', <---Can't work again
        ],
        [
            'label' => 'BIB',
            'attribute'=>'rider_id',
        ],
        'rider_firstname',
        'rider_email:email',
        'rider_no_tlpn',

        ['class' => 'yii\grid\ActionColumn'],
    ],
]); ?>

在我使用sqldataprovider之前,它可以从模型函数调用有关系,使用后sqldataprovider无法正常工作。如何获得关系表值? 然后在使用它之前,我可以在使用sqldataprovider之后将rider_firstnamerider_lastnamereturn $model->rider_firstname . " " . rider_lastname;合并吗?

1 个答案:

答案 0 :(得分:0)

SqlDataProvider将数据作为数组返回,因此您无法使用$dataProvider->models()

访问相关对象

您必须使用ActiveDataProvider或使用连接条件更改SqlDataProvider的Sql

sql='sql' => 'SELECT * FROM riders inner join '. Categories::tableName() .' as c on c.category_id=riders.cagories_category_id WHERE user_id = :user_id'