Multilingual Databases Using Codeigniter

I have applied the Complementary Approach to Translation method in database design.

relationship

With this table structure, the code becomes more complex for each 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 his code ...


If MULTILINGUAL

to true , and for each request are fields column title

, content

...

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

      

it automatically uses the JOIN method and the table is suffixed _translation

(like my code above).

Otherwise, the queries should run like a normal query.

How can I make a modified class db

but does not affect the main Codeigniter system?


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 fully automatic.

I think this might change the class db

to solve this problem, but direct intervention into the main system is unsustainable (as part of a major update) ...

I sincerely appreciate your help in solving my problem!

+3


source to share


2 answers


It can help you work. I don't know how you used this config file, but you can achieve this 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;
}

      

+1


source


You can create a view and just call:, $this->db->where('t.language_id', LANGUAGE);

but I don't know if this is really the best solution.



0


source







All Articles