0

I have a database with two parent tables and several child tables. I need to display a single parent record from one table with all the related records from each child on a single page. I thought the way to do that was to create a model and a view/template for each table and combine them all in the template for the parent table, but I can't figure out how to do this.

I can get the templates for the child views to load on the parent template, but can't figure out how to populate them from the associated model. I tried using the method demonstrated in the lendr tutorial (http://lendr.websparkinc.com/#beginning/editphp) of using a helper to load the view with the model, which seems to be exactly what I need, but I can't figure out how to get joomla to find the view (I get Fatal error: Class 'MycomponentViewWhatevs' not found in C:...). I've searched for days trying to figure out what I'm missing.

Is there a way to do what I'm trying to do? Am I going in the wrong direction? Should I be getting all the data using the parent table's model and duplicating the code for the child tables related to both parents (only a couple are related to both)? This is my first time working with joomla and php, and I am completely lost.

1

I think you're going in the wrong directing a little.

It sounds like you're creating a different view for each of your parent and children templates. What you really want to do is have a single view per page layout or per.. data structure (hard to explain without an example). It shouldn't depend on how you've structured your database. A single view can have many template pieces that make up the final HTML output.

Look at com_content for example. There is a view for "category". That category view has two major looks/layout, the blog layout, and the list layout (default layout). Both are part of the same view because both do the same thing data-wise: they display articles from a single category (and maybe its sub-categories). Within each of the layouts there are multiple template pieces/files that all get build and combined (based on the settings) into the final page:

 /views/category/tmpl/blog.php <-- the main template for the blog layout
 /views/category/tmpl/blog_item.php <-- each article in the blog layout
 /views/category/tmpl/blog_links.php <-- additional links that show after the blog_item.php articles
 /views/category/tmpl/blog_children.php <-- links to sub-categories (if there are any) of the category being displayed

blog.php will grab/include blog_item.php, blog_links.php, and blog_children.php to build the final HTML output.

The data that the category view gets is a combination of multiple tables in the database (#__content, #__assets, #__categories, #__users, etc) and all the data needed to build all those template pieces/files is taken care of in the category model.

In other words, there isn't a separate view for the main blog.php template, and another view to build the blog_item.php template and another view to build the blog_links.php template, etc.

Now, if you DO have vastly different layouts that require multiple views (such as how com_content has a view for a single article, a view for the homepage/featured articles, and the view for list of articles in categories) that's fine. If your models end up having to pull some of the same data in exactly the same way in multiple views you can consider putting those methods in a helper class that you can register using JLoader::register(); or a simple require '';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.