【问题标题】:merge two CActiveDataProvider and show result in a CGridview in yii合并两个 CActiveDataProvider 并在 yii 中的 CGridview 中显示结果
【发布时间】:2014-08-21 16:33:06
【问题描述】:

我有 2 个模型:

 $model = new ProfileInformation('internetConection');
 $modeliner = new ProfileInformation('inerConection');

我在 yii 中显示了 2 个 CGridView 中的那些,如何在 CGridView 中显示

型号:

 public function internetConection() {
        // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria = new CDbcriteria();
    $criteria->with = array('user');
    $criteria->condition = 'serviceId=:serviceId';
    $criteria->params = array(':serviceId' => '1');
    $criteria->group = 't.user_Id';
    $criteria->select = array('count(distinct psh_profile_information_services.profileInformationId) AS internetConectionCount');
    $criteria->join = 'left join psh_profile_information_services on t.id=psh_profile_information_services.profileInformationId';
    $criteria->order = 't.id';
    $criteria->compare('user_Id', $this->user_Id);
    $criteria->compare('isService', $this->isService, true);


    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}

public function inerConection() {
    // @todo Please modify the following code to remove attributes that should not be searched.

    $criteria = new CDbcriteria();
    $criteria->with = array('user');
    $criteria->addInCondition('serviceId', array(2, 3, 4, 5));
    $criteria->group = 't.user_Id';
    $criteria->select = array('count(distinct psh_profile_information_services.profileInformationId) AS inerConectionCount');
    $criteria->join = 'left join psh_profile_information_services on t.id=psh_profile_information_services.profileInformationId';
    $criteria->order = 't.id';
    $criteria->compare('user_Id', $this->user_Id);
    $criteria->compare('isService', $this->isService, true);


    return new CActiveDataProvider($this, array(
        'criteria' => $criteria,
    ));
}

我现在正在使用 2 个 CgridView,但如果我可以在表格中显示,那就太好了。 每个结果搜索都有一个新字段:inerConectionCount 和 internetConectionCount。

internetConectionCount 表
inernetConectionCount 表

我想要它:

 $this->widget('zii.widgets.grid.CGridView', array(
        'id' => 'profile-information-grid1',
        'dataProvider' => $dataprovider
            'columns' => array(
            array(
                'header' => '',
                'value' => '$this->grid->dataProvider->pagination->offset + $row+1', //  row is zero based
            ),

             array(
                'name' => 'ProfileInformation.user.organization',
                'value' => 'CHtml::encode($data->user->organization)',

            ),

            array(
                'name' => 'ProfileInformation.user.scope',
                'value' => 'CHtml::encode($data->user->scope->name)',
                'filter' => Scope::model()->options,
            ),
            array(
                'name' => 'id',
                'value' => 'CHtml::encode($data->id)',
            ),

          'inerConectionCount',



        ),
    ));

【问题讨论】:

  • 你找到你一直在寻找的东西了吗?

标签: php search yii criteria cgridview


【解决方案1】:

您可以合并来自两个提供者的数据,但您必须禁用分页,否则每个提供者中将限制为 10 条记录

$model = new ProfileInformation('internetConection');
$modeliner = new ProfileInformation('inerConection');

$data = CMap::mergeArray( // combine two data
    $model->search()->getData(),
    $modeliner ->search()->getData()
);

$provider = new CArrayDataProvider( $data ); // use CArrayDataProvider instead of CActiveDataProvider

【讨论】:

  • 我觉得你的反应不好,如何在新的CGridView中填充dataprovider参数?我用 $model->internetConection() 填充 CGgridView1,用 $model->inerConection() 填充 CGgridView1
  • 如果我使用 MyModel::model->findAll($criteria1) 和 MyModel::model->findAll($criteria2) 。我可以合并 2 个结果吗?
  • 在 CGridView 中,给提供者这个:$provider
  • 我试了一下,但出现错误:CArrayDataProvider 及其行为没有名为“getValidators”的方法或闭包。
  • كد مربوط به گريدتون را هم بگزاريد
猜你喜欢
  • 2014-08-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多