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

default sort error on mongodb dataProvider #19019

Open
naroogheh opened this issue Nov 14, 2021 · 10 comments
Open

default sort error on mongodb dataProvider #19019

naroogheh opened this issue Nov 14, 2021 · 10 comments

Comments

@naroogheh
Copy link

naroogheh commented Nov 14, 2021

What steps will reproduce the problem?

when use mongodb dataProvider , beacause of mongodb have not "id" colums as default , so we get error
Undefined index: id

What is the expected result?

What do you get instead?

replace these 2 line on
vendor/yiisoft/yii2/data/Sort.php Line 232 , Line 233
$definition = $this->attributes[$attribute]??'_id ';
$columns = $definition[$direction === SORT_ASC ? 'asc' : 'desc'] ?? 'asc';

Additional info

Q A
Yii version ~2.0.38
PHP version 7.4
Operating system linux (Docker Desktop on windows but linux containers )
naroogheh added a commit to naroogheh/yii2 that referenced this issue Nov 14, 2021
@naroogheh
Copy link
Author

#19020

I fixed it and put a pull request

@WinterSilence
Copy link
Contributor

yii2 not require mongodb, fix it in https://github.com/yiisoft/yii2-mongodb

@bizley
Copy link
Member

bizley commented Nov 15, 2021

Yes, @jarooghe where is this id set in your case? Sort is either provided directly with attributes or is taking the list of model's attributes.

@naroogheh
Copy link
Author

@bizley id marked as default sort colum , when not sort option exist on url params ,
beacause in mongodb there is not id column (_id instead) , so code says undefined id

@naroogheh
Copy link
Author

yii2 not require mongodb, fix it in https://github.com/yiisoft/yii2-mongodb

but i my suggestion it is not related to yii2-monodb
code has error when model has not id attribute and any sort has passed .
so its better to use ?? operator to handle the code .

@bizley
Copy link
Member

bizley commented Nov 16, 2021

Hmm, ok, could you provide your configuration of DataProvider that causes that problem?

@naroogheh
Copy link
Author

naroogheh commented Nov 16, 2021

@bizley , @WinterSilence
i used gii generator , as you know ia have A controller that have this code

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

and have search Model called HotelsSearch that have search() method
in this method have this code

$query = Hotels::find();
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);
 ////finally 
return $dataProvider; 

in addition ,
class Hotels extends \yii\mongodb\ActiveRecord
because dataProvider dosent have default sort , as default yii core use id column that not exist in my Hotels (MongoDb Model) and return error.
(vendor/yiisoft/yii2/data/Sort.php Line 232 , Line 233)
i fixed it by adding default sort

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

instead we can fix it in the framework core ( error is on farmework core ).

@bizley
Copy link
Member

bizley commented Nov 16, 2021

i used gii generator

Ok, this is the piece of information that was missing and the cause of that.

This looks directly connected to #19018

@naroogheh
Copy link
Author

@bizley
the problem is :
yii sort as default consider id column to sort data , so it not ok when the model dosent have id column (its clear , read it again ) .

i tried to clear the bug , but you treid hard to link that for sun,moon,math,laravel ,...
i understood the bug and solved for myself.
never mind
yii2 dosent need any updates .

@WinterSilence
Copy link
Contributor

@jarooghe you try change it not only for mongodb, it's not right way

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

3 participants