Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

when use EMongoSort with EMongoDataProvider, not act as like CSort with CActiveDataProvider #254

Closed
Icedroid opened this issue Aug 23, 2015 · 11 comments

Comments

@Icedroid
Copy link

public function search()
 {
        $criteria = new EMongoCriteria;
        $criteria->compare('name', $this->name);

        $sort = new EMongoSort();
        $sort->attributes = array(
            'startTime',
            'endTime',
            //'*', // this adds all of the other columns as sortable
        );
        $sort->defaultOrder = 't.startTime DESC';

        return new EMongoDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'sort' => $sort,
            'pagination' => array('pageSize' => 15),
        ));
}

I want only startTime and endTime attributes be sortable in CGridView. But It doesn't done as the CSort do.

@Sammaye
Copy link
Owner

Sammaye commented Aug 23, 2015

So you're saying that * is supposed to? Can you link documentation for that?

@Icedroid
Copy link
Author

$sort->attributes = array(
            'startTime',
            'endTime',
        );

These code take no effect.

Or I want to know how to custom some attributes be sortable in CGridView.
http://www.yiiframework.com/wiki/281/searching-and-sorting-by-related-model-in-cgridview/

@Sammaye
Copy link
Owner

Sammaye commented Aug 28, 2015

Oops, I forgot all about this. It will take me sometime to test this since I have not checked out my MongoYii test repo in a long time (haven't used Yii1 in like a year so far) so I will need more time to find some spare time to work on this.

@Icedroid
Copy link
Author

@Sammaye Thanks for your reply. Does you have moved to yii2?

@Sammaye
Copy link
Owner

Sammaye commented Aug 29, 2015

Indeed I am totally on Yii2 these days

@Sammaye
Copy link
Owner

Sammaye commented Sep 23, 2015

Ok, I finally got round to this and I notice that the code to sort by * does exist. Can you clarify what you mean by "But It doesn't done as the CSort do." can you tell me what the response is? What attributes are sortable?

Sammaye pushed a commit to Sammaye/MongoYii-test that referenced this issue Sep 23, 2015
@Sammaye
Copy link
Owner

Sammaye commented Sep 23, 2015

Seems to work for me in my test repo: https://github.com/Sammaye/MongoYii-test/blob/master/protected/views/user/index.php#L30 despite the group field being sortable without defining any attributes in the data provider once I did it became unsortable: https://github.com/Sammaye/MongoYii-test/blob/master/protected/models/User.php#L86 and a default order was applied

@Icedroid
Copy link
Author

$sort = new EMongoSort();
        $sort->attributes = array(
            'startTime',
            'endTime',
            //'*', // this adds all of the other columns as sortable
        );
        $sort->defaultOrder = array('endTime' => -1);

        return new EMongoDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'sort' => $sort,
            'pagination' => array('pageSize' => 15),
        ));

It does not take effected.

@Sammaye
Copy link
Owner

Sammaye commented Oct 3, 2015

OK that's done now, tested.

@Sammaye Sammaye closed this as completed Oct 3, 2015
Sammaye pushed a commit that referenced this issue Oct 3, 2015
@Icedroid
Copy link
Author

I update to this fix version, the bug is fixed. But I found a new bug.
after use these code:

$sort = new EMongoSort();
        $sort->attributes = array(
            'startTime',
            'endTime',
            //'*', // this adds all of the other columns as sortable
        );
        $sort->defaultOrder = array('endTime' => -1);

        return new EMongoDataProvider(get_class($this), array(
            'criteria' => $criteria,
            'sort' => $sort,
            'pagination' => array('pageSize' => 15),
        ));

the following code not run like as before:

 public function attributeLabels()
   {
        return array(
            '_id' => 'ID', 
            'start'=>'开始时间',
        );
}

@Sammaye Sammaye reopened this Oct 17, 2015
@Sammaye
Copy link
Owner

Sammaye commented Oct 26, 2015

Are you defining the columns in your widget like so:

<?php $this->widget('zii.widgets.grid.CGridView', array(
    'id'=>'user-grid',
    'dataProvider'=>$model->search(),
    'filter'=>$model,
    'columns'=>array(
        '_id',
        'username',
        'email',
        'group',
        array(
            'class'=>'CButtonColumn',
            'template'=>'{update}{delete}',
        ),
    ),
)); ?>

?

I have found out that if you define the columns then you must define the names for some odd reason. It comes down to the contents of CGridView:

/**
 * Creates column objects and initializes them.
 */
protected function initColumns()
{
    if($this->columns===array())
    {
        if($this->dataProvider instanceof CActiveDataProvider)
            $this->columns=$this->dataProvider->model->attributeNames();
        elseif($this->dataProvider instanceof IDataProvider)
        {
            // use the keys of the first row of data as the default columns
            $data=$this->dataProvider->getData();
            if(isset($data[0]) && is_array($data[0]))
                $this->columns=array_keys($data[0]);
        }
    }
    $id=$this->getId();
    foreach($this->columns as $i=>$column)
    {
        if(is_string($column))
            $column=$this->createDataColumn($column);
        else
        {
            if(!isset($column['class']))
                $column['class']='CDataColumn';
            $column=Yii::createComponent($column, $this);
        }
        if(!$column->visible)
        {
            unset($this->columns[$i]);
            continue;
        }
        if($column->id===null)
            $column->id=$id.'_c'.$i;
        $this->columns[$i]=$column;
    }

    foreach($this->columns as $column)
        $column->init();
}

/**
 * Creates a {@link CDataColumn} based on a shortcut column specification string.
 * @param string $text the column specification string
 * @return CDataColumn the column instance
 */
protected function createDataColumn($text)
{
    if(!preg_match('/^([\w\.]+)(:(\w*))?(:(.*))?$/',$text,$matches))
        throw new CException(Yii::t('zii','The column must be specified in the format of "Name:Type:Label", where "Type" and "Label" are optional.'));
    $column=new CDataColumn($this);
    $column->name=$matches[1];
    if(isset($matches[3]) && $matches[3]!=='')
        $column->type=$matches[3];
    if(isset($matches[5]))
        $column->header=$matches[5];
    return $column;
}

So it seems this is a framework problem not an extension problem.

@Sammaye Sammaye closed this as completed Oct 26, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants