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

Cake\I18n\Time numeric sorting in collection #7095

Closed
kyleweishaupt opened this issue Jul 22, 2015 · 2 comments
Closed

Cake\I18n\Time numeric sorting in collection #7095

kyleweishaupt opened this issue Jul 22, 2015 · 2 comments
Assignees
Labels
Milestone

Comments

@kyleweishaupt
Copy link

Not sure if this is is a framework issue or I'm doing something wrong with the ORM. I'm querying a couple different models and using a collection to display them sorted by the modified property to make a makeshift news feed. This is on the latest Cake release, 3.0.9.

The problem is in the sortBy method for the Collection. The expected behavior is all the various models to be in the $recent collection and sorted by their modified date descending.

The only sort method that returns as expected is SORT_NUMERIC, but it throws the error: Object of class Cake\I18n\Time could not be converted to double twice. Changing to SORT_NATURAL, SORT_STRING, SORT_LOCALE_STRING or leaving the sort option off will display the wrong entities based on the date.

        $recent_uploads     = $this->Users->Uploads->find('recent')->limit(10)->all();
        $recent_charts      = $this->Users->Clients->Charts->find('recent')->limit(10)->all();        
        $recent_users       = $this->Users->find('recent')->limit(10)->all();

        $recent = (new Collection([]))
            ->append($recent_charts)
            ->append($recent_uploads)
            ->append($recent_users)
            ->sortBy('modified', SORT_DESC, SORT_NUMERIC)
            ->take(10);

        $feed = $recent->toArray();

Suppressing the error will also cause the incorrect sort order. I tried also just passing the result sets into the new collection, but the sort ordering was incorrect as well.

Any help or direction would be greatly appreciated. Thanks!

@lorenzo
Copy link
Member

lorenzo commented Jul 22, 2015

Hmm, this is annoying... I'll take a look at it

@lorenzo lorenzo added this to the 3.0.11 milestone Jul 22, 2015
@lorenzo lorenzo added the defect label Jul 22, 2015
@lorenzo lorenzo self-assigned this Jul 22, 2015
antograssiot added a commit to antograssiot/cakephp that referenced this issue Jul 31, 2015
Solves cakephp#7095 by converting automatically Datetime to seconds when SORT_NUMERIC is used.
antograssiot added a commit to antograssiot/cakephp that referenced this issue Jul 31, 2015
Solves cakephp#7095 by converting automatically Datetime to seconds when SORT_NUMERIC is used.
@antograssiot
Copy link
Contributor

Closing as #7153 is open now

dakota pushed a commit that referenced this issue Nov 1, 2017
Solves #7095 by converting automatically Datetime to seconds when SORT_NUMERIC is used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants