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);