We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

DB model paginate with relationships

Hello i have relationship db model.

use Phalcon\Mvc\Model;

class A extends Model
{
    public function initialize()
    {
        $this->belongsTo(
            'b_id',
            B::class,
            'id',
            [
                'reusable' => true,
                'alias'    => 'B'
            ]
        );

        $this->belongsTo(
            'c_id',
            C::class,
            'id',
            [
                'reusable' => true,
                'alias'    => 'C'
            ]
        );
    }
}
use Phalcon\Mvc\Model;

class B extends Model
{
    public function initialize()
    {

        $this->hasMany(
            'id',
            A::class,
            'b_id',
            [
                'reusable' => true,
                'alias'    => 'A'
            ]
        );
    }
}
use Phalcon\Mvc\Model;

class C extends Model
{
    public function initialize()
    {

        $this->hasMany(
            'id',
            A::class,
            'C_id',
            [
                'reusable' => true,
                'alias'    => 'A'
            ]
        );
    }
}

A model belongs to B and C.

I am trying to paginate A model datas.

use Phalcon\Paginator\Adapter\Model as PaginatorModel;

// Passing a resultset as data
$paginator = new PaginatorModel(
[
'model'  => A::class,
'limit' => $count,
'page'  => $page,
]
);
$paginate = $paginator->paginate();

"paginate" method returns to array (inside model->toArray()), how I can access to B and C model and properties in A with model based. I know, I can use queryBuilder or inside to foreach I can call B and C model with "findFirst"

this code not working because of returning array.

foreach ($paginate->getItems() as $a) {
    $data[] = [
        'a' => $a,
        'b' => $a->b,
        'c' => $a->c
    ];
}

I'm assuming your first code block should defined the A class, not the Tests class.

getItems() should be returning you an array of A objects, so in your last code block, $a should be an instance of the A class. Is this not the case?



285

I editted my code, yes it's A class.

getItems() returns to model->toArray() list, not return class. So i can't use $a like a model class. (https://github.com/phalcon/cphalcon/blob/master/phalcon/Paginator/Adapter/Model.zep#L125)

I'm assuming your first code block should defined the A class, not the Tests class.

getItems() should be returning you an array of A objects, so in your last code block, $a should be an instance of the A class. Is this not the case?

The only thing I can suggest is rather than setting a model property, set a data property:

$paginator = new PaginatorModel(
    [
        'data'  => A::find(),
        'limit' => $count,
        'page'  => $page,
    ]
);

That's how v3.4 did it. I see the documentation for v4 does use model instead though. Are you using Phalcon v3.4 or v4?

https://docs.phalcon.io/3.4/en/db-pagination#examples



86

Is your proyect on git?