Jump to content

MVC model


Recommended Posts

Ok here's the problem. I am trying to make a model object for my system. I'm trying to work step by step so things like a registerclass and a controller etc are not yet implemented. First of all the procedural code:

<?phperror_reporting(E_ALL);$included = true;//configuration globals//This will be arranged by the registry object later on$host			= "localhost";$username		= "root";$password		= "";$database		= "core";$dbprefix		= 'core';//Load classfilesinclude('classes/class_database.php');include('classes/class_module.php');include('classes/class_layout.php');include('classes/class_model.php');//Start database connection$db = new database($host, $username, $password, $database);//The container object is an extention of the module object. It loads files and returns its output//In this case it loads the template and returns its html$template = new container('themes/Redroest/layout.php', '', 'layout');//The template html will be stored in the template object to insert containers that load later on$layout = new layout($template->getBody());//The model will load moduleinfo from the database using query's//To switch to another storage type just change this model$model = new dbModule($db);//The model contains a method to load all the requested modules//If no condition is set it will load the home module + the blocks using 'WHERE home=1 OR blockMode=1';$model->condition = 'WHERE moduleID=13 OR blockMode=1';$model->getModules();//Output the final layoytecho $layout->getLayout();

Second is my model. This is where it goes wrong. The model contains a query that loads the modules using a while loop. My goal is to process these modules using my moduleclass without writing them in my model. This should be controlled using the controller....??? I inserted my situation in the code, I hope to get a solution or another approach to solve this problem.

<?phpclass publicModel{	private $db;		function __construct(database $db)	{	  //Check if the database connection exists	  if($db->ping())		{		  $this->db = $db;		}			  }	//Load a default condition when no condition is set	public function __get($value)	{	if(!isset($this->condition))		{		  $this->condition = 'WHERE home=1 OR blockMode=1';		}	}	//Load all required modules  public function getModules()  {			  $sql = 'SELECT * FROM core_modules '.$this->condition;	if(!$ModuleQuery = $this->db->query($sql))	{		  //I know I know, this should be an exeption	  trigger_error('Error in the modulequery: '.$this->db->error);	}	else	{	  //$modules = array();	  while($ResultModule = $ModuleQuery->fetch_assoc()) 	  {		//This is where it goes wrong..... I can push the modules into another array and return it 				//but this requires me to make another loop in for example my controller object to load them 				//into containers and to push them into a layout.								 //Load a new module object		  $modules[$ResultModule['moduleID']] = new container('Modules/'.$ResultModule['controllerFile'], 'Modules/'.$ResultModule['headerFile'], $ResultModule['title']);		//Set the layout for the module			$modules[$ResultModule['moduleID']]->setLayout('themes/Redroest/'.$ResultModule['layoutFile']);			//get the final output marked up with layout			$container = $modules[$ResultModule['moduleID']]->getOutput();			//Insert the container into the layout			$layout->setContainer($ResultModule['positionTag'], $container);		}	} 	}}		?>

Help me on this please. I spend some hours trying to solve this puzzle, but couldn't find any usable examples

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...