| Programming in Joomla 1.5 way |
|
Joomla 1.5 provide a JTabless class for creating,reading,updating, and deleting records in the database table, it's easy to save data into database:
$row = &JTable::getInstance('table_name');
$row->bind($data_array);
$row->store();
Create a new article is a bit complex, now we use the previous method to create article into database:
$row = &JTable::getInstance('content');
$row->bind($data_array);
// sanitise id field
$row->id = (int) $row->id;
$isNew = true;
$user = &JFactory::getUser();
$row->created-by = $user->get('id');
$datenow = &JFactory::getDate();
$row->created = $datenow->toMySQL();
$row->publish_up = $row->created;
$row->publish_down = $db->getNullDate();
$row->state = 0;
$row->params = null;
$row->attribs = "";
$row->metadata = "";
// Prepare the content for saving to the database
ContentHelper::saveContentPrep($row);
if ($row->check()) {
JError::raiseError(500, $db->stderr());
return false;
}
// Increment the content version number
$row->version++;
$dispatcher = &JDispatcher::getInstance();
$result = $dispatcher->trigger('onBeforeContentSave', array($row, $isNew);
if(in_array(false, $result, true)) {
JError::raiseError(500, $row->getError());
return false;
}
// Store the content to the database
if (!$row->store()) {
JError::raiseError(500, $db->stderr());
return false;
}
// Check the article and update item order
$row->checkin();
$row->reorder('catid = '.(int) $row->catid.' AND state >= 0');
$dispatcher->trigger('onAfterContentSave', array(&$row, $isNew));
|