Welcome Guest, Not a member yet? Register   Sign In
model in view, or passing from view strange behavior
#1

(This post was last modified: 10-25-2019, 10:47 AM by Goddard.)

When passing any variable to an object function like $object->my_function($object_id);  it doesn't give you the object id as you would expact.  Instead you get a variable containing every single object in the array you were looping over.

Is this expected behavior?
Reply
#2

Show us what my_function() function in your object looks like.
What type of object is it? CI uses objects (classes) in controllers, models and libraries.
You load a library in a controller with $this->load->library('library_name');
To call a function in the library: $var = $this->library_name->my_function($parameter);
Reply
#3

(10-25-2019, 12:29 PM)Wouter60 Wrote: Show us what my_function() function in your object looks like.
What type of object is it? CI uses objects (classes) in controllers, models and libraries.
You load a library in a controller with $this->load->library('library_name');
To call a function in the library: $var = $this->library_name->my_function($parameter);

Well I had to reimplement it because the original wasn't working, but it is pretty simple.

myfunction($db_object) {
   $returnValue = null;
   foreach($db_object as $db_row_value){
      if($db_row_key == $my_key)
         $returnValue = $db_row_value;
   }
   return $returnValue;
}

This is obviously not exactly, but this is the gist of it.


If I call this function from inside my view by passing in my model it has unexpected behavior. 

In the view I will loop over a $array_of_db_objects and then with the model $my_model->myfunction($db_object);

Instead of it being a single object it is the entire array I was looping over inside the view.

I hope this makes sense.
Reply
#4

It's bad practice to call a model function from a view.
Typically, a model is used to retrieve information from your database. You should call the model inside your controller, not inside a view.
After you've retrieved the data, pass it to the view to display it in your browser.

If you need some repetitive action inside your view, you can simply create a php function inside the view itself, or create your own helper and load that (before you open the view).
Have a look at the helper documentation here: https://codeigniter.com/user_guide/general/helpers.html

In your case, it's all about the $db_object variable that is processed by the function.
According to the function, it is an array, because you have a foreach ... structure to loop through it's elements.
So my question is: what do you pass to the function, and what do you expect as it's return value?
Reply
#5

(This post was last modified: 10-29-2019, 07:27 AM by Goddard.)

In the view, if you pass it an array. You can then use it in your view.

so if I loop over that array in my view, but I need to alter one value in the view for whatever reason I can use my model to change it.

view.php
-----------
foreach($db_object_array as $db_object){
echo $db_object_model->my_function($db_object);
}

When doing the above the result is still a $db_object_array inside "my_function"

Really you should be able to just do, $db_object->my_function() and get your value, but database results in CI3 are not actual models of your data structure and more of just a collection of SQL functions.
Reply
#6

It sure looks like unexpected behavior.
If I do this in a view:
PHP Code:
//$records is the result from my model, passed by the controller
Echo '<pre>';
Foreach(
$records as $record) {
 
   Print_r($record); 
   Echo '<hr>';

}
Echo 
'</pre>; 
I get the data from all individual records, separated by a horizontal line.
If I would pass $record to a function, only THAT record will be passed, not the complete set of records.
My advice is not to use your own objects, but the objects CI provides for.
If your function has database operations, put it in a model.
If there's no database operation, put it in a library.
If it's a simple operation that doesn't need OOP approach, put in a helper.
In all cases, load the model, library or helper in your controller.
Use $this->model_name->function_name(), or $this->library_name->function_name() or $this->helper_name->function_name() to call the approriate function.
Reply
#7

(10-29-2019, 09:14 AM)Wouter60 Wrote: It sure looks like unexpected behavior.
If I do this in a view:
PHP Code:
//$records is the result from my model, passed by the controller
Echo '<pre>';
Foreach(
$records as $record) {
 
   Print_r($record); 
   Echo '<hr>';

}
Echo 
'</pre>; 
I get the data from all individual records, separated by a horizontal line.
If I would pass $record to a function, only THAT record will be passed, not the complete set of records.
My advice is not to use your own objects, but the objects CI provides for.
If your function has database operations, put it in a model.
If there's no database operation, put it in a library.
If it's a simple operation that doesn't need OOP approach, put in a helper.
In all cases, load the model, library or helper in your controller.
Use $this->model_name->function_name(), or $this->library_name->function_name() or $this->helper_name->function_name() to call the approriate function.

Pass the $record variable inside your loop to your model that they pass to the view.
Reply
#8

(This post was last modified: 10-29-2019, 11:09 AM by Wouter60.)

Quote:Pass the $record variable inside your loop to your model that they pass to the view.

No, pass the $record variable inside your loop to your model, library or helper that will give a return value.
Reply
#9

(10-29-2019, 11:08 AM)Wouter60 Wrote:
Quote:Pass the $record variable inside your loop to your model that they pass to the view.

No, pass the $record variable inside your loop to your model, library or helper that will give a return value.

I was just explaining what happens and how it is not expected behavior.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB