Just a little explanation for a beginner... |
[eluser]knucklehead[/eluser]
Hello! I am getting started with CI, just as with MVC approach... So, please tell me if I understood the logic of all this correctly; Let's say I have a relational database full of artists and events for a club. I would like to get clean URLs at the end, something like last.fm style www.blah.com/artists/NAME www.blah.com/events/EVENTNAME etc. So, I should start with creating models for db queries, right? Let's call it artistsModel.php; Code: <?php Also, my results are now not 'grouped'. I wonder if I should do 'grouping' in my controller or my view? What I mean by that? If an artist has more than 1 link to their website i will get more than 1 row of that artist from db. So I should make each artist another 'object' with 'formatted' properties before I start to manipulate with him, right? I imagine my object would look like artist_name = string artist_description = string artist_links = array() artist_countries = array() ... And all my artists should look like that... So, should I create those objects in controller or view? If I make a controller like this: Code: class Artists extends Controller{ Should I make some programming to define an artist 'object' in controller and another programming in view to loop through my objects? I also wonder which datatype should be $data here? Should it be also an array of objects? Because right now, if i var_dump($data) I get an array with 1 value and it is called 'artists' which is actually an array of my rows... Is that ok? If I make some programming and create objects inside of controller, how should I 'pack' my results and send it to view? Uf, I am sorry for all the confusion and I know this is some extremely basic and beginner stuff, but I started with OOP few weeks ago and I am still a little bit confused about everything... Any suggestion/advice/help is extremely welcome! =) Thanks!
[eluser]jedd[/eluser]
Hi knucklehead and welcome to the CI forums. A couple of things that might help - it's a big message you've posted there with a fair bit of stream of consciousness going on First, CI libraries and helpers stick with the underscore separator, in preference to the camel case thing. Personal preference, of course, but if you're not fussed, consider flipping now, early on. Grouping - do it in your model. If you might want to group differently, either have a private model method that does the actual call and work, and a few 'public' model methods that do subtly different things - or just use an additional parameter to your method in your model, such as defining the grouping, ordering, etc aspects. You might also be more comfortable with returning your data as arrays rather than objects - this is what I do, and I find it a bit easier on my brain. The functions are pretty similar, and are documented next to the object-returning functions in the manual.
[eluser]knucklehead[/eluser]
Hi and thanks for quick replies and warm welcoming =) I am used to write with camel case, but yes, I have noticed those underscores all around source code and I will listen to your advice and switch, before I actually start with everything! Grouping in model - ok, I can make what you said - group it in model or make some private/public methods. I hope you understand that this is not mysql query grouping, this is actual formatting of my artist object, right... =) But ok, let me see if I understood you correctly; in my model: Code: function get_all_artists(){ ...then my controller stays the same as above example (but without properties) and my view gets an $artists array which I format to some HTML. My $artists ends up like an array full of artist arrays, not objects... Would that be the correct logic?
[eluser]BrianDHall[/eluser]
[quote author="knucklehead" date="1253904378"]...then my controller stays the same as above example (but without properties) and my view gets an $artists array which I format to some HTML. My $artists ends up like an array full of artist arrays, not objects... Would that be the correct logic?[/quote] Yup, that's just fine - but you can make it easier on yourself by using CI's built-in ActiveRecord functions. As in: Code: function get_all_artists(){ $a is then an array of all your artists, so in your view you can do a foreach to print out the results all pretty. If you would prefer to deal with objects you can set $a to $query->result() and then it will be an array of objects. Up to you whether you prefer $artist->name or $artist['name']. EDIT: As an extra bit of fun, if you are using PHP5 you can make it a one-liner function: Code: function get_all_artists(){ Code is your worst enemy - the less of it there is, generally the better off you are so long as it is well organized
[eluser]jedd[/eluser]
And yes, if you want to restructure your data - such that you have a 2D array where the first dimension is the artist_id say, as shown, then I'd probably do that in my model - especially if that was what I consistently wanted back from that function. If I wanted different formatted results, then .. it's a bit less obvious which way to go. You could have a helper function that mungs consistently formatted input into whatever output layout you want, or duplicate your model methods to provide the different outputs, or a private function within your controller (especially if the scope of this problem is a single controller). Not to rain on BrianDHall's appreciation of objects, but in response to your 'array of array's question/example above, here's what I do for one of my model methods - it might be of relevance: Code: function get_appellations_info () { In this particular instance I wanted the key to be the ID and preferred to mung it in the model. I could probably have adjusted my view logic to cope with an array of two-element arrays (id and text). Six of one, in many cases, I suspect.
[eluser]Colin Williams[/eluser]
The model should always return nice, shiny, polished objects to the controller. If your controller starts manipulating data structures too much, it's time to refactor.
[eluser]kmil0[/eluser]
for models I always use something like this Code: function get($id = null,$limit = null, $offset = null)
[eluser]knucklehead[/eluser]
Hello everybody! Sorry for a delay, I had some terrible weeks... I was reading more about MVC pattern and how to use it, and I was reading carefully your posts... I have to thank you all for great tips and explanations!!!! I love CI and I love this forum =) I was playing with my 'application' for a while and I have few more beginners' dilemmas so please stick with me just a little more =) OK, my models now look similar to this; Code: class Events_model extends Model{ @kmil0 Your example looks very nice and clean, I will sure try to use it in the near future =) Then, my controller handles the model like this: Code: class Events extends Controller{ Anyway, I have two big misunderstandings here; 1) In my model i declared some properties for that model (in my case $event_ID, $event_title, etc.) but I actually never use them... This seems somehow wrong, nah? =) Or maybe I will need them later, when I will try to insert/update my data?? Seems like I am missing something really important here =) and 2) In my case I have 2 'major' classes: Events and Artists. Both of classes have that method for 'formatting' query (in my case functions format_event($r) and format_artist($r)). In mysql, those two tables (events and artists) are in many to many relation so I have a junction table to handle this. My final events view (when for example method get_all_events() from model is used) should print out list of all events and within each event list of one or more artists. I don't understand how to link all those fancy-formatted artists (which I made in artists_model) inside of my Events? Should I do it in events_model - to retrieve data with some other method/query (but I cannot call other models from a model, right?), or should I do it in controller - after retrieving all events make some loop (commented line in controller) and associate each artist for particular event here? I hope I didn't confuse you even more and that you will write some nice posts like before =D Thanks for any help in advance! |
Welcome Guest, Not a member yet? Register Sign In |