While working on a consulting gig where I’m using CakePHP to rewrite an existing site, I was fooling around with the latest version of Cake from SVN and discovered a nifty little feature. Cake now has dynamic models.
I discovered this because my app was working, pulling data from the database but I hadn’t baked any models at all. After confirming with gwoo via IM that I was not going crazy, he told me what was going on.
By default now, Cake will automatically use the AppModel class and create a unique alias to a database table if it does not find the file. This means that if you have a table that follows the Cake conventions, needs no validation, and has no associations to other tables, you don’t even have to create the model file any more. Woah, talk about a serious time saver in some cases.
However, keep in mind that this feature *also* means that if you misspell the name of your model file, this dynamic model feature will mean that your misspelled model file will NOT get loaded, and you might be scratching your head as to why your associations don’t work and why validation is not going on. So pay close attention to the naming of your files to avoid confrontations with grumpy Canadian programmers on the mailing list.
Tags: CakePHP


This sort of implicit logic is what separates the rubyists from the pythonistas. It’s clear who’s right. Choose the path of light.
Thanks Chris for write this, it’s save my time …
This has existed for some time now… I discovered it accidentally as well, while investigating the error saying table not found rather than model not found!
I spotted this a couple of days ago and assumed it had always worked like that!
That’s surely is a piece of Cake. Infact I’ve come across this before while I was playing with plugins.
I pr()’ed a plugin model, and found its name as ‘AppModel’ – I had no idea at that time
What’s the benefit of this feature? Ok, I don’t have to create a file, and else? On the other hand I no longer have a representation of my design at the model level if I use this feature…
I felt the same when I first discovered this feature. Though at first I thought it’s a bug
I dint write the model class and for some weird reason Cake was not throwing any error on my face! But soon after digging some more into the Cake core, I realized now we can have dynamic models
– models that are not physically there! In some cases it may simply stuffs, but I felt like, it may encourage fat controller – thin model ? People may start writing codes in controllers that are best housed in models. Just my thoughts anyway.
Handy, yes, but performance impact?
Does it do a structure query every time you use that model?
I wonder the same thing about Zend DB.
By contrast, Propel and others keep static “map” filez, outlining the structure.
Perhaps cake dynamically creates & caches map files?
Although that’s a undocumented one, it’s not that new…
But you warned it well, it may be a real headache when you’re mispelling model names.
cheers
I for one welcome our new dynamic model overlords. I think it’s a good move and can save some time early on when your just trying to get some of the initial view work together. Although this strengthens my opinion that validation should be moved to its own class. Then you could validate models by defining a validation subclass and applying it to a dynamic model.
@anonymous
There’s nothing wrong with “implicit logic” as long as you KNOW that it is being used. I’ve used both Rails and Django, and while I prefer Python and Django over Ruby and Rails, that isn’t the point. Both Rails and Django use implicit logic as part of the framework. Otherwise, what would be the point?
Btw: upon further research, CakePHP’s validation is within its own class. I’m still on the fence as to whether applying a validation object to a dynamic model makes sense. If you’re building business logic, it makes sense to define a proper model and contain it there.