Page 1 of 1

Database management query.

Posted: Mon Dec 10, 2007 10:54 am
by reashlin
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.

Re: Database management query.

Posted: Tue Dec 11, 2007 12:54 pm
by manuman
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

Re: Database management query.

Posted: Fri Dec 14, 2007 2:41 pm
by reashlin
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.

Re: Database management query.

Posted: Sun Dec 16, 2007 1:02 pm
by reashlin
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.

Re: Database management query.

Posted: Sun Dec 16, 2007 5:13 pm
by reashlin
OK,

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