简体   繁体   中英

Cakephp Model ignoring database settings

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...

Well well well, it seems that the problem was that I didn't put the trailing / in the App::build() path. The correct one should be App::build(array('Model' => array(dirname(ROOT) . DS . '_shared/Cake_v2/Model/')));

What made it hard for me to figure this out is that Cake was actually finding the models in that folder, even without the trailing / , but for some reason that prevented it from loading the internal contents of those model files.

Anyway, it works now!

As far as i can tell, cake is NOT loading your provided model file! You can find out by putting an exit(); call in the first line of that (shared) file and see if your app honors that (by showing a white page ,-)).

If not, your model file does not get picked up by cakephp.

Another way to check if your model file gets picked up is to define a custom member variable like public $myModelTestVar = 'sharedModel'; to the file and just check for the variable in the controller that uses the model (just debug($this->EjercicioMultChoice) in an action).

If you can confirm/proove that the file is picked up, I would volunteer to set this scenario up and try myself whats going on there - but am very certain I am right.

debug(dirname(ROOT) . DS . '_shared/Cake_v2/Model')

to proove it's pointing to where you expect.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM