Coder Social home page Coder Social logo

sort by datetime fields about data HOT 8 CLOSED

albertborsos avatar albertborsos commented on June 19, 2024
sort by datetime fields

from data.

Comments (8)

vjik avatar vjik commented on June 19, 2024

Seems, problem here:

$methodName = 'get' . ucfirst($field);

For post_id method name is getPost_id.

May be we can remove magic here?

from data.

samdark avatar samdark commented on June 19, 2024

Removing magic is good. How would you do it?

from data.

albertborsos avatar albertborsos commented on June 19, 2024

Ok, that is an issue too, but after I update it to $methodName = 'get' . (new Inflector())->toPascalCase($field); then I will get this error:

Error: Object of class DateTimeImmutable could not be converted to string in /app/vendor/yiisoft/data/src/Paginator/KeysetPaginator.php:264
Stack trace:
#0 /app/vendor/yiisoft/data/src/Paginator/KeysetPaginator.php(176): Yiisoft\Data\Paginator\KeysetPaginator->readData(Object(Yiisoft\Yii\Cycle\Data\Reader\EntityReader), Object(Yiisoft\Data\Reader\Sort))
#1 /app/views/blog/comments/_comments.php(19): Yiisoft\Data\Paginator\KeysetPaginator->read()
#2 /app/vendor/yiisoft/view/src/PhpTemplateRenderer.php(28): require('/app/views/blog...')
#3 /app/vendor/yiisoft/view/src/PhpTemplateRenderer.php(37): Yiisoft\View\PhpTemplateRenderer->Yiisoft\View\{closure}('/app/views/blog...', Array)
#4 /app/vendor/yiisoft/view/src/ViewTrait.php(449): Yiisoft\View\PhpTemplateRenderer->render(Object(Yiisoft\View\WebView), '/app/views/blog...', Array)
#5 /app/vendor/yiisoft/view/src/ViewTrait.php(402): Yiisoft\View\WebView->renderFile('/app/views/blog...', Array)
#6 /app/views/blog/comments/index.php(25): Yiisoft\View\WebView->render('_comments', Array)
#7 /app/vendor/yiisoft/view/src/PhpTemplateRenderer.php(28): require('/app/views/blog...')
#8 /app/vendor/yiisoft/view/src/PhpTemplateRenderer.php(37): Yiisoft\View\PhpTemplateRenderer->Yiisoft\View\{closure}('/app/views/blog...', Array)
#9 /app/vendor/yiisoft/view/src/ViewTrait.php(449): Yiisoft\View\PhpTemplateRenderer->render(Object(Yiisoft\View\WebView), '/app/views/blog...', Array)
#10 /app/vendor/yiisoft/view/src/ViewTrait.php(402): Yiisoft\View\WebView->renderFile('/app/views/blog...', Array)
#11 /app/vendor/yiisoft/yii-view/src/ViewRenderer.php(322): Yiisoft\View\WebView->render('index', Array)
#12 /app/vendor/yiisoft/yii-view/src/ViewRenderer.php(119): Yiisoft\Yii\View\ViewRenderer->renderProxy('index', Array, Array, Array, Array, Array)
#13 /app/vendor/yiisoft/data-response/src/DataResponse.php(324): Yiisoft\Yii\View\ViewRenderer->Yiisoft\Yii\View\{closure}()
#14 /app/vendor/yiisoft/data-response/src/Formatter/HtmlDataResponseFormatter.php(43): Yiisoft\DataResponse\DataResponse->getData()
#15 /app/vendor/yiisoft/data-response/src/DataResponse.php(353): Yiisoft\DataResponse\Formatter\HtmlDataResponseFormatter->format(Object(Yiisoft\DataResponse\DataResponse))
#16 /app/vendor/yiisoft/data-response/src/DataResponse.php(106): Yiisoft\DataResponse\DataResponse->formatResponse()
#17 /app/vendor/yiisoft/cookies/src/CookieMiddleware.php(148): Yiisoft\DataResponse\DataResponse->getHeader('Set-Cookie')
#18 /app/vendor/yiisoft/cookies/src/CookieMiddleware.php(85): Yiisoft\Cookies\CookieMiddleware->encodeResponseSetCookieHeaders(Object(Yiisoft\DataResponse\DataResponse))
#19 /app/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\Cookies\CookieMiddleware->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#20 /app/vendor/yiisoft/session/src/SessionMiddleware.php(36): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#21 /app/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\Session\SessionMiddleware->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#22 /app/vendor/yiisoft/error-handler/src/Middleware/ErrorCatcher.php(135): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#23 /app/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(104): Yiisoft\ErrorHandler\Middleware\ErrorCatcher->process(Object(HttpSoft\Message\ServerRequest), Object(Psr\Http\Server\RequestHandlerInterface@anonymous))
#24 /app/vendor/yiisoft/middleware-dispatcher/src/MiddlewareStack.php(53): Psr\Http\Server\RequestHandlerInterface@anonymous->handle(Object(HttpSoft\Message\ServerRequest))
#25 /app/vendor/yiisoft/middleware-dispatcher/src/MiddlewareDispatcher.php(50): Yiisoft\Middleware\Dispatcher\MiddlewareStack->handle(Object(HttpSoft\Message\ServerRequest))
#26 /app/vendor/yiisoft/yii-http/src/Application.php(87): Yiisoft\Middleware\Dispatcher\MiddlewareDispatcher->dispatch(Object(HttpSoft\Message\ServerRequest), Object(App\Handler\NotFoundHandler))
#27 /app/vendor/yiisoft/yii-runner-http/src/HttpApplicationRunner.php(103): Yiisoft\Yii\Http\Application->handle(Object(HttpSoft\Message\ServerRequest))
#28 /app/public/index.php(36): Yiisoft\Yii\Runner\Http\HttpApplicationRunner->run()
#29 {main}

from data.

albertborsos avatar albertborsos commented on June 19, 2024

I had some time to dig deeper into it.

As @vjik said it is a problem, because it will try to call getCreated_at method.
If I replace it with

$methodName = 'get' . (new Inflector())->toPascalCase($field);

, then it will be OK, but it is still magic.

The next issue is with the following line:

$this->currentFirstValue = (string) $this->getValueFromItem($item, $field);

If the getValueFromItem returns a \DateTimeInterface then it is not possible to cast it to string.

$methodName = 'get' . ucfirst($field);

I made a new method:

    /**
     * @param mixed $item
     *
     * @return mixed
     */
    private function getStringValueFromItem($item, string $field): string
    {
        $value = $this->getValueFromItem($item, $field);
        if ($value instanceof \DateTimeImmutable) {
            return (string) $value->getTimestamp();
        }

        return (string) $value;
    }

And I replaced these two lines:

$this->currentFirstValue = (string) $this->getValueFromItem($item, $field);

$this->currentLastValue = (string) $this->getValueFromItem($item, $field);

to:

$this->currentFirstValue = $this->getStringValueFromItem($item, $field);

and

$this->currentLastValue = $this->getStringValueFromItem($item, $field);

After these modifications it sorts well for datetime fields too.
Of course there may be more values that cannot be casted to string.

from data.

albertborsos avatar albertborsos commented on June 19, 2024

Is there any reason the KeysetPaginator class is final?
I like in Yii 2.0 that I can easily extend the classes of the framework and I can use it if I configure it in the config files.

Now I would like to extend the KeysetPaginator class and fix it with my own methods while it is not fixed in this repository.
But it is not possible since this class is final.

from data.

albertborsos avatar albertborsos commented on June 19, 2024

I realised that KeysetPaginator is not good for my scenario (I just picked it from the demo app).

Because if I want to order by date, there could be multiple records with the same date, and KeysetPaginator will not know by the token which was the last record with the same date.

So I found OffsetPaginator which works great for my scenario.

from data.

samdark avatar samdark commented on June 19, 2024

@albertborsos would you please submit pull requests with your changes? Preferably separate ones but one PR would do as well.

Classes are final by default in Yii3. There are many advantages and disadvantages in this decision but that's how it is.

from data.

albertborsos avatar albertborsos commented on June 19, 2024

I close this issue, since I made a PR to access the field with the getter method.
The other issue I think is not a bug, I just used the wrong pagiantor.

from data.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.