3

In my CMS I want to add modules by creating a table in the database. But Codeigniter with datamappers has one mandatory rule: give the tablename in your code. I have create a Datamapper model as following:

class Module_universeel extends DataMapper {

    public $table = 'module_universeel';
    public $has_one = array('page');

    public function __construct($module_table=null) {

        parent::__construct();
        if(isset($module_table)) $this->table = $module_table;

    }
}

This works when I create a table with the name: 'module_universeel'. When I change the tablename into 'module_news' or something, it works. But it doesn't change the structure ($this->fields) of my datamapper.

How can I do this? Does anybody have experience with this?

Geert
  • 1,217
  • 8
  • 20

3 Answers3

1

Datamapper caches table definitions, controlled by the configuation property "production_cache".

If cache is enabled, the table will not be queried on every pageload, but the cached information is used.

WanWizard
  • 2,574
  • 15
  • 13
0

Because the this->fields array gets filled in the parent::__construct(); And at that point the table name would still be module_universeel

Try this:

if(isset($module_table)) $this->table = $module_table;
parent::__construct();
Geert
  • 1,217
  • 8
  • 20
0

I have the solution. I add this to my datamapper model:



    public function updateTableFields(){
            $this->fields = array();
            $data_fields = $this->db->field_data($this->table);
            foreach ($data_fields as $data_field){
                $this->fields[] = $data_field->name;
                $this->stored->{$data_field->name} = "";
            }

        }

And I added this to my constructor:

$this->updateTableFields();

And now I changed the structure succesfully. Thanks for helping me!