custom class/model advice - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: custom class/model advice (/showthread.php?tid=5380) |
custom class/model advice - El Forum - 01-20-2008 [eluser]Unknown[/eluser] Hello, I'm working on building classes and using CI to access it - basically I just want to use the CI database driver - and then I'm going to put my control logic in the controllers. But I'm not sure where to put my "business" logic. example - I'm working on a shipping notification interface. I have orders that have items and I would like them to be nice objects (eg, $order->isComplete(), $order->hasShipping(), etc.) for the time being I've created a model and put my logic in there: Code: $this->load->model('order'); Code: $all_orders = $order->getAllOrders(); I've run into a big of a snag in that inside my order model I haven't figured out how to return objects - and I'm starting to feel if I'm pounding a round peg in a square hole. Can anyone give advice on how to do this - should I be building "libraries" instead of models? Thanks, yehosef custom class/model advice - El Forum - 01-20-2008 [eluser]tonanbarbarian[/eluser] I am not sure why you have to return an array of objects I prefer to return my data from models as arrays rather than objects If you want to return the action model class, I do not do things that way. I fell that the model should only return data and nothing else. So I use models like this... In the controller I load the model as normal (slightly differently to you) Code: $this->load->model('order'); Code: $orderRow = $this->order->load($id); Code: $allOrders = $this->order->getAll(); Code: class Order extends Model { Models are the best way to implement this sort of Business Logic. I tend to use Libraries to do thing that are needed in multiple controllers. My Libraries do sometimes load models to get the data they need Oh and not sure if this is a typo or a bug, but the following line you have in your example code will not work Code: $order = $this->Order; Code: $order = $this->order; however the safest way to do it would be Code: $order =& $this->Order; personally I just stick with Code: $this->order... |