first commit
This commit is contained in:
59
application/models/Category_model.php
Executable file
59
application/models/Category_model.php
Executable file
@@ -0,0 +1,59 @@
|
||||
<?php if (!defined('BASEPATH')) {
|
||||
exit('No direct script access allowed');
|
||||
}
|
||||
|
||||
|
||||
class Category_model extends MY_Model
|
||||
{
|
||||
private $table = 'cm_category';
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function getOne($categoryId)
|
||||
{
|
||||
return get_instance()->db->where_in('categoryID', $categoryId)->get('cm_category')->row();
|
||||
}
|
||||
|
||||
public function getAll($categoryId)
|
||||
{
|
||||
$categoryId = is_array($categoryId) ? $categoryId : [$categoryId];
|
||||
return get_instance()->db->where_in('categoryID', $categoryId)->get('cm_category')->result();
|
||||
}
|
||||
|
||||
public function getSubcategoriesByCategoryId(int $categoryId) {
|
||||
return $this->db->where('parentID', $categoryId)->get('cm_category')->result();
|
||||
}
|
||||
|
||||
public function getTableCategoryTitle(&$rows)
|
||||
{
|
||||
$categories = get_instance()->db->get('cm_category')->result();
|
||||
array_map(function ($r) use (&$rows, $categories) {
|
||||
foreach ($rows as $k => $row) {
|
||||
if ($row['postID'] == $r->postID) {
|
||||
$rows[$k]['categoryID'] = $r->categoryID;
|
||||
$rows[$k]['categories'][] = $r->categoryID;
|
||||
foreach ($categories as $c) {
|
||||
if ($c->categoryID == $r->categoryID) {
|
||||
$rows[$k]['categoryTitle'] = (empty($rows[$k]['categoryTitle']) ? '' : $rows[$k]['categoryTitle'] . ', ') . $c->categoryTitle;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, $this->getPostCategoryByPostIdIn(array_column($rows, 'postID')));
|
||||
}
|
||||
|
||||
private function getPostCategoryByPostIdIn($postIds)
|
||||
{
|
||||
if (count($postIds) == 0) return [];
|
||||
return $this->db->from('cm_post_category pc')->where_in('pc.postID', $postIds)->get()->result();
|
||||
}
|
||||
|
||||
public function getCategoriesBySlug($slug) {
|
||||
$slug = is_array($slug) ? $slug : [$slug];
|
||||
return $this->db->where_in('categorySlug', $slug)->from($this->table);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user