Models in a block

I'm developing a block that will pull a lot of information from a custom table, and since c5 and codeigniter have finally convinced me to stay with the MVC approach

i'm wondering what the best practices for models would be. Do we include them in the models folder and load them into the block controller like so?
function somefunction(){
$sgArray = $this->seasonglance->getPerformances();

Is there a specific filename to use so that it autoloads, much like the view?


View Replies: View Best Answer
Remo replied on at Permalink Best Answer Reply
I recommend you use a package and not only a block.

- Create a new package
- Put your block in it
- Add an installer to the package (lots of examples in the marketplace)
- Put your models in the package as well
- Load them using Loader::model but add the second (optional) parameter to specify the package where c5 should look for the model

Names don't really matter, c5 also doens't use autoload like Zend does. Choose whatever sounds right.
ScottC replied on at Permalink Reply
yeah you can also probably set up autoload if you want in a package controller(registering concrete5's existing autoload for helpers and whatnot first)

but yeah Remo is absolutely right.
nerdess replied on at Permalink Reply
I am just wondering: why should blocks not have their own models?

When I look e.g at the c5 "core" block concrete/blocks/slideshow and open controller.php I find the MVC approach violated.....! There are database calls all over the place in this controller, these db calls should be in a separate model, so this block should have a model.

Currently I am writing a little package that is so far just a wrapper for a block and I am a bit unsure where to stick the models that are only relevant to the block: inside the [packagename]/blocks/[blockname]/models folder? or inside [packagename]/models?

My tummy says the first folder is the right one but you guys say it's the second?!? why?
andrew replied on at Permalink Reply
I can see why you'd think the second – because the models are specific to that block, rather than to a package that uses that block within it. Really, though, there are a lot more helper functions available to load models that are found within the root models directory within a package than there would be to load a model from within a models directory inside the block. So I'd recommend putting the model in that package-level-root directory (so you can load it with Loader::model('model_name', 'package_handle');

Once they broke out of using one database table (which the MVC setup completely uses – to the point where you don't even have to write a single line of database code) it gets a little tricky, which is why you see database code in the controller.php.