Welcome Guest, Not a member yet? Register   Sign In
The concept of models

This is a slightly beginner-ish question, but I've been avoiding the matter since I started using CI and thought it's about time I asked. I've been sticking all my queries in my controllers so far, which works just fine, but want to start making use of models. I understand the concept of models, but the one thing that slightly baffles me is, what's the best course of action with the result the query in the model returns?

At the moment, with my queries in my controller, I send the query result to the view and foreach through the results there. Is this the best method for the using models to? Or is it best to foreach through the results within the model? I can't help but think the latter is a slightly daft way of doing it, as wouldn't it require any HTML the results are going to be structured with to be in the model?

Apologies if the question itself is a bit daft. Just wanting to get using those darn models. Smile

[eluser]Pascal Kriete[/eluser]
Well it does depend on your data, but generally your workflow won't change much. The controller will go from:
$answer = final query
$answer = ask_the_model()

Since ideally you want the Model to be an abstraction of the data, it should not know anything about presentation.
So while I may loop through the data in the model on occasion, it is never to add html etc to it. It is only to do very basic formatting (making sure that dates are formatted how the view wants it for example) or to do summations or other calculations on the data before returning it.

Thanks for the feedback guys. Much appreciated.

I've gone ahead and started to use models, but seems I've fallen at the first hurdle. I can't see anything obvious wrong with the code, but it something must be wrong as all I get is a blank white page.

<?php if($getSongList->num_rows() > 0):?>

    <?php foreach($getSongList->result() as $song):?>
        <?$artist = $song->artist?>
        <?$album = $song->album?>
        <?$songID = $song->id?>

    <?php endforeach;?>

<?php endif;?>

function loadSongList()
        $data['getSongList'] = $this->music_model->get_SongList();
        $this->load->view('includes/musicList_include.php', $data);

function get_SongList()
    $query = $this->db->query("SELECT id, title, playtime, artist, album, genre, fileOrigName FROM songs WHERE ownerID='".                            $_SESSION['username']."' ORDER BY artist ASC");
    return $query->result();

The same query was working just fine previously when I had all my queries in the controller. Anyone any ideas?

[eluser]Pascal Kriete[/eluser]
Did you load your model?

Yeah. It's loaded in the constructer. Should it be moved to the function that's using it?

[eluser]Pascal Kriete[/eluser]
No, that's fine, as long as it's loaded. Try capitalizing the model name. CI is quirky with names and it's not properly documented. Just do it exactly as in the example. File name lowercase, class, load, and usage uppercase.

Capitalized the model name, and still no luck. I did notice that, when checking what the page does get returned using Firebug, everything before the code I've posted above in my view loads. But soon as it tries to load the "<?php if($getSongList->num_rows() > 0):?>" and after it just stops.

[eluser]Code Arachn!d[/eluser]
It's been awhile since I've banged through this on the documentation - but I think that using num_rows() after the result has been called is pointless since you've essentially exited the query mode and now have either an array or data object.

Essentially fix your view like this:
<?php foreach($getSongList as $song) { ?>
        <?= $song->artist; ?>
        <?= $song->album; ?>
        <?= $song->id; ?>
    <?php } ?>

Whipped out the IF num_rows(), and still no luck. Starting to drive me slightly mad. Smile

Theme © iAndrew 2016 - Forum software by © MyBB