phalcon / cphalcon

High performance, full-stack PHP framework delivered as a C extension.
https://phalcon.io
BSD 3-Clause "New" or "Revised" License
10.79k stars 1.96k forks source link

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

Closed maulanasatyaadi closed 3 years ago

maulanasatyaadi commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

zsilbi commented 4 years ago

We will revisit this later.

niden commented 3 years ago

Resolved in https://github.com/phalcon/cphalcon/pull/15455