Several tables in JTable of joomla 2.5

2019-04-14 19:29发布

问题:

I want to display/add/delete data from several table in MVC component, in JTable class:

class HelloWorldTableHelloWorld extends JTable
{
    function __construct(&$db)
    {
        parent::__construct('code', 'id', $db);
      //parent::__construct('#__fairinfo', 'flight_id', $db);
      //parent::__construct('hotelinfo', 'hotelid', $db);
    }
}

Constructor initializes the id's of tables for deletion and edition purpose. I have successfully displayed the data of three tables, but when I performed the deletion operation then only that table data is delete, which is initailized in JTable class, but if I add all tables and initialize them in JTable it gives me an error.

回答1:

Joomla is not designed to work that way. You are meant to use one table file per database table.

In your model you can instantiate all three tables if needed, but each table file should be separate.

In your model do the following:

 $tableCode = JTable::getInstance('Code', 'HelloWorldTable');
 $tableFairinfo = JTable::getInstance('Fairinfo', 'HelloWorldTable');
 $tableHotelinfo = JTable::getInstance('Hotelinfo', 'HelloWorldTable');

Then you can:

 $tableCode->load($id);

or

 $tableHotelinfo->delete($id); 

etc;

But instead of messing about with all of this, why don't you give the component creator a try?