Welcome Guest, Not a member yet? Register   Sign In
Model Example is Bad in User Guide
#11

[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]
#12

[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)
{
   //forces to have the required data before doing anything on your function
}
#13

[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
#14

[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)
{
   //forces to have the required data before doing anything on your function
}
[/quote]

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
#15

[eluser]Madoc[/eluser]
Ok, thank you for the replies !
#16

[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
#17

[eluser]Madoc[/eluser]
So you add a level of separation doing something like that ?

Code:
//in controller
function process_something()
{
    //validation rules here
    
    if ($this->form_validation->run())
    {
        //array your post values
        $form_values = array(
            'input1' => $this->input->post('input1'),
            'input2' => $this->input->post('input2'),
            'textarea1' => $this->input->post('textarea1')
        );
        
        $this->load->library('process_forms');
        if ($this->process_forms->process_my_something_form($form_values))
        {
            //your something is processed as your library function
            //checked the validity of the data and called the
            //model function that inserts (or else) the record
        }
    }
    
}
#18

[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




Theme © iAndrew 2016 - Forum software by © MyBB