Welcome Guest, Not a member yet? Register   Sign In
How to paginate model using join?
#1

I have declared this model:

PHP Code:
<?php

namespace App\Models\Blog;

use 
CodeIgniter\Model;

/**
 * Class PostModel.
 */
class PostModel extends Model
{
    protected $table 'blog_posts';
    protected $primaryKey 'id';

    protected $allowedFields = ['author_id''title''slug''content''status''type''published_date'];

    protected $useTimestamps true;
    protected $createdField 'created_at';
    protected $updatedField 'updated_at';

    // trigger
    protected $beforeInsert = ['addSlug'];

    protected $validationRules = [
        'title'        => 'required|min_length[1]|max_length[60]',
        'author_id'    => 'required|integer',
        'status'       => 'required|in_list[draft,published]'
    ];

    protected $validationMessages = [];

    const ORDERABLE = [
        1 => 'title',
        2 => 'author',
        5 => 'created_at',
    ]; 

I usually apply the pagination in the following way:

PHP Code:
$post = new PostModel();
$posts $this->post->paginate(6);
$pager $this->post->pager

and then I send `$posts` and `$pager` to the view, this mechanism works just fine, but how can I paginate the post using a join?

Essentially I need to return all the blog_posts records which contains meta in the `blog_post_meta` table, this is the structure of the two tables:

blog_posts

id | title | content

blog_post_meta

id | post_id | meta_key  | meta_value

how can I use the paginate replicating this query?

PHP Code:
return $this->builder()
            ->select('blog_posts.*')
            ->join('blog_post_meta''blog_post_meta.post_id = blog_posts.id')
            ->where('blog_post_meta.meta_key'$key)
            ->where('blog_post_meta.meta_value'$value); 
Reply
#2

PHP Code:
public function myPaginateMethod()
{
    
$this->builder() .....
    return 
$this->paginate();
}
// $this->post->myPaginateMethod();

public function myMethod()
{
    
$this->builder() .....
    return 
$this;
}
// $this->post->myMethod()->paginate(); 
Reply
#3

(This post was last modified: 04-19-2021, 10:41 AM by sfarzoso.)

(04-19-2021, 09:53 AM)iRedds Wrote:
PHP Code:
public function myPaginateMethod()
{
    $this->builder() .....
    return $this->paginate();
}
// $this->post->myPaginateMethod();

public function myMethod()
{
    $this->builder() .....
    return $this;
}
// $this->post->myMethod()->paginate(); 


Thanks for the answer, but I don't get it, I return this from the model:

PHP Code:
    public function getPostByMeta(string $keystring $value)
    {
        return $this->builder()
            ->select('blog_posts.*')
            ->join('blog_post_meta''blog_post_meta.post_id = blog_posts.id')
            ->where('blog_post_meta.meta_key'$key)
            ->where('blog_post_meta.meta_value'$value);
    


then in the controller:


PHP Code:
$posts $this->post->getPostByMeta('tag'$tag->id)->paginate(10); 


I get: undefined method paginate

also, how can I access to pager?
Reply
#4

paginate is a model method.
Therefore, your method must return an instance of the model.
You are returning a Builder instance.

See the examples I wrote more carefully.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB