Welcome Guest, Not a member yet? Register   Sign In
Need a bit of help using CI to build a Website.
#11

[eluser]Oliver Ponder[/eluser]
[quote author="juanvillegas" date="1283057591"]I don’t believe that’s the way. At least if you want to stick strictly to MVC modelling. If you google images for “MVC” you’ll find no connectin between models and views, just view<->controller<->model .
Justification for this is that models are usually closely related to a database table, and we don’t want the table structure to spread all over the application. As more we can hide the details of the table, the better. For example, in the code you posted that view is only going to work with a table having content as its main field. A better approach would be using Model::FIELD_CONTENT to ask the model for the content column name, and a better one to delegate this to controller.
Also, the controller is there to control what information is forwarded to views and how. If we accept views having access to models then we don’t need controllers anymore than for rendering.

My approach for this would be setting up some filters system like we do in Rails. For example, all methods requiring a “news” block have a before_filter procedure “instantiate_news”, which somehow (setting session, flashdata or just retrieving news). You can write libraries to simulate filters (coding in the constructor) and using $this->load->library in the controllers construct. [/quote]

The problem I run into when doing such strict seperation of the view and model is that I end up repeating myself a LOT in the controller if I'm simply trying to get a modular piece of information into my main template.

For example, say my site has 3 things going on.

1. Pages
2. People profiles
3. News items

And I want recent news items to show up on pages and also on people profiles, that means I would be repeating myself in both the pages and people controllers, calling the news_model and passing the results to the view.

And say you have a bunch of little bits, then it becomes quite annoying to have a controller which was originally intended for Pages calling a bunch of other models.

I get your point though about the view becoming dependent on a certain setup of the model, which is what we should be trying to avoid.

The filter stuff is going a little over my head though, but I'm getting started with rails, so hopefully I'll understand the correct approach better.

EDIT: Sorry by the way, Lord Ve, for taking this to a slight tangent. I think the issue is relevant though to your original question
#12

[eluser]Buso[/eluser]
[quote author="juanvillegas" date="1283057591"][quote author="Oliver Ponder" date="1283054862"]I'm not sure of its 'morally' wrong, but what I sometimes do is call a model from a sub view.

So for example, in your main view you call a sub-view "news" (its ok to call views from views right?)

Code:
&lt;? $this->load->view('sub/news');  ?&gt;

Which then iterates through results it obtains from a model your wrote for it

Code:
//views/sub/news.php

<div id="news">
<h2>News of the day:</h2>
&lt;?
    $news = $this->News_model->get_news();
    
    foreach ($news as $news_item){
        echo '<p>'.$news_item->content.'</p>';
    }
?&gt;
</div>

I'd appreciate a more experienced coders comment on this approach.[/quote]

I don't believe that's the way. At least if you want to stick strictly to MVC modelling. If you google images for "MVC" you'll find no connectin between models and views, just view<->controller<->model . [/quote]
Here is a picture with the views related to the models : http://en.wikipedia.org/wiki/Model–View–Controller

Quote:Justification for this is that models are usually closely related to a database table, and we don't want the table structure to spread all over the application. As more we can hide the details of the table, the better. For example, in the code you posted that view is only going to work with a table having content as its main field. A better approach would be using Model::FIELD_CONTENT to ask the model for the content column name, and a better one to delegate this to controller.
Also, the controller is there to control what information is forwarded to views and how. If we accept views having access to models then we don't need controllers anymore than for rendering.
We still need them to authorize and pick the next action to execute (maybe show a view, or redirect), which is their main purpose anyway.

Quote:My approach for this would be setting up some filters system like we do in Rails. For example, all methods requiring a "news" block have a before_filter procedure "instantiate_news", which somehow (setting session, flashdata or just retrieving news). You can write libraries to simulate filters (coding in the constructor) and using $this->load->library in the controllers construct.

To sum up, each framework and user has his own vision of the MVC pattern, which isn't wrong, and 'strict MVC' doesn't mean 'better'. For example, the data validation is suposed to go in the models, but codeigniter tells us to do it in the controllers, and it works fine for most ppl.

Why would it be right to call a helper (which could also call models or not) from a view, and calling a model would be wrong?
It's not like we were authenticating the user in the view. We are just fetching some extra data.
#13

[eluser]Lord Ve[/eluser]
To be honest, I was hoping for a simple solution, but thus far, I can only understand the usage of about 1/3 of the above posts. But not something I need worry about right now as I am still learning.

I have another question. Why doesn't this work:
Code:
//Login_Model
function validate(){
$this->db->where('username', $this->input->post('username'));
        $this->db->where('password', md5($this->input->post('password')));
        $query = $this->db->get('login');

        if($query->num_rows == 1)
        {
            foreach($query->result() as $row){
            $data[] = $row;
            }
            return $data;
        }
        else
        {
            return FALSE;
        }
}

With:
Code:
//Main Controller
        function validate_credentials()
    {        
        $this->load->model('membership_model');
        $query = $this->membership_model->validate();
        
        if($query !== FALSE) // if the user's credentials validated...
        {
            $id = $query['id'];
            $data = array(
                'username' => $this->input->post('username'),
                'id' => $id, //<- Error Below
                'is_logged_in' => true
            );
            $this->session->set_userdata($data);
            $this->index();
        }
        else // incorrect username or password
        {
            $this->index();
        }
    }

I get this:
Quote:Severity: Notice

Message: Undefined index: id

Filename: controllers/home.php

Line Number: 30
and
Quote:Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at C:\Xampp\xampp\htdocs\system\libraries\Exceptions.php:166)

Filename: libraries/Session.php

Line Number: 662

I'm not even sure I am doing it right to begin with. The data is there, but I can't seem to access it... Can see the values of $query if I use print_r for example...
#14

[eluser]Buso[/eluser]
use result_array() if you need arrays

otherwise access the data as object properties: $query->id
#15

[eluser]Lord Ve[/eluser]
I see. That makes sense.
#16

[eluser]juanvillegas[/eluser]
Two things regarding this:
I have researched a little bit and i guess this is our solution (located in one of the core sections of the guide, shame on us! )
http://ellislab.com/codeigniter/user-gui...views.html

It would be as easy as loading the header, passing on some data which would be the news in this case. A before filter is appended to every method needed the header with news, and ready up!. I think the article calls it dinamycally loading views.

This is the MVC schema proposed by CI official guide:

http://ellislab.com/codeigniter/user-gui...pflow.html

But i agree there may be some others where view and models communicate.

Regards
#17

[eluser]Lord Ve[/eluser]
I'm already dynamically displaying views. ^_^ I load my view, template, and it calls the views needed and passes variables to them. But no worries. There just isn't a simple way to do what I asked in my original question...




Theme © iAndrew 2016 - Forum software by © MyBB