Welcome Guest, Not a member yet? Register   Sign In
Models, where, when and why?
#18

(11-16-2014, 11:55 AM)alroker Wrote: Thyrosis, think about it this way. We'll start off with the router. So someone arrives at your URL looking for a blog post. The router nudges the blog controller and says, "Oi someone want a blog post. You can find the post ID here." The controller grabs the ID then goes to the blog model, and says, "Hello blog model, can you see if you can find this blog post for me please?". The blog model takes the post ID from the controller, runs it under the tap to clean it off (just in case someone stuck a bogie on it), then it looks for the post in the database. The blog model finds the post data, grabs it, grabs all the info associated with it, then asks the user model to hand over the info for the author of the blog post. Then it sees it needs to get the user's avatar image too, so then the blog model goes to the image library and asks it to check if the user's avatar file exists, and to create a URL to it. The image library is only too happy to oblige. The blog model now has everything the controller expects, and so it packages it up into an array, and says, "Here you go blog controller. This is all the blog info." The blog controller grabs it, says "Thanks for that mate!" and shakes the blog post view and says, "Oi view wake up! You need to show this to a visitor. Take this array and render your stuff!". The view takes the array and renders it's stuff, and they all live happily ever after.

Brilliant explanation there! That's exactly the way I'm doing things now, but then with libraries. It also answered my question how different models interact, so thank you for that Smile

bclinton Wrote:I think what most people would do is use SQL joins to do what you are doing and select that information in one query.

That is one way of doing it too. Joins could get complicated pretty quickly in my experience, so in my personal opinion I find it a lot easier to write simple queries in the form of
PHP Code:
$page $this->db->where('page_id'1)->get('pages')->row();
$page->author $this->db->where('user_id'$page->author)->get('users')->row(); 
And achieve thesame result.
Reply


Messages In This Thread
Models, where, when and why? - by Thyrosis - 10-24-2014, 12:03 AM
RE: Models, where, when and why? - by Rufnex - 10-24-2014, 02:39 AM
RE: Models, where, when and why? - by navotjer - 10-24-2014, 04:10 AM
RE: Models, where, when and why? - by InsiteFX - 10-24-2014, 04:44 AM
RE: Models, where, when and why? - by kilishan - 10-24-2014, 08:17 AM
RE: Models, where, when and why? - by Thyrosis - 10-25-2014, 02:16 AM
RE: Models, where, when and why? - by John_Betong - 10-24-2014, 10:04 AM
RE: Models, where, when and why? - by Hobbes - 10-29-2014, 06:41 AM
RE: Models, where, when and why? - by jlp - 10-29-2014, 08:50 PM
RE: Models, where, when and why? - by albertleao - 10-30-2014, 11:41 AM
RE: Models, where, when and why? - by InsiteFX - 10-30-2014, 04:28 PM
RE: Models, where, when and why? - by Thyrosis - 11-01-2014, 01:27 AM
RE: Models, where, when and why? - by alroker - 11-16-2014, 11:55 AM
RE: Models, where, when and why? - by Thyrosis - 11-18-2014, 05:51 AM
RE: Models, where, when and why? - by RobertSF - 11-20-2014, 08:19 PM
RE: Models, where, when and why? - by veedeoo - 11-17-2014, 09:04 PM
RE: Models, where, when and why? - by bclinton - 11-17-2014, 09:32 PM



Theme © iAndrew 2016 - Forum software by © MyBB