Database management query.

Discussion and education for beginner / novice programmers interested in embarking on the development process to take advantage of the extensible nature of the Joomla! CMS.

Moderators: tjay, seadap, Rogue4ngel, matthewhayashida

Forum rules
Post Reply
reashlin
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Mon Dec 03, 2007 11:23 am

Database management query.

Post by reashlin » Mon Dec 10, 2007 10:54 am

Hey,

I'm relatively new to developing for Joomla! so this may seem a stupid question but how does Joomla! manage dealing with multiple tables within a single component.  I know about using an extension of the JTable class which utilises a __construct function.

However I have only worked with this file refering to one table and what I need to do now is link several tables together.  Can anyone tell me how to do this or show me a good example or even a tutorial.

Many thanks.
Last edited by reashlin on Sun Dec 16, 2007 5:12 pm, edited 1 time in total.

User avatar
manuman
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 211
Joined: Fri Aug 12, 2005 1:58 am
Location: Albany - Western Australia
Contact:

Re: Database management query.

Post by manuman » Tue Dec 11, 2007 12:54 pm

It would depend on what your doing, is it a simple relationship you can do in a standard select or one to many?

If you can provide a bit more detail I can probably assist.

Cheers
Shayne
Core Team Member :: Project Manager & Foundation WG Coordinator
OSM Board Member :: Treasurer
Vote for Joomla! http://www.packtpub.com/nominations-homepage

reashlin
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Mon Dec 03, 2007 11:23 am

Re: Database management query.

Post by reashlin » Fri Dec 14, 2007 2:41 pm

The relationship is fairly simple.  I have 5 tables

  • hotels - Contains a list of hotels and their main details
  • hotel_featurelist - links hotels to features
  • hotel_features - lists features hotels may have
  • hotel_descriptions - describes the type of hotel
  • hotel_nearest_regions - describes the hotels nearest region

The plan is to have it so I can add features regions and descriptions as the list of hotels I have grows.

As I say I like the use of the tables file that many Joomla! components use and would like to use such a file myself.  However I cannot work out how to put multiple tables within this file.
Last edited by reashlin on Fri Dec 14, 2007 5:09 pm, edited 1 time in total.

reashlin
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Mon Dec 03, 2007 11:23 am

Re: Database management query.

Post by reashlin » Sun Dec 16, 2007 1:02 pm

This post is getting many views but not many replies so I am guessing that I am not being detailed enough and so will try to elaborate a little here.

I have a component under development called "hotels".
So my com_hotels folder as of now looks like

..
tables
->hotels.php
admin.hotels.html.php
admin.hotels.php
toolbar.hotels.html.php
toolbar.hotels.php


My current "tables/hotels.php" looks like

Code: Select all

<?php
    defined( '_JEXEC' ) or die( 'Restricted access' );
   
    class TableHotels extends JTable {
        function __construct(&$db) {
            parent::__construct( '#__hotels', 'id', $db );
        }
    }
?>


If I were to add a row from the tables in my database it would look like

Code: Select all

<?php
    defined( '_JEXEC' ) or die( 'Restricted access' );
   
    class TableHotels extends JTable {
       
        // The additional line of code.
        var $id = null;

        function __construct(&$db) {
            parent::__construct( '#__hotels', 'id', $db );
        }
    }
?>


This means that later in my component I can refer directly to the "var $id" to manipulate it.

However what I want to do is build a relational database for my component and so I require access to more than one table.  I can use SELECT queries to access the data in other tables without a problem.  However I would like to INSERT and UPDATE values in other tables and this is where I am having a problem.

I am using functions of Joomla that work out what should be happening in these circumstances (ie. UPDATE or INSERT) by referring just to the "var $x" value.

can this "tables/hotels.php" file be adjusted to refer to many different tables or not.  If so how do I do this.

reashlin
Joomla! Apprentice
Joomla! Apprentice
Posts: 20
Joined: Mon Dec 03, 2007 11:23 am

Re: Database management query.

Post by reashlin » Sun Dec 16, 2007 5:13 pm

OK,

The problem is that I didnt have multiple files within the tables drectory, one relating to each table within the database.


Post Reply