Just what is a "Model" supposed to be in CI?

To the top, you are correct that ActiveRecord is just a database abstraction layer with helper functions. You seem to have wanted ORM - it might be a moot point, but I think DataMapper Overzealous Extension (DMZ)does exactly what you want and it is pretty much just a drop-in ORM solution.

So for instance:

$u = new User();
$u->username = 'Billy';
$u->password = 'cleartext';

$g = new Usergroup('admin');

if ($u->save($g))
// account created and saved correctly)

That bit of code creates an entry in the database, gets the usergroup desired for the user, saves the account and relates the user to the usergroup (stored in a separate table).

In DMZ you can set validation rules like CI's form validation class, so that password is automatically hashed using whatever method you want (a simple sha1 hash or a custom salted solution, etc).

Your perceptions from the tutorials are correct, they are not much observing strong OOP principles and models are used like flimsy CRUD handlers.

This is not a weakness of CI, it's just proof of the simplicity of the examples. Real-world business apps built in CI (every one I've ever seen) uses models much more as they should be, to fully encapsulate data AND to control its manipulation, handling, and ensure its safe and efficient functioning.

ORM, such as DMZ, just handles a large portion of a model. It is an easy mistake to make, under-estimating the power of models, but DMZ showed this to me as it automated so much that I found my models totally empty and it seemed like such a waste. That is when it became clear to me I'm missing something, which is when I began to appreciate the deeper power of models.

If you really think its easier and faster to build an MVC/ORM framework from scratch when you need it...well, wow, you've really missed the point and power of CI entirely. Perhaps it really isn't for you, as it isn't for everyone (Ramus Lerdorf states CI is the best framework he has seen...but he goes on to say he doesn't like frameworks, so the less there is the better the framework is in his opinion) - but I think you are completely going about CI the wrong way.

If you want MVC, CI has everything you need - and if you want ORM functionality, it's as simple as dropping an extension in and going for it. But its power, and some thing its weakness, is CI won't complain if you do something entirely ignorant or inadvisable. Unlike Cake, it will happily let you ignore the unique roles of Views and Models and let you treat your controllers like little more than big functional libraries with only the tiniest connection to object oriented principles.

The fact is, ORM is something else that would need to be learned to get started. If you had to learn OOP, MVC, and ORM all just to get started with CI...hell, I wouldn't use it at all, I don't have time for months of 'experimenting' or 'learning curve' just to start producing something.

CI lets you start within minutes and you can use all your previous PHP experience. It won't complain if you do things 'wrong', it just helps you get done faster - time you can easily then use to go back and make things better with what you have learned in the mean time.

CI is about productivity, not about forcing its opinions and principles on you. If you want to be forced to use all the tools available - whether you want to or not - CI really isn't the right framework for you whatsoever.

[quote author="BrianDHall" date="1254255178"]CI is about productivity, not about forcing its opinions and principles on you. If you want to be forced to use all the tools available - whether you want to or not - CI really isn't the right framework for you whatsoever.[/quote]

This is why I came to CI after trying it first in Rails. The Configuration over Convention in Rails can be limiting when it comes to complex database schemas and unconventional data objects. (Side note: read 'Enterprise Rails' if you want info on how to effectively work around these conventions.)

For whatever reason, I never came across DMZ in my search for an ORM solution for CI. It does sound like what I was after at the time, so I will have to give it another go.

Just to clarify, I didn't mean I would rather build a new framework from scratch every time I need to build an application. Clearly the benefit of a framework is that it's already built and ready to go. My point was that at the time I was evaluating CI, I could find nothing in it that didn't do what I could do with my own ever-growing library of helper functions and classes, existing tools (like Smarty), and/or a few lines of code to route friendly URLs to physical controller classes. CI simply didn't give me any upper hand, specifically when what I was looking for was robust ORM capabilities. If DMZ does all that, then CI deserves another round of experimentation.

FWIW, the CI documentation may well be the culprit here. The examples were extremely basic, and suited more to building a small blogging utility then a real-world enterprise application. If there was a site for CI enterprise recipes, then I never found it, and the existing documentation definitely didn't give any clues for where to go for advanced material.

In any case, thank you for the very comprehensive answer.

Oooo! Well I'm glad I could be of help - and yes, if you come to CI hoping for help dealing with the database and application data you will be rather disappointed with ActiveRecord. I still much prefer it to writing SQL myself, but still it doesn't help much with more complicated things like joins so its of minor use. DMZ, on the other hand, can handle inner and outer joins behind the scenes that I don't even understand, so I'm quite the fan.

As to your search for real-world enterprise applications, no - I too have found that one thing that is missing in the world of CI, which is a solid guide to 'the next step' beyond the simplicity of getting started. I have just pieced together what I learn from the forums here and from reading about the extensions (over in the Ignited Code forum).

DMZ link to it's guide: http://www.overzealous.com/dmz/

From the guide:

Quote:General Features

Everything is an object!
Easy to setup, easy to use.
Custom Validation on object properties.
Lazy Loading (related objects are only loaded upon access).
Relations and their integrity are automatically managed for you.
One to One, One to Many, and Many to Many relations fully supported.
Select data in the style of Active Record (with or without Method Chaining).
If you are new to DMZ, please start here to get DMZ running on your server. Then follow along with the Next Topic links at the bottom of each page.

If you are an existing DMZ user, you will find changes in new releases and upgrade instructions.

DataMapper OverZealous Edition

DataMapper was originally developed by Simon Stenhouse. The basic design and original development was by him. The OverZealous Edition of DataMapper came from a desire to continue to enhance and develop DataMapper, and has been developed and maintained by Phil DeJarnett. DMZ is a near drop-in replacement if you are already using DataMapper 1.6.0 (and possibly older versions).

Some of the enhancements to the original DataMapper include:

In-table foreign-keys for singular relationships.
The ability to view, edit, and query using extra columns on relationship join tables.
The ability to include data from singularly related objects.
The ability to query and include data from deep relationships.
Significantly enhanced relationships, including multiple relationships between the same objects.
The ability to extend DMZ through simple classes, and share those extensions.
Lots of little improvements, such as default ordering, saving objects with existing ids, query grouping, performance improvements, and much more.
If you would like to upgrade to DMZ from DataMapper, please see the upgrade guide.

It was everything I was getting really jelous of having heard about in Kohana and Rails - I was convinced that ORM MUST be important, and it seemed so silly to have to write my own functions for CRUD. Tops to the people that have written other libraries, but I wanted a revolution, not an automatic tool or evolution.

You might also look at the extensions which come with DMZ, specifically the html form and array extension. I found form validation to be one other of CI's weakpoints - not that it does it badly, but it just seems that it greatly improves the annoying process of handling forms but doesn't eliminate ever-so-much code replication (set rule over here, set value of there, name field over there, associate posted field with something else in another place...ick).

I'm only getting started with those particular tools, but I think they may tickle your fancy as well. If I hadn't found them I really wanted to try to make them myself Smile

Thanks for all of the additional information. It sounds like DMZ was what really sold you on CI. I will take a look at it.

Did you experiment with Kohana at all? I see it's based off of CI, but it doesn't seem to have improved upon the model implementation, at least not by the looks of the documentation.

[quote author="Chrisbloom7" date="1254258475"]Thanks for all of the additional information. It sounds like DMZ was what really sold you on CI. I will take a look at it.

Did you experiment with Kohana at all? I see it's based off of CI, but it doesn't seem to have improved upon the model implementation, at least not by the looks of the documentation.[/quote]

I was going to try Kohana as I heard a lot of comments here that they found ORM was just such a huge improvement to their coding, and everyone said CI didn't have it.

I was very happy with CI, but after my first project I was running into all its limits and wondered if I shouldn't have chosen something else. It just didn't seem to go far enough to the next level - it sold me on the framework concept, but something was missing.

I was going to then try out Kohana, but the documentation and forum I found less useful and I found multiple complaints of stability between versions (they seem to mind less if old scripts break, for good or for ill) and I was paranoid in using a 'system' to begin with - I create enough headaches on my own, thanks!

So then I found DMZ and it filled the hole of ORM and I did some more learning of CI and found it to work just fine. I still really hated forms and it seemed like there must be a better way to do it, it seems like I spend most of my time just dealing with forms! I was going to make my own, then found Overzealous' idea to be just what I was after.

Now whenever I find an annoyance I find it easy enough to get around, like enabling query strings if I have to have them or running CI from the command line, auto-configuring CI for localhost or live use, extending a class if I must (which I've yet to do, I've found the constructor of my controller just fine for what I need), etc.

Now I just don't feel like I'm missing anything, so why look anywhere else? I have debugging working great in multiple different IDEs (PHPed, Netbeans, Eclipse, and more - take your pick), I have yet to see the need for unit testing (and there are solutions people have come up with to bring it to CI), I regularly bend URLs to spur of the moment wills.

To this day I've yet to find a single instance where CI was the problem - its always been my own code that had the bugs, and with a debugger I haven't yet had one that was really a super pain in the ass to find. Ok, 1 - my localhost sessions on my first project work weird, but are fine on live. Go figure - they use to work, not sure what I did to break them and don't really care enough to find out.

Also, CI easily supports drop-in libraries from other frameworks like Zend, so you get the advantage of their bloated libraries (if you really insist) without the...well, bloat.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.