在我的下面列表中,我显示了所有类别,但子类别不显示名称。子类别名称应显示为
Example
Example > Some Example 1
Example > Some Example 2
PHP
PHP > Functions
问题:在我的模型上,我应该能够检索子类别名称和父类别名称。但不行。只能显示父图像,如图像所示,我使用组连接,但它不显示在列表组中,concat应显示为>当子猫名称在那里时。
为什么它不起作用,什么是最佳解决方案?
如图所示,不显示> Some Example 1
。
在我的模型上,我使用多个连接表,例如.c1和.cd1获取父类别,.cd2 .c2获取子类别。
但是,当我使用我的db->选择Group Concat时,它不起作用,只获取父类别名称。
public function get_categories() {
$language_id = '1';
$this->db->select('cp.category_id AS category_id, GROUP_CONCAT(cd1.name SEPARATOR " > ") as name');
$this->db->from('category_path cp', 'LEFT');
// Parent Cat
$this->db->join('category c1', 'c1.category_id = cp.category_id', 'LEFT');
// Sub Cat
$this->db->join('category c2', 'c2.category_id = cp.path_id', 'LEFT');
// Parent Cat
$this->db->join('category_description cd1', 'cd1.category_id = cp.path_id', 'LEFT');
// Sub Cat
$this->db->join('category_description cd2', 'cd2.category_id = cp.category_id', 'LEFT');
$this->db->group_by('cp.category_id');
$this->db->order_by('name', 'ASC');
$this->db->where('cd1.language_id', (int)$language_id);
$this->db->where('cd2.language_id', (int)$language_id);
$query = $this->db->get();
return $query->result_array();
}
在下面尝试使用false而不是同样的结果
$this->db->select('cp.category_id AS category_id, GROUP_CONCAT(cd1.name SEPARATOR " > ") as name', false);
Var结果转储
array(5) { [0]=> array(2) { ["category_id"]=> string(2) "19" ["name"]=> string(7) "Example" } [1]=> array(2) { ["category_id"]=> string(2) "22" ["name"]=> string(7) "Example" } [2]=> array(2) { ["category_id"]=> string(2) "25" ["name"]=> string(7) "Example" } [3]=> array(2) { ["category_id"]=> string(2) "23" ["name"]=> string(3) "PHP" } [4]=> array(2) { ["category_id"]=> string(2) "24" ["name"]=> string(3) "PHP" } }
最后查询
SELECT `cp`.`category_id` AS `category_id`, GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR " > ") as name FROM `category_path` `cp` LEFT JOIN `category` `c1` ON `c1`.`category_id` = `cp`.`category_id` LEFT JOIN `category` `c2` ON `c2`.`category_id` = `cp`.`path_id` LEFT JOIN `category_description` `cd1` ON `cd1`.`category_id` = `cp`.`path_id` LEFT JOIN `category_description` `cd2` ON `cd2`.`category_id` = `cp`.`category_id` WHERE `cd1`.`language_id` = 1 AND `cd2`.`language_id` = 1 GROUP BY `cp`.`category_id` ORDER BY `name` ASC
类别表
类别说明表
类别路径表
答案 0 :(得分:2)
试试这个:
$this->db->select("cp.category_id AS category_id, GROUP_CONCAT(cd1.name ORDER BY cp.level SEPARATOR ' > ') AS name, c1.parent_id, c1.sort_order", FALSE);
$this->db->select()
接受可选的第二个参数。如果将其设置为FALSE,CodeIgniter将不会尝试使用反引号来保护您的字段或表名称。如果您需要复合选择语句,这非常有用。
答案 1 :(得分:0)
感谢所有愿意提供帮助的人。
我发现问题的原因在于我的添加类别功能。
如果我是category_path表格中的子类别,我什么时候创建一个类别
如果子类别ID是22和父ID 19
然后在category_path表中,行应该有两行插入两行,如下所示。
category_id path_id level
22 19 0
22 22 1
由于某些原因,我的模型添加类别功能,它没有插入路径ID正确
现在我知道需要修复模型功能。
完整工作模式
<?php
class Model_category extends CI_Model {
public function add() {
$data = array(
'parent_id' => (int)$this->input->post('parent_id'),
'status' => $this->input->post('status'),
'date_edited' => mdate('%Y-%m-%d %H:%i:%s', now()),
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->insert($this->db->dbprefix . 'category', $data);
$category_id = $this->db->insert_id();
$data = array(
'category_id' => (int)$category_id,
'language_id' => (int)'1',
'name' => $this->input->post('name'),
'meta_title' => $this->input->post('meta_title'),
'meta_description' => $this->input->post('meta_description'),
'meta_keyword' => $this->input->post('meta_keyword'),
'date_edited' => mdate('%Y-%m-%d %H:%i:%s', now()),
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->insert($this->db->dbprefix . 'category_description', $data);
// MySQL Hierarchical Data Closure Table Pattern
$level = 0;
$this->db->select('*');
$this->db->from($this->db->dbprefix . 'category_path');
$this->db->where('category_id', (int)$this->input->post('parent_id'));
$this->db->order_by('level', 'ASC');
$query = $this->db->get();
foreach ($query->result_array() as $result) {
$data = array(
'category_id' => (int)$category_id,
'path_id' => (int)$result['path_id'],
'level' => (int)$level,
);
$this->db->insert($this->db->dbprefix . 'category_path', $data);
$level++;
}
// The problem was here was not adding category into model but now
// All Fixed.
$data = array(
'category_id' => (int)$category_id,
'path_id' => (int)$category_id,
'level' => (int)$level
);
$this->db->insert($this->db->dbprefix . 'category_path', $data);
if (isset($data['keyword'])) {
$data = array(
'category_id' => 'category_id=' . (int)$category_id,
'keyword' => $this->input->post('keyword'),
'date_edited' => mdate('%Y-%m-%d %H:%i:%s', now()),
'date_added' => mdate('%Y-%m-%d %H:%i:%s', now())
);
$this->db->insert($this->db->dbprefix . 'url_alias', $data);
$this->db->query("INSERT INTO " . $this->db->dbprefix . "url_alias SET query = 'category_id=" . (int)$category_id . "', keyword = '" . $this->db->escape($data['keyword']) . "'");
}
}
public function edit() {
}
public function get_categories($data = array()) {
$language_id = '1';
$this->db->select('cp.category_id AS category_id, GROUP_CONCAT(cd1.name ORDER BY level SEPARATOR " > ") as name, c1.parent_id, c1.sort_order');
$this->db->distinct();
$this->db->from($this->db->dbprefix . 'category_path cp', 'LEFT');
$this->db->join($this->db->dbprefix . 'category c1', 'cp.category_id = c1.category_id', 'LEFT');
$this->db->join($this->db->dbprefix . 'category c2', 'cp.path_id = c2.category_id', 'LEFT');
$this->db->join($this->db->dbprefix . 'category_description cd1', 'cp.path_id = cd1.category_id', 'LEFT');
$this->db->join($this->db->dbprefix . 'category_description cd2', 'cp.category_id = cd2.category_id', 'LEFT');
$this->db->where('cd1.language_id', '1');
$this->db->where('cd2.language_id', '1');
$this->db->group_by('cp.category_id');
$this->db->order_by('name', 'ASC');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return FALSE;
}
}
}