Files
cm-web/application/models/Category_model.php
2020-06-10 06:21:34 +09:00

60 lines
2.1 KiB
PHP
Executable File

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