我在数据库设计中应用了附加翻译方法。
由于表的结构如此,每个查询的代码变得更加复杂。
我的 PHP 代码在 models 中:
<?php
// SHOW ALL RECORDS
$this->db->select('m.id, m.title, m.content');
$table = 'blog';
if (MULTILINGUAL) {
$this->db->from($table.' AS m');
$this->db->select('t.title, t.content');
$this->db->join($table.'_translation AS t', 'm.id = t.parent_id', 'left');
$this->db->where('t.language_id', LANGUAGE);
$query = $this->db->get();
} else $query = $this->db->get($table.' AS m');
?>
所以我想改变它的代码...
当MULTILINGUAL
为true时,并且每个查询具有列字段title
,content
,...
$table = 'blog';
$this->db->select('id, title, content');
$query = $this->db->get($table);
它将自动使用带有后缀_translation
的表的方法JOIN(如上面的代码)。
否则,查询应该像普通查询一样运行。
我如何修改db
类但不影响Codeigniter的核心系统?
PHP代码(使用Codeigniter):
// Query 1:
$this->db->select('id, title, content');
$query = $this->db->get('blog');
// Query 2:
$this->db->select('id, title, content');
$this->db->where('id', 1);
$query = $this->db->get('blog');
生成
$this->db->last_query()
:if (MULTILINGUAL) {
// Query 1:
// SELECT t.title, t.content FROM blog AS m LEFT JOIN blog_translation AS t ON m.id = t.parent_id WHERE t.language_id = 1
// Query 2:
// SELECT t.title, t.content FROM blog AS m LEFT JOIN blog_translation AS t ON m.id = t.parent_id WHERE t.language_id = 1 WHERE m.id = 1
else {
// Query 1:
// SELECT title, content FROM blog
// Query 2:
// SELECT title, content FROM blog WHERE id = 1
}
我希望它能完全自动化。
我认为可以通过更改db
类来解决这个问题,但直接干预核心系统是不稳定的(在核心更新中)...
非常感谢您帮助我解决我的问题!
MULTILINGUAL
的值的? - Narendrasingh Sisodia