Generating View from MySQL to Simulate the Model-View-Controller Schema in PHP - Defining the next link of the chain: creating a model founded on native MySQL data sets
(Page 4 of 6 )
Logically, the next step required to continue defining this MVC-driven relationship rests on creating a concrete model, which will accept the instructions sent by the controller class that you learned in the previous section.
As I mentioned before, again this model should be logically represented by a MySQL result class. In accordance with this concept, below I coded a new class, which I named "MySQLResult." Here it is:
// define 'MySQLResult' (model)
class MySQLResult{
private $resultController;
private $controllerOutput;
public function __construct(ResultController
$resultController,Result $resultObj){
$this->resultController=$resultController;
$this->controllerOutput=$resultController->getOutput();
$this->resultObj=$resultObj;
}
public function getControllerOutput(){
return $this->controllerOutput;
}
public function getResultSet(){
return $this->resultObj->getResultSet();
}
}
Closely similar to previous examples shown in the two preceding tutorials, the above "MySQLResult" class accepts the controller object itself as its unique input parameter. Of course, the type of output selected by this controller (or view) is assigned as a new property inside the constructor via the "getOutput()" method.
Additionally, the "MySQLResult" class exposes two extra accessing methods, that is "getControllerOutput()" and "getResultSet()" respectively, which are used to generate the different types of views. Short and understandable, right?
Fine, I believe that the previous class is quite comprehensible, therefore after having defined the corresponding model that belongs to the MVC scheme, it's a good time to see how a View element can be constructed and represented by yet another PHP class. That's exactly the subject of the upcoming section, therefore I suggest you to click on the below link and keep reading.
Next: Generating distinct views from a single MySQL result set: creating an output generator class >>
More PHP Articles
More By Alejandro Gervasio