joomla store value Using the JTable Class

The first step is to extend the JTable class with your new table class in table folder:

class TableRestaurant extends JTable 
   { 
   var $id = null; 
   var $name = null; 
   var $address = null; 
   var $comments = null; 
   var $comment_date = null;
   var $published = null; 
   function __construct(&$db) 
      {
      parent::__construct( '#__restaurant', 'id', $db );
      } 
   }

To include the file, place this line in your extension's source code (use com_nameofyourcomponent in place of com_restaurant):
JTable::addIncludePath(JPATH_ADMINISTRATOR.DS.'components'.DS.'com_restaurant'.DS.'tables');
To get an instance of the object, use this code:
$row =& JTable::getInstance('restaurant', 'Table');

In a model class (extends JModel) you can also use:

$row =& $this->getTable('restaurant');

Updating is as simple as setting the row values in this manner.

$row->name = 'ashwin';
$row->address = 'test address';
$row->comments = 'Open Source Developer';
// Save results
$row->store();

Notice that the $row object of type TableRestaurant has all the columns we defined above.  When we call the store() method these rows are automatically stored in the database.  Our code above assumes that the id field will be auto-incremented on store(), so we are not providing a value for this field (as well as comment_date and published).  So this code will store a new value for this restaurant (essentially running and 'INSERT' query for us).  If we had provided value for the id field, the store() method would have instead 'UPDATE' query.
Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s