Problem with linking models containing multiple words in CakePHP

I had two models, “Example” and “ExampleModel”, linked with belongsTo – hasMany relationship.

The problem was, after baking, the dropdown selection for ExampleModel in Example’s add and view pages were totally blank. Yet, in the index page, the data is shown (which means that relationship is established correctly).

So I checked the controller.php files to make sure that:

$exampleModels = $this->Model->ExampleModel->find('list');
$this->set(compact('exampleModels'));

was in the add() and edit().

Sure enough they are there. I verified that $exampleModels weren’t empty either.

Then found the cause of the problem.

compact(string) doesn’t like uppercase letters in the argument at all. The solution was to change $exampleModels to $examplemodels so that it looks like this:

$examplemodels = $this->Model->ExampleModel->find('list');
$this->set(compact('examplemodels'));

With that, the dropdown selection showed up.

So my lesson is that avoid multiple-worded model names as much as possible…because the baking will generate the variable name with uppercase letter mixed in (because that’s the standard naming convention for the multi-worded model names in CakePHP). So every time you bake it, you need to manually adjust that variable names.

The end.

Leave a Reply