79 lines
2.3 KiB
PHP
Executable File
79 lines
2.3 KiB
PHP
Executable File
<?php defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
class category_model extends CI_Model
|
|
{
|
|
|
|
public $table = 'category';
|
|
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->load->database();
|
|
}
|
|
|
|
public function findById($categoryId = null)
|
|
{
|
|
if (!is_null($categoryId)) {
|
|
if(is_array($categoryId)) $categoryId = implode(',', $categoryId);
|
|
$sql = "SELECT *
|
|
FROM `category`
|
|
WHERE `category`.`idx` IN($categoryId)";
|
|
} else {
|
|
$sql = "SELECT *
|
|
FROM `category` order by category_name";
|
|
}
|
|
return $this->db->query($sql);
|
|
}
|
|
|
|
public function getCategories($categoryId)
|
|
{
|
|
if (is_array($categoryId)) {
|
|
$categoryId = implode(",", $categoryId);
|
|
}
|
|
|
|
$query = $this->db->query("SELECT
|
|
*
|
|
FROM
|
|
`category`
|
|
WHERE
|
|
`category`.`idx` IN ($categoryId) order by category_name");
|
|
return $query->result();
|
|
}
|
|
|
|
public function findByName($categoryName)
|
|
{
|
|
return $this->db->where_in('category_name', $categoryName)->get($this->table);
|
|
}
|
|
|
|
public function findByParentId($parentId)
|
|
{
|
|
return $this->db->where_in('parent', $parentId)->get($this->table);
|
|
}
|
|
|
|
public function getSubCategoryId($parentCategoryName = 'grace-category')
|
|
{
|
|
$subCategoryId = array();
|
|
$parentId = 0;
|
|
$categoryQuery = $this->findByName($parentCategoryName);
|
|
if (!$categoryQuery->num_rows()) {
|
|
$this->db->insert($this->table, array(
|
|
'category_name' => $parentCategoryName,
|
|
));
|
|
$parentId = $this->db->insert_id();
|
|
} else {
|
|
$parentId = $categoryQuery->row()->idx;
|
|
$subCategories = $this->findByParentId($parentId)->result();
|
|
foreach ($subCategories as $sub) {
|
|
array_push($subCategoryId, $sub->idx);
|
|
}
|
|
}
|
|
return array('parent' => $parentId, 'subcategories' => $subCategoryId);
|
|
}
|
|
|
|
public function findAll($categoryId = null)
|
|
{
|
|
return $this->db->from($this->table . ' l');
|
|
}
|
|
|
|
}
|