0

I read tutorial on Joomla.org about com development using database in the link https://docs.joomla.org/Special:MyLanguage/J3.2:Developing_an_MVC_Component/Using_the_database. on the file admin/tables/helloworld.php I found a class I don't understand. this is the code.

<?php
/**
 * @package     Joomla.Administrator
 * @subpackage  com_helloworld
 *
 * @copyright   Copyright (C) 2005 - 2018 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */
// No direct access
defined('_JEXEC') or die('Restricted access');

/**
 * Hello Table class
 *
 * @since  0.0.1
 */
class HelloWorldTableHelloWorld extends JTable
{
    /**
     * Constructor
     *
     * @param   JDatabaseDriver  &$db  A database connector object
     */
    function __construct(&$db)
    {
        parent::__construct('#__helloworld', 'id', $db);
    }
}

in the class HelloWorldTableHelloWorld there is only constructor with 1 line parent::__construct('#__helloworld', 'id', $db); and no other code. but I believe it does something I dont understand. can anyone explain this , thanks

1 Answer 1

0

The Joomla JTable class has got a lot of functionality for performing CRUD operations on database tables. The code above is extending this JTable class so that it can be used for CRUD operations on the #__helloworld table.

Elsewhere in the code for this tutorial step there is

$table = $this->getTable();

which results in the call to the JTable static method getInstance()

public function getTable($type = 'HelloWorld', $prefix = 'HelloWorldTable', $config = array())
    {
        return JTable::getInstance($type, $prefix, $config);
    }

Because of the parameters passed into JTable::getInstance() the code there will look for a class called HelloWorldTableHelloWorld and to try to find it, it will look in certain directories and for certain filenames - this is why you have to put the code for this file into admin/tables/helloworld.php for it to be found.

Once it's found the file, and the class within the file, it creates an instance of that class, and that's when the constructor will be called. The constructor provides the name of the database table (namely #__helloworld, where the #__ will get replaced by the prefix you've set on your joomla tables) and the primary key to use for database accesses (namely the id field), plus the database connector.

When the HelloWorldTableHelloWorld instance has been created then you can easily perform CRUD operations eg

$table->load($id);

which will read into memory the record from this table whose id is specified by $id and make the fields available as properties eg $table->greeting.

You can read more about how JTable works in the Joomla Table Basic API Guide.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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