Model Example is Bad in User Guide - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Model Example is Bad in User Guide (/showthread.php?tid=32588) Pages:
1
2
|
Model Example is Bad in User Guide - El Forum - 03-29-2011 [eluser]MNG GHOST[/eluser] [quote author="InsiteFX" date="1297159850"]This is why use use a library and you set all of your input post data in the library then you pass the $data array to your model! Very simple! InsiteFX[/quote] Dear All, In a Model structure, there only be parameters like function GetMenus($parrams = array(), $id = 0) { // here we can manipulate what is comming from our user interfaces, so we can use $parrams array or our single parameter id. //Its not needed to use directly input->post(); //Since the idea behind the model is just interacting with DB and returning to any where its called. } Naser Gulzade reply: [email protected] Model Example is Bad in User Guide - El Forum - 03-29-2011 [eluser]Madoc[/eluser] Apologies to be a bit off topic but for the sake of clarification as I am a CI beginner: [quote author="InsiteFX" date="1297159850"]This is why use use a library and you set all of your input post data in the library then you pass the $data array to your model! [/quote] By library do you mean the controller ? [quote author="InsiteFX" date="1297159850"]then you pass the $data array to your model! [/quote] You pass your array to the model function rather than the model right ? [quote author="MNG GHOST" date="1301402223"] function GetMenus($parrams = array(), $id = 0) { // here we can manipulate what is comming from our user interfaces, so we can use $parrams array or our single parameter id. //Its not needed to use directly input->post(); //Since the idea behind the model is just interacting with DB and returning to any where its called. } [/quote] Am I wrong in saying that you generate your array on the controller (using the input->post()) ? And for my information: why use an array rather than individual parameters for each information intended to be used on the model ? is it better practice ? Isn't it more risky due to the flexibility of the array (no size restriction on the array passed to the function) ? Code: function GetMenus($param1, $param2, $param3, $param4, $id = 0) Model Example is Bad in User Guide - El Forum - 03-29-2011 [eluser]InsiteFX[/eluser] You can setup the data to be passed in either your Controller or a Library then pass it to the method/function in your Model. I use a Library to keep everything in one place like my Auth library in handles all the forms and information for the User_model. This is just the way I do it. InsiteFX Model Example is Bad in User Guide - El Forum - 03-29-2011 [eluser]MNG GHOST[/eluser] [quote author="Madoc" date="1301456492"]Apologies to be a bit off topic but for the sake of clarification as I am a CI beginner: [quote author="InsiteFX" date="1297159850"]This is why use use a library and you set all of your input post data in the library then you pass the $data array to your model! [/quote] By library do you mean the controller ? [quote author="InsiteFX" date="1297159850"]then you pass the $data array to your model! [/quote] You pass your array to the model function rather than the model right ? [quote author="MNG GHOST" date="1301402223"] function GetMenus($parrams = array(), $id = 0) { // here we can manipulate what is comming from our user interfaces, so we can use $parrams array or our single parameter id. //Its not needed to use directly input->post(); //Since the idea behind the model is just interacting with DB and returning to any where its called. } [/quote] Am I wrong in saying that you generate your array on the controller (using the input->post()) ? And for my information: why use an array rather than individual parameters for each information intended to be used on the model ? is it better practice ? Isn't it more risky due to the flexibility of the array (no size restriction on the array passed to the function) ? Code: function GetMenus($param1, $param2, $param3, $param4, $id = 0) hi, That is not a professional way to convert an array to the parameters, You can use both (Array + Vars) according to your data coming and going. Since you are going filter each data before interacting with db, - If you use array, your code will use less memory than using lots of params. - Your code will be well defined and can be understand - You can manipulate all array options in your code, e.g. Merge, sort,push, etc Model Example is Bad in User Guide - El Forum - 03-30-2011 [eluser]Madoc[/eluser] Ok, thank you for the replies ! Model Example is Bad in User Guide - El Forum - 03-30-2011 [eluser]InsiteFX[/eluser] Most of the Active Record methods expect an array to passed to them! look at the Insert and Update Methods. As I stated above my preference is to use a Library, I build my libraries so that they are more of and API Library. In my library I do all the form handling, accessing the models and showing the form views. InsiteFX Model Example is Bad in User Guide - El Forum - 03-30-2011 [eluser]Madoc[/eluser] So you add a level of separation doing something like that ? Code: //in controller Model Example is Bad in User Guide - El Forum - 03-30-2011 [eluser]InsiteFX[/eluser] Yes something like that, but I have a custom load_view in my libraries that load the views from the library. InsiteFX |