CRUD (Create Read Update Delete) is the name given to the four common data manipulation tasks.

The first thing we need to do in our class is to define the public properties. The public properties relate directly to the fields and must have exactly the same names. We use these properties as a 'buffer' to store individual records.

The second thing we need to do is to define the constructor. In order to use the JTable::getInstance() method, we must override the JTable constructor with a constructor that has a single referenced parameter, the database object.

The third thing we need to do is override the check() method. This method is used to validate the buffer contents, returning a Boolean result. If a check() fails we use the setError() method to set a message that explains the reason why the validation failed.

/**
* #__myextenstion_foobars table handler
*
*/
class TableFoobar extends JTable
{
/** @var int Primary key */
var $id = null;
/** @var string Content */
var $content = null;
/** @var int Checked-out owner */
var $checked_out = null;
/** @var string Checked-out time */
var $checked_out_time = null;
/** @var string Parameters */
var $params = null;
/** @var int Order position */
var $ordering = null;
/** @var int Number of views */
var $hits = null;
/**
* Constructor
*
* @param database Database object
*/
function __construct( &$db )
{
parent::__construct('#__myextension_foobars', 'id', $db);
}
/**
* Validation
*
* @return boolean True if buffer is valid
*/
function check()
{
if(!$this->content)
{
$this->setError(JText::_('Your Foobar must contain some
content'));
return false;
}
return true;
}
}

 

Now CRUD:

$table->reset();
$table->set('content', "Lorem ipsum dolor sit amet");
$table->set('ordering', $table->getNextOrder());
if ($table->check())
{
if (!$table->store())
{
// handle failed store
// use $table->getError() for an explanation
}
}
else
{
// handle failed check
// use $table->getError() for an explanation
}

So, we have following methods:

set - set data in table
load - loads data from table
delete - delete records from table

Taken from book Packt Publishing Mastering Joomla 1.5 Extension and Framework Development.

Component you can download from here. Table you will must to create by your self.

If you want to add your javascript to Joomla! header from your component, you can do it like this:

JHTML::_('behavior.mootools');
 $doc =& JFactory::getDocument();
$js = "

function show(options)
{
$('update').innerHTML = '<div align=".'"center"'."><img src="/.'"images/ajax-loader.gif" width="66" height="66"/>'."<p/>Prosim poÄŤakajte</div>'
var ajaxRequest = new Ajax('index.php', {
method: 'get',
 onComplete: function(response) {
  $('update').innerHTML = response;
  }
  });
  ajaxRequest.request(options);
 }

 window.addEvent('domready', function()
 {
 $('form1').addEvent('submit', function(e)
  {  $('update').innerHTML = '<div align=".'"center"'."><img src="/.'"images/ajax-loader.gif" width="66" height="66"/>'."<p/>Prosim poÄŤakajte</div>'
  // Stop the form from submitting
  new Event(e).stop();
  // Update the page
  this.send({ update: $('update') });
  });
  });";
  $doc->addScriptDeclaration($js);

Javascript in component you can add only once, if you try to do it twice, one javascript will be overwriten with another. 

If you want instead of adding event to button in custom form to add ajax to some link (javascript function) here is how you can do it: 

function show(options)
         {
           $('update').innerHTML = '<div align=".'"center"'.">
  <img src="/.'"images/ajax-loader.gif" width="66" height="66"/>'."<p/>Please wait</div>'
           var ajaxRequest = new Ajax('index.php', {
                   method: 'get',
                   onComplete: function(response) {
                       $('update').innerHTML = response;
                   }
               });
               ajaxRequest.request(options);
}

 

Where options are variables which you sent through ajax, for example:

<a onclick="show('option=com_regdavzav&task=result&format=raw&pageNumber=2&naziv=ANGEL')" href="#">2</a>

It seems that mootools which are coming with Joomla! old, so documents about mootools which are in Joomla! you can find here.

 

When creating component, in manifest file, xml, name of the component must be name of file which are you using.

This example will work:

<?xml version="1.0" encoding="utf-8"?>
<install version="1.5.0" type="component">
  <name>Component</name>
  <author>Stanko MiloL?ev</author>
  <version>1.0.0</version>
  <description>Search for taxpayers from database</description>
  <files>
    <filename component="com_regdavzav">Component.php</filename>
  </files>
  <install>
  </install>
  <uninstall>
  </uninstall>
  <administration>
    <menu>Registar davÄTnih zavezancev</menu>
  </administration>
</install>

This example will not work:

<?xml version="1.0" encoding="utf-8"?>
<install version="1.5.0" type="component">
  <name>Component</name>
  <author>Stanko MiloL?ev</author>
  <version>1.0.0</version>
  <description>Search for taxpayers from database</description>
  <files>
    <filename component="com_regdavzav">com_component.php</filename>
  </files>
  <install>
  </install>
  <uninstall>
  </uninstall>
  <administration>
   <menu>Registar davÄTnih zavezancev</menu>
  </administration>
</install>

Because  <filename component="com_regdavzav">com_component.php</filename>, com_component.php is different from Component as it is defined in <name>Component</name>

Subcategories