CodeIgniter Forums
Database MVC seperation problems (beginners question) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Database MVC seperation problems (beginners question) (/showthread.php?tid=16301)



Database MVC seperation problems (beginners question) - El Forum - 03-02-2009

[eluser]Slowcheetah[/eluser]
I'm new to OOP, MVC and CodeIgniter. I'm having problems to learn the MVC coding principles. A simple question;

Why this code IS working;

MODEL
Code:
<?php
class Startpagina_Model extends Model {

    function Startpagina_Model()
    {
        // Call the Model constructor
        parent::Model();
        
    }
    
    function Overzicht_Competities()
    {
        $query = $this->db->query('SELECT id, name FROM game_competitions');
        
        foreach ($query->result() as $row)
        {
            echo $row->name;

        }
        
    }
    
}

/* End of file startpagina_model.php */
/* Location: ./system/application/models/startpagina_model.php */

CONTROLLER

Code:
<?php

class Startpagina extends Controller {

    function __construct()
    {
        parent::Controller();
    }


    function index()
    {
        $this->load->model('startpagina_model');
    
        $data['query'] = $this->startpagina_model->Overzicht_Competities();
    
        $this->load->view('startpagina', $data);
    }

}

/* End of file startpagina.php */
/* Location: ./system/application/controllers/startpagina.php */

And this code IS NOT working?

MODEL
Code:
<?php
class Startpagina_Model extends Model {

    function Startpagina_Model()
    {
        // Call the Model constructor
        parent::Model();
        
    }
    
    function Overzicht_Competities()
    {
        $query = $this->db->query('SELECT id, name FROM game_competitions');
        
    }
    
}

/* End of file startpagina_model.php */
/* Location: ./system/application/models/startpagina_model.php */


VIEW

Code:
<html>
<head>
<title>test</title>
</head>
<body>


<?php

foreach ($query->result() as $row)
{
    echo $row->name;
}

?>


</body>
</html>

CONTROLLER

Code:
<?php

class Startpagina extends Controller {

    function __construct()
    {
        parent::Controller();
    }


    function index()
    {
        $this->load->model('startpagina_model');
    
        $data['query'] = $this->startpagina_model->Overzicht_Competities();
    
        $this->load->view('startpagina', $data);
    }

}

/* End of file startpagina.php */
/* Location: ./system/application/controllers/startpagina.php */



Database MVC seperation problems (beginners question) - El Forum - 03-02-2009

[eluser]TheFuzzy0ne[/eluser]
You're not returning the result.

Code:
function Overzicht_Competities()
    {
        return $this->db->query('SELECT id, name FROM game_competitions');
        
    }



Database MVC seperation problems (beginners question) - El Forum - 03-02-2009

[eluser]Slowcheetah[/eluser]
[quote author="TheFuzzy0ne" date="1236068029"]You're not returning the result.[/quote]

Easy as that... thanks!

i suppose this one is also correct?

Code:
function Overzicht_Competities()
    {
        $query = $this->db->query('SELECT id, name FROM game_competitions');
        return $query;
        
    }



Database MVC seperation problems (beginners question) - El Forum - 03-02-2009

[eluser]TheFuzzy0ne[/eluser]
That will work too, although there's not much point in storing the result in a variable as you're not doing anything with the variable other than returning it. Returning the result straight from the query is a little more efficient on RAM usage, and slightly less code to read and maintain.


Database MVC seperation problems (beginners question) - El Forum - 03-02-2009

[eluser]JayTee[/eluser]
What I try to do is imagine that the model, view, and controllers exist on complete separate physical servers - and the controller's job is to handle traffic between them. With that in mind, I'd recommend coding each to handle it's unique job.
Models:
1. Handles all DB interactions
2. Returns record sets to the controller
3. Should rely on input from the controller (shouldn't echo to the browser)

Views:
1. Should NOT have any DB interaction
2. Should only have limited logic and loops for display

Controllers:
1. receive data from the view (forms, clicks, etc)
2. processes/scrubs data
3. moves data between the model and the view

There's something in the wiki that's good to read about MVC:
http://codeigniter.com/wiki/MVC/


Database MVC seperation problems (beginners question) - El Forum - 03-02-2009

[eluser]Slowcheetah[/eluser]
[quote author="JayTee" date="1236068489"]What I try to do is imagine that the model, view, and controllers exist on complete separate physical servers - and the con...........[/quote]

Thanks, i readed this suggestions earlier today in this forum. It's one of the reason's i asked this question..I want to learn the fundaments of mvc and oop..

Thanks for all the help


Database MVC seperation problems (beginners question) - El Forum - 03-02-2009

[eluser]kgill[/eluser]
Honestly, if you're having to ask something as basic as why a function that's missing a return doesn't work, you need to go and learn the basics of PHP & coding first. Jumping in and working on OOP, MVC, and frameworks is fine but without understanding the fundamentals behind it all you're doing yourself a serious disservice.


Database MVC seperation problems (beginners question) - El Forum - 03-02-2009

[eluser]Slowcheetah[/eluser]
[quote author="kgill" date="1236073043"]Honestly, if you're having to ask something as basic as why a function that's missing a return doesn't work, you need to go and learn the basics of PHP & coding first. Jumping in and working on OOP, MVC, and frameworks is fine but without understanding the fundamentals behind it all you're doing yourself a serious disservice.[/quote]

Probably you are right. But what's wrong with learning for a hobby purpose? I learned PHP on a practical rather that a theoratic point of view. (in my old-fashioned 'ad-hoc' PHP programming style i actualy never used the RETURN function), may sound stupid. But now i'm starting to learn those (time-saving) fundamentals an basics so i can bring it a step further.

And i know, it's best to first learn the fundamentals FIRST (i should have learned a few years ago). But this requires some serious concentration, that's my problem.


Database MVC seperation problems (beginners question) - El Forum - 03-02-2009

[eluser]TheFuzzy0ne[/eluser]
[quote author="kgill" date="1236073043"]Honestly, if you're having to ask something as basic as why a function that's missing a return doesn't work, you need to go and learn the basics of PHP & coding first. Jumping in and working on OOP, MVC, and frameworks is fine but without understanding the fundamentals behind it all you're doing yourself a serious disservice.[/quote]
Ouch... It's just as well that not everyone shares your views. I was happy to help, JayTee was also happy to share his pearls of wisdom (which I've enjoyed reading), what's the problem?

Granted, Slowcheetah has much to learn, but the rest of the code looked fine to me, and I've also made the same mistake in the past.


Database MVC seperation problems (beginners question) - El Forum - 03-03-2009

[eluser]kgill[/eluser]
[quote author="Slowcheetah" date="1236073804"]
Probably you are right. But what's wrong with learning for a hobby purpose?
<snip>
And i know, it's best to first learn the fundamentals FIRST (i should have learned a few years ago). But this requires some serious concentration, that's my problem.[/quote]

There's nothing wrong with it, I wasn't suggesting you stop, learning is good but learning without the basics can cause bad habits which makes for bad programming. If you're going to invest the time to learn something, why not do it right. Of course in the end you've got to decide what works best for you.

[quote author="TheFuzzy0ne" date="1236074054"]Ouch... It's just as well that not everyone shares your views. I was happy to help, JayTee was also happy to share his pearls of wisdom (which I've enjoyed reading), what's the problem?[/quote]

You're reading too much into my comment, I said he's doing himself a disservice, without knowing the basics it makes it harder to grasp the more complex things - I don't see how that rates an ouch it's the truth.