I want to dynamically change the underlying source table used for a model. Can't seem to get this to work.
As an example, I've two tables in my database:
- 'source1' has 1 record
- 'source2' has 2 records
This following code does report a changed source, but the count of records doesn't change:
$test = new TestModel(); echo 'Source: '.$test->getSource().'<br/>'; echo 'Count: '.count($test->find()).'<br/>'; echo '<br/>'; $mgr = $test->getModelsManager(); $mgr->setModelSource($test, 'source2'); echo 'Source: '.$test->getSource().'<br/>'; echo 'Count: '.count($test->find()).'<br/>'; echo '<br/>';
It outputs this:
Source: source1 Count: 1 Source: source2 Count: 1
Checking through the cphalcon source code, I can see that the Models Manager is called to initialize the Model. It also keeps a track of the initialized modules. The Model constructor is declared as final meaning I can't overload it. I've run out of ideas, short of dynamically having my code modify it's own files, which would be really bad practice.
Have I missed something? Any help much appreciated.