0

OK, please before slamming me guys know that I have been studying and studying and am new to PHP OOP/MVC. I am really trying to learn and learn the right way from the start.

My current code is as follows and it WORKS. I just want to know if this is proper formatting to fall in line with MVC standards. Unfortunately I was able to find stuff for a single query on a MySQL database but when it comes to performing multiple record query's I cannot find any good information.

For that reason I thought this question would not only be helpful to me but to others like me in the future. So without further ado here is my code:

Main Page:

$job_controller = new job_controller();
$job_model = new job_model();
$job_view = new job_view();
$job_model->get_jobs();

$job_controller->job_query($country, $job_model->jobStatus, $job_model->skill1, $job_model->skill2, $job_model->skill3, $job_model->skill4, $job_model->skill5);

Job Classes:

class job_model extends job_controller{

    public $skills;
    public $skillarray;
    public $skill1;
    public $skill2;
    public $skill3;
    public $skill4;
    public $skill5;
    public $jobStatus;

    public function get_jobs(){
        $this->skills = 'web';
        $this->skillarray= explode(',', $this->skills);
        $this->skill1 = $this->skillarray[0];
        $this->skill2 = $this->skillarray[1];
        $this->skill3 = $this->skillarray[2];
        $this->skill4 = $this->skillarray[3];
        $this->skill5 = $this->skillarray[4];
        $this->jobStatus = 1;
    }
}

class job_view extends job_controller{

    public function set_jobs($jobid, $jobname, $jobDescription, $jobCategory, $jobTags, $jobStatus, $jobOwner, $jobOwnerName, $jobWorkerName, $jobState, $jobCountry){

        $this->jobid = $jobid;
        $this->jobname = $jobname;
        $this->jobDescription = $jobDescription;
        $this->jobCategory = $jobCategory;
        $this->jobTags = $jobTags;
        $this->jobStatus = $jobStatus;
        $this->jobOwner = $jobOwner;
        $this->jobOwnerName = $jobOwnerName;
        $this->jobWorkerName = $jobWorkerName;
        $this->jobState = $jobState;
        $this->jobCountry = $jobCountry;

        echo '<p><strong><a href="../jobs/view_job.php?jobID='.$this->jobid.'">' . $this->jobname . '</a></strong><br>'
        . $this->jobDescription . '<br>'
        . $this->jobOwnerName . '<br></p>';
    }   
}

DB Classes:

class job_controller extends dbconnect{


    public function job_query($country, $jobStatus, $skill1, $skill2, $skill3, $skill4, $skill5){
        $this->country = $country;
        $this->jobStatus = $jobStatus;
        $this->skill1 = $skill1;
        $this->skill2 = $skill2;
        $this->skill3 = $skill3;
        $this->skill4 = $skill4;
        $this->skill5 = $skill5;
        echo $this->country . $this->jobStatus . $this->skill1;

        $jq = $this->con()->prepare("SELECT jobID, jobName, jobDescription, jobCategory, jobTags, jobStatus, jobOwner, job_owner_name, job_worker_name, jobState, jobCountry FROM jobs WHERE jobCountry=? AND jobStatus=? AND jobCategory LIKE ? OR jobcategory LIKE ? OR jobcategory LIKE ? OR jobcategory LIKE ? OR jobcategory LIKE ?  ORDER BY jobID");
        $jq-> bind_param('sisssss', $this->country, $this->jobStatus, $this->skill1, $this->skill2, $this->skill3, $this->skill4, $this->skill5);
        $jq-> execute();
        $jq-> bind_result($jobid, $jobname, $jobDescription, $jobCategory, $jobTags, $jobStatus, $jobOwner, $jobOwnerName, $jobWorkerName, $jobState, $jobCountry);
        while($jq->fetch()){
            $job_view = new job_view();
            echo $job_view->set_jobs($jobid, $jobname, $jobDescription, $jobCategory, $jobTags, $jobStatus, $jobOwner, $jobOwnerName, $jobWorkerName, $jobState, $jobCountry);
        }
    }   
}

If this is not proper please let me know what I could do to change it. I know that using a framework is best but this is for school and that is not an option. Thanks in advance for any help :)

Shadow
  • 33,525
  • 10
  • 51
  • 64
Joe Alvini
  • 306
  • 1
  • 3
  • 15
  • 1
    Even if you're not going to use an existing MVC framework, you should get familiar with how they work. This is… well, I'm not sure what to call it, but it is not any recognizable form of MVC. –  Jul 16 '18 at 22:57

1 Answers1

1

if this is proper formatting to fall in line with MVC standards

What is and what isn't MVC for PHP is quite ambiguous. However, choosing to use the keywords "model", "view", "and controller" does not thereby mean you are using any "known" or reasonably sane MVC approach (or even any good architectural pattern approach whether MVC or otherwise).

I won't debate your MVC is not MVC, but your code has smells and suffers from various things.

"Generally speaking":

Model should not extend a controller.
View should not extend a controller.

It seems you have this the wrong way around really. While there are differences of opinion on this, so I'm not saying this is right just a valid argument - a view should not "control" it should just produce the GUI. In fact, really, view shouldn't even be a controller or class it should be given the data to represent the page and attempt to render it (like a template, again in the simplest form). Model should just be domain business logic and such like, which would be called upon, not be a place that would itself call upon a controller.

In it's simplest form, MVC in PHP would have a controller request requirements from model, manipulate them, and pass them to view. There can be many ways to do this and various other things wiring it all together (like services, factories, traits etc).

Info about MVC:
This is the most relevant info I think to your current questions and trying to learn:
https://blog.ircmaxell.com/2014/11/a-beginners-guide-to-mvc-for-web.html

Also:
https://stackoverflow.com/a/5864000/2632129

Regarding your questions about inheritance:
Inheritance isn't always the better way:
https://en.wikipedia.org/wiki/Composition_over_inheritance https://softwareengineering.stackexchange.com/a/302052/104375

I also suggest this - it's maybe above beginner level but it's worth trying to get some of it to sink in:
https://blog.ircmaxell.com/2013/11/beyond-inheritance.html
https://blog.ircmaxell.com/2013/09/beyond-design-patterns.html

James
  • 4,644
  • 5
  • 37
  • 48
  • I didnt think that naming them model view controller would make them valid MVC I did that as to make it easier for someone to read when I posted it on here to find out if I was doing it correctly which I am clearly not. So I guess its back to the drawing board and I will start over. – Joe Alvini Jul 17 '18 at 00:45
  • I wasn't being funny when I said that :) just making it clear really. Also, maybe don't focus on what is and what isn't MVC, but more what is a good structure that works for your code and requirements. MVC is a good guideline to initiate good structure and practices, but it's not a complete blueprint to follow :) – James Jul 17 '18 at 10:46
  • Oh sorry. But i have changed it around a bit. So if I'm not mistaken it should work like this. Controller takes in the info and does what it needs to do, it then passes that info to Model which queries the database, then Model passes it to view which makes it able to view the information. So moving back to my original code, I had model and controller mixed up for one (I fixed that). I was also querying Model (which was then Controller, it was mixed up) from the main page which was not correct, I shouldve been querying that from Controller (which was then model, again cause I had it mixed up). – Joe Alvini Jul 17 '18 at 12:12
  • Yeah on a basic level that's about right :) it gets more complex when you have more complex requirements, such as you said "model queries the DB" but really model should be more than one part, with (eg) a service and repository and ORM. – James Jul 17 '18 at 12:20
  • Hey, Im sorry but one more question, can a controller extend a model? I seen that you said model should not extend controller but can controller extend model or are they not supposed to touch at all? – Joe Alvini Jul 17 '18 at 12:34
  • There are no "rules" perse I'm just debating the tried and tested approaches :) So yes a controller can extend something from a model (a service or whatever), but "inheritance" (`extend`) isn't always (or usually) the best way to get data or functionality into a class, because you can only extend once. Here is some good reading on that: https://en.wikipedia.org/wiki/Composition_over_inheritance. I also suggest this (although it's complex it'll sink in eventually) https://blog.ircmaxell.com/2013/11/beyond-inheritance.html and https://blog.ircmaxell.com/2013/09/beyond-design-patterns.html – James Jul 17 '18 at 14:28
  • Thanks James, I need more people like you teaching me the right ways of doing things. I just wish it extended past this post. It is very hard to find people willing to teach. However I did like what you said about extending only once. God I hate that LOL. I found that to be a problem quite a few times. – Joe Alvini Jul 17 '18 at 15:27