Can CakePHP model modify its table without re-instantiating it?

I am working with an immutable database history where each object instance has its own table in the database with corresponding records. I need to change the model useTable

every time the model is created, but keep Cake with good caching and what not.

Let's say I have many pad objects, each with multiple note objects (note belongs to To Pad, Pad hasMany Note). Each pad has an entry in the pad table, however each note has its own table in the database (for example, "pad_ {id}"). This circuit has been fixed and I have to use it.

Right now I don't need to do any savings, so I do this in the model before I find for read support:

function beforeFind($query_data) {
    if(empty($query_data['pad_id'])) {
        return false;
    } else {
        $this->useTable = $query_data['pad_id'];
        parent::__construct();
        return $query_data;
    }

}

      

This changes the model table used in the database and works great with Core::debug > 0

. However, when it is zero, I think CakePHP is caching the model code and modifying the table incorrectly. Anyway, I get 404 error when I visit / pads / view / {pad_id} or any action dynamically changes this table. I can't figure out what the exact error is because it works great when I turn on debugging. So any pointers to debug this issue will help as well.

Thank!

+2


source to share


2 answers


You can use setSource()

to change the table used by the model. $this->setSource('pad_x')

will set the table to 'pad_x' and reset the model schema. API reference



+9


source


Try it var $persistModel = false;

in your controller or AppController.



See: http://www.pseudocoder.com/archives/2009/03/17/8-ways-to-speed-up-cakephp-apps/

+1


source







All Articles