Welcome Guest, Not a member yet? Register   Sign In
CI4 with join and pagination
#3

Thank you InsiteFx. Here is my model


Code:
namespace App\Models;

class PostModel extends \CodeIgniter\Model {
    
    protected $table      = 'post';
    protected $primaryKey = 'post_id';

    //protected $useAutoIncrement = true;

    protected $returnType     = 'App\Entities\PostEntity';
    //protected $useSoftDeletes = true;

    protected $allowedFields = [
                                    'post_id',
                                    'post_category_id',
                                    'post_user_id',
                                    'post_title',
                                    'post_slug',
                                    'post_body',
                                    'post_is_published',
                                    'post_image',
                                ];

    protected $useTimestamps    = true;
    protected $createdField     = 'post_created_at';
    protected $updatedField     = 'post_updated_at';
    //protected $deletedField  = 'deleted_at';

    protected $validationRules    = [
            'post_category_id'  => 'required',
            'post_title'        => 'required|is_unique[post.post_title,post_title,{post_title}]|min_length[5]|max_length[255]',
            'post_body'         => 'required|min_length[5]'
        ];
    
    protected $validationMessages = [
        'post_category_id' => [
            'required' => 'The Category is required',
            ],
        'post_title' => [
            'required'      => 'The title name is required',
            'is_unique'     => 'That title already exists',
            'min_length'    => 'The minimum length of a title is 5 characters',
            'max_length'    => 'The maximum length is 255 characters'
            ],
          'post_body' => [
              'required' => 'The body of a post is required.',
               'min_length' => 'The minimum length of the body is 5 characters',
             ],
        ];
    //protected $skipValidation     = false;


    function getPostsByCategory($post_category_id = null, $category_is_published = 1, $post_is_published = 1, $pagination = 0 ) {
        $builder = $this->builder('post');
        $builder->select(
            '
            post.post_id,
            post.post_category_id,
            post.post_title,
            post.post_slug,
            post.post_body,
            post.post_is_published,
            post.post_updated_at,
            post.post_created_at,
            post.post_user_id,
            post.post_image,
            user.name,
            user.email,
            category.category_id,
            category.category_user_id,
            category.category_name,
            category.category_is_published,
            category.category_created_at,
            category.category_updated_at
            ');
        $builder->join('category', 'category.category_id = post.post_category_id');
        $builder->join('user', 'user.id = post.post_user_id');
        $builder->orderBy('post.post_updated_at', 'DESC');
        if (!empty($post_category_id)) {
            $builder->where('post.post_category_id', $post_category_id);
        }
        $builder->where('post.post_is_published', $post_is_published);
        $builder->where('category.category_is_published', $category_is_published);
        // there will be many so return an array
        $query = $builder->get();
        $result = $query->getResult();
        if ($pagination == 1) {
            // see this https://forum.codeigniter.com/thread-77644.html
            return $this;
        } else {
            return $result;
        }
    }
Reply


Messages In This Thread
CI4 with join and pagination - by spreaderman - 08-20-2021, 06:43 PM
RE: CI4 with join and pagination - by InsiteFX - 08-21-2021, 12:52 AM
RE: CI4 with join and pagination - by spreaderman - 08-21-2021, 01:20 AM
RE: CI4 with join and pagination - by paliz - 08-21-2021, 01:23 PM
RE: CI4 with join and pagination - by spreaderman - 08-21-2021, 06:57 PM



Theme © iAndrew 2016 - Forum software by © MyBB