UserNaN
UserNaN

Reputation: 137

Multilingual databases using Codeigniter

I have applied the method Additional Translation Approach in the database design.

relationship

With such structure of the tables, the code becomes more complex for per query.

My PHP code in 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');
?>

So I want to change it's code...


When MULTILINGUAL is true, and with per query has column fields is title, content,...

$table = 'blog';
$this->db->select('id, title, content');
$query = $this->db->get($table);

it will automatically use the method JOIN with a table have suffix _translation (as my code above).

Otherwise, queries should be run as a normal query.

How can I do modified db class but don't affects core system of Codeigniter?


PHP code (using 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');

Produces $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
}

I want it to be completely automatic.

I think that could change the db class to solve this problem, but direct intervention into the core system is unstable (within core update)...

I truly appreciate your help in resolving my problem!

Upvotes: 3

Views: 242

Answers (2)

Flaz
Flaz

Reputation: 135

You could create a view and just call : $this->db->where('t.language_id', LANGUAGE); , but I don't know really if this is a better solution.

Upvotes: 0

Narendrasingh Sisodia
Narendrasingh Sisodia

Reputation: 21437

This might help you to work around I don't know how you were using that config file but can achieve that functionality as

function your_function($multilingual = false) {
    $table = 'blog';
    if ($multilingual === true) {
        $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($table . ' AS m')->result_array();
    } else {
        $this->db->select('m.id, m.title, m.content');
        $query = $this->db->get($table . ' AS m')->result_array();
    }
    return $query;
}

Upvotes: 1

Related Questions