One model that loads different tables

The situation is as follows. I have multiple pages with the same layout (5 blocks orso). Each block can be changed (inline editing) seperatly from the other blocks at the other pages. I offcourse can make different models for each individual block on every page. However I would like to have one model that dynamically loads an table dependent on which page the block is on.
Quite a difficult explanation but I dont know how to say it differently.
So lets I have a layout that calls a block: columnright.php:

$CI->load->model('banner_rechts_model'); $kolom = $CI->banner_rechts_model->find_all();

Just a part of the code but I do a for each and then a fuel_edit. Pretty straight forward.

Now the problem is that the model 'banner_rechts_model' should load a table based on an if statement. All the table in the database have the same name structure, they begin with the name of the view + _banner_rechts. However I do not know how I can get the name of the from withing the model:

class Banner_rechts_model extends Base_module_model { function __construct() { $CI =& get_instance(); if ((' '.$CI->uri->segment(1)) == ' ') { $table_to_load = 'mainpage_banner_rechts'; } else if ($CI->uri->segment(1) == 'fuel' && $CI->uri->segment(3) == 'inline_edit') { ????? How to get the name of the view file that the block is in? $table_to_load = view . '_banner_rechts'; } else { $table_to_load = trim($CI->uri->segment(1)) . '_banner_rechts'; } parent::__construct($table_to_load);

As you can see I tried to do it with the URI. But that doesn't work because the URI only contains the name of the model.

I realize my explanation is quite vaque but I hope someone can point me in the right direction.

Comments

  • edited 2:58PM
    I understand what you're wanting to do, personally I'd split the models. Models map to tables..

    You could use the initialize() method on the model like:

    # Load $CI->load->model('banner_rechts_model'); # $table = Figure out what table you need. # Set it $CI->banner_rechts_model->initialize($table); # Get all from set table $kolom = $CI->banner_rechts_model->find_all();

    I don't think you'll have access the the models (MY_model) $table_name var to set it directly.
  • edited 2:58PM
    Okay thanks. I am a little confused though. The __construct function is always executed first.
    To say it as a amateur: how do I tell the __constuct() function to use the $table variable from the initialize function?
  • edited May 2012
    You don't.

    You're right that __construct() is a 'magic' method that runs on load (first/instantiation).
    When you then run the initialize() method you'll reset it's $table value.

    Since your doing this in five individual blocks, you don't need to do any of this in the model because you can do it in the block which know's what table to use.

    So in block one:

    # Load with whatever the default table is (you'll overwrite it) $CI->load->model('banner_rechts_model'); # Set it to use block one table $CI->banner_rechts_model->initialize('block_one'); # Get all from table = block_one $kolom = $CI->banner_rechts_model->find_all();

    Please do reply if I've misunderstood, I'm not a big user of inline editing.
  • edited 2:58PM
    Does this mean that a connection is made twice to the database?
Sign In or Register to comment.