Update: please see my EDIT section below where I explain what I have discovered after testing
I'm working on a website with CakePHP 2.5.1, and I'm experiencing a very strange behavior: one of the 2 models I have seems to refuse to use a different database configuration (other than the default one) if I don't place it in Cake's default Model folder (if I place it there everything works great).
Both models are located in the same folder, which happens to be not the default Model folder (this is because those are shared with other websites and I have a central place where I put all the shared ones).
To make Cake find those models I use the following line of code in the bootstrap.php file: App::build(array('Model' => array(dirname(ROOT) . DS . '_shared/Cake_v2/Model')));
The 2 models I have there are called Campanya and EjercicioMultChoice.
Everything seems to be setup properly:
- If I print
App::objects('Model')
I can see both models listed there, which means Cake is finding them in that external folder - If I print
get_class_vars('DATABASE_CONFIG')
I can see both database configurations: the default one, used by Campanya, and the custom one (called BD_Contenidos), used by EjercicioMultChoice. - Both models are included in the AppController.php with the
$uses
variable array.
I can call the Campanya model in the controller without any problem. However, when call the EjercicioMultChoice model, I get the following error:
MISSING DATABASE TABLE
Error: Table ejercicio_mult_choices for model EjercicioMultChoice was not found in datasource default.
This is how the EjercicioMultChoice model file looks like:
class EjercicioMultChoice extends AppModel {
public $name = 'EjercicioMultChoice';
public $primaryKey = 'id';
public $useTable = 'ejercicios_multiple_choice';
public $useDbConfig = 'BD_Contenidos';
}
As you can see, that model indicates to use a specific database configuration, which Cake is aware of, but in the error shown above it's saying that it can't find the table in datasource default. It can't because it's not there of course, it's in another table in another database (which by the way this other database is in the same server as the default one and I use it without issues on other projects).
As I mention above, if I move that exact same EjercicioMultChoice.php model file to Cake's default app/Model/ folder, then I don't have any issues.
So I'm a bit lost here, I don't know what else I can check to find what the issue is.
EDIT
So I believe I've discovered what the problem is, and a workaround about it, but I still would like to know why this is happening and how can I make it work as it should.
If I add these 2 lines of code (both of them) before calling the model then it all works fine:
$this->EjercicioMultChoice->setDataSource('BD_Contenidos');
$this->EjercicioMultChoice->setSource('ejercicios_multiple_choice');
This clearly means that even though Cake finds the EjercicioMultChoice.php model file, which indicates the database and table names, it ignores those 2 variables ($useTable and $useDbConfig). The only way to make it work is by manually setting those 2 variables with the methods that I wrote above.
Why is this happening? This does not happen if the model file is in the default folder...