Welcome Guest, Not a member yet? Register   Sign In
Calling data from a model from a view - is this a right way?
#1

[eluser]F. Albrecht[/eluser]
Hi there,

I've just finished two projects with CI. It was the first time for me (and my clients hrhr).

Some frontend CRUD editing, storing the data in a mysql db, a "tell a friend" function - nothing spectacular, only basic stuff. Now I'm at a point to refactore my code to reuse it in further projects.

One problem I've noticed were recurrent snippets for the db stuff:

If I have 2 or 5 or 7 controllers and a view for metadata then I have to fetch the data 2 or 5 or 7 times (in every controller).

I think I follow the MVC paradigma if I fetch the data directly from the view instead of fetching them in the controller. The CI documentation does'nt provide this way - therefore I'm asking here.

Instead of doing this n-times in everey controller

Code:
$data['something'] = $this->my_model->get_all();
   $this->load->view('my_view'',$data);

I would like to use in my view

Code:
<?
   $something = $this->my_model->get_all();
?>
<h2>&lt;?=$something['title']?&gt;</h2>


Am I right with my thoughts?

Cheers & thanks
#2

[eluser]Yash[/eluser]
But dear model function will call similar time. I mean whenever view loads for controller functions... model function will be called. So there is no difference in time.

Even you can try to call by using super object reference type. See under section Utilizing CodeIgniter Resources within Your Library[url="http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html"]SEE this[/url] .I never and don't know it can also work for views.
#3

[eluser]F. Albrecht[/eluser]
Not in time but in typing it on my keyboard Wink

For example for 6 controllers I have to type the function 6 times into every controller. With calling from a view I do have to type the funcion only once.
#4

[eluser]Yash[/eluser]
it's just matter of copy paste ;-P
#5

[eluser]F. Albrecht[/eluser]
C & P is the key point - I would like to avoid this. If I would like to C and P I can code my scripts in the procedural way...
#6

[eluser]mdowns[/eluser]
Calling the model from the view will work if you are loading the same data for each controller. If you need to process the data differently depending on the controller, it is probably nicer to put the call in each controller.
#7

[eluser]Colin Williams[/eluser]
It's not the worst thing to do in the world, but it's kinda hard to just consider it a View now. It's a View and Controller all in one. So, you've gained some things, but you've also sacrificed the whole concept of separating logic from presentation. Consider the conversation, "I need to change the way this data is retrieved, but for some reason I don't see where it's loaded in our application's logic." "Oh, that actually happens in a View file.." "Hrm, not where I expected to find business logic, but ok..."


There are many ways to "call something all the time," like employing Libraries that are autoloaded, or Hooks, or extending the CI_Controller class, or plugin systems, and on and on and on...
#8

[eluser]F. Albrecht[/eluser]
Hmm ok, but what is about these sentences:

Quote:The view generally have free access to the model, but should not change the state of the model. Views are read only representations of the state of the model. The view reads data from the model using query methods provided by the model.

Source: http://www.phpwact.org/pattern/model_view_controller

Did I have misunderstood something? Other threads show the concept of "a view calls a model", for example this one: http://ellislab.com/forums/viewthread/86128/
#9

[eluser]Colin Williams[/eluser]
Well, it obviously doesn't concern my interpretation of MVC or phpwact.org's interpretation of MVC. Subscribe to whichever you will. When I think of a "read only representation," that doesn't include querying the model. Reading error_strings or properties, maybe, but not running queries. That's the part where I find difference in most of my projects.

Ultimately, any way that "works" is right, is it not?
#10

[eluser]fatnic[/eluser]
Can you not just assign the data to a public variable in the controller construct?
Code:
$this->$data['something'] = $this->my_model->get_all();

Then to data will be available to all functions.




Theme © iAndrew 2016 - Forum software by © MyBB