Welcome Guest, Not a member yet? Register   Sign In
MVC and Objects
#1

[eluser]Jareish[/eluser]
Hi,

Yesterday I ran intoo an architectual problem and I can't seem to figure it out.

Models connect to a database, Views is the output that users see, and Controllers manage data between models and views.

but where do you put the Object Classes?

For instance:

you have Project Controller, a project has an id and a description. A Project consists of a collection of Pages and a collection of Widgets. Since they are part of eachother, you use the projectid as parameter to get the information of the pages and widgets.

Project Controller Class:
Code:
function show_member_page(){
$data['widgets'] = $this->widgets_model->get_widgets($projectid);
$data['pages'] = $this->pages_model->get_pages($projectid);

$this->load->view('members_page', $data);
}

and the get_pages function would consist of a database query which returns an array of pages. Next you pass the $data to your view and you put the right stuff on the right spot. Untill this point there was no problem.

But loading up vars with database queries is not really a structured setup. This will work fine with 1 project, but when you create a new project, you have to fill the info through the widget/pages model.

I would like to do something like, $project = new Project(); And I could get the description through $project->Description, $project->get_pages(), $project->get_widgets();

But $project is an Object/Class here not a Controller, nor a Model or a View. I think you would instantiate $project in a controller. Instatiation would run the cunstructor to adress the model if needed.

So my question would be, where would you write such Classes and where do you instantiate them. I have the feeling I'm missing something. The definition of a car, a wheel or an engine should be declared outside the MVC pattern, but the creation/variations of a car in side the MVC?

Any tips?

regards
#2

[eluser]toopay[/eluser]
What you need is Module, HMVC instead MVC.
#3

[eluser]Jareish[/eluser]
Thank you for your reply, I'm gonna look intoo this (i found a nice tut @ http://net.tutsplus.com/tutorials/php/hv...plication/)

Atm I 'solved' it by calling creating the class in the model.

require 'libaries/Project.class.php';

and later in the get_project method of the projects_model I create a new project() and load it up, and return it to the controller. Main benefit about this is, that I can create a hierarchy in the object, which would be hard or hard to read through the default method ($data['projects'][0]['pages']['0']['title'] etc...) Instead I can do In the view:
$defaultpage = $project->get_page(0);
echo $defaultpage->title;




Theme © iAndrew 2016 - Forum software by © MyBB