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

[BUG]: afterFetch method in the model does not work when using model paginator #15074

Closed
maulanasatyaadi opened this issue Jun 5, 2020 · 4 comments · Fixed by #15455
Closed
Assignees
Labels
5.0 The issues we want to solve in the 5.0 release breaks bc Functionality that breaks Backwards Compatibility enhancement Enhancement to the framework

Comments

@maulanasatyaadi
Copy link

maulanasatyaadi commented Jun 5, 2020

I made a model that using the afterFetch method inside, it's work when directly fetch data from the model. Here is my code

<?php
declare(strict_types=1);

use Phalcon\Mvc\Model;

class Artist extends Model
{
    public $id;
    public $slug;
    public $title;
    public $content;
    public $artwork;
    public $wallpaper;
    public $tags;
    public $website;
    public $genre;
    public $hits;
    public $verified;
    public $view;

    public function initialize()
    {
        $this->setSource('artist');
        $this->belongsTo('tags', Tags::class, 'id');
        $this->hasMany('id', Chord::class, 'artist');
        $this->hasMany('id', News::class, 'artist');
        $this->hasMany('id', UserRequest::class, 'artist');
        $this->hasMany('id', Vote::class, 'content', [
            'params' => [
                'conditions' => 'division = "artists"'
            ]
        ]);
    }

    public function beforeUpdate()
    {
        $this->genre = implode('|', $this->genre);
    }

    public function afterFetch()
    {
        $this->genre = explode('|', $this->genre);
    }
}

And this my pagination script

public function genreAction()
{
    $genre = $this->dispatcher->getParam('genre');
    $this->tag->prependTitle('Genre ' . $genre);

    $paginator = new ModelPaginator([
        'model' => Artist::class,
        'parameters' => [
            'genre LIKE :genre:',
            'bind' => [
                'genre' => '%' . $genre . '%'
            ]
        ],
        'limit' => 29,
        'page' => $this->request->get('page') ?? 1
    ]);

    // Need to be cached
    $paginated = $paginator->paginate();

    $this->view->genre = $genre;
    $this->view->artists = $paginated;
}

The result should be like

object(Artist)[3]
    public 'id' => string '4761' (length=4)
    public 'title' => string 'Via Vallen' (length=10)
    public 'genre' =>
        array (size=2)
            0 => string 'Pop' (length=3)
            1 => string 'Rock' (length=4)
    ...
    // next object

But the result is

array (size=3)
    'id' => string '4761' (length=4)
    'title' => string 'Via Vallen' (length=10)
    'genre' => string 'Pop|Rock' (length=8)
    ...
    // next array items

I can use another method like an array_map to generate a result items, but I think it will better if the result in pagination is a result object.

@dwinmj
Copy link

dwinmj commented Jun 19, 2020

Hi Maulana, I am having the same issue. I was wondering how did you fix this issue? what did you end up doing?
Thank you so much for your help

@ruudboon ruudboon added discussion Request for comments and discussion enhancement Enhancement to the framework and removed bug A bug report status: unverified Unverified labels Jul 10, 2020
@ruudboon
Copy link
Member

Don't think this is a bug but could be an improvement. We need to think how not to break BC

@zsilbi zsilbi added 6.0 The issues we want to solve in the 6.0 release breaks bc Functionality that breaks Backwards Compatibility transfer and removed discussion Request for comments and discussion labels Oct 4, 2020
@zsilbi
Copy link
Member

zsilbi commented Oct 4, 2020

We will revisit this later.

@zsilbi zsilbi closed this as completed Oct 4, 2020
@zsilbi zsilbi reopened this Oct 6, 2020
@Jeckerson Jeckerson added 5.0 The issues we want to solve in the 5.0 release and removed 6.0 The issues we want to solve in the 6.0 release transfer labels Mar 28, 2021
@Jeckerson Jeckerson assigned Jeckerson and unassigned niden May 3, 2021
Jeckerson added a commit that referenced this issue May 3, 2021
niden added a commit that referenced this issue May 3, 2021
#15074 - Change return from `array` to `ResultsetInterface` in Model Paginator
@niden
Copy link
Member

niden commented May 3, 2021

Resolved in #15455

@niden niden closed this as completed May 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
5.0 The issues we want to solve in the 5.0 release breaks bc Functionality that breaks Backwards Compatibility enhancement Enhancement to the framework
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants