Joomla save() Method of Admin Model

It is a method to save the form data into the database.

  • param array $data - The form data.
  • return boolean - True on success, False on error.

public function save($data)
{
$dispatcher = \JEventDispatcher::getInstance();
$table = $this->getTable();
$context = $this->option . '.' . $this->name;

if (!empty($data['tags']) && $data['tags'][0] != '')
{
$table->newTags = $data['tags'];
}

$key = $table->getKeyName();
$pk = (!empty($data[$key])) ? $data[$key] : (int) $this->getState($this->getName() . '.id');
$isNew = true;

// Include the plugins for the save events.
\JPluginHelper::importPlugin($this->events_map['save']);

// Allow an exception to be thrown.
try
{
// Load the row if saving an existing record.
if ($pk > 0)
{
$table->load($pk);
$isNew = false;
}

// Bind the data.
if (!$table->bind($data))
{
$this->setError($table->getError());

return false;
}

// Prepare the row for saving
$this->prepareTable($table);

// Check the data.
if (!$table->check())
{
$this->setError($table->getError());

return false;
}

// Trigger the before save event.
$result = $dispatcher->trigger($this->event_before_save, array($context, $table, $isNew, $data));

if (in_array(false, $result, true))
{
$this->setError($table->getError());

return false;
}

// Store the data.
if (!$table->store())
{
$this->setError($table->getError());

return false;
}

// Clean the cache.
$this->cleanCache();

// Trigger the after save event.
$dispatcher->trigger($this->event_after_save, array($context, $table, $isNew, $data));
}
catch (\Exception $e)
{
$this->setError($e->getMessage());

return false;
}

if (isset($table->$key))
{
$this->setState($this->getName() . '.id', $table->$key);
}

$this->setState($this->getName() . '.new', $isNew);

if ($this->associationsContext && \JLanguageAssociations::isEnabled() && !empty($data['associations']))
{
$associations = $data['associations'];

// Unset any invalid associations
$associations = ArrayHelper::toInteger($associations);

// Unset any invalid associations
foreach ($associations as $tag => $id)
{
if (!$id)
{
unset($associations[$tag]);
}
}

// Show a warning if the item isn't assigned to a language but we have associations.
if ($associations && $table->language === '*')
{
\JFactory::getApplication()->enqueueMessage(
\JText::_(strtoupper($this->option) . '_ERROR_ALL_LANGUAGE_ASSOCIATED'),
'warning'
);
}

// Get associationskey for edited item
$db = $this->getDbo();
$query = $db->getQuery(true)
->select($db->qn('key'))
->from($db->qn('#__associations'))
->where($db->qn('context') . ' = ' . $db->quote($this->associationsContext))
->where($db->qn('id') . ' = ' . (int) $table->$key);
$db->setQuery($query);
$old_key = $db->loadResult();

// Deleting old associations for the associated items
$query = $db->getQuery(true)
->delete($db->qn('#__associations'))
->where($db->qn('context') . ' = ' . $db->quote($this->associationsContext));

if ($associations)
{
$query->where('(' . $db->qn('id') . ' IN (' . implode(',', $associations) . ') OR '
. $db->qn('key') . ' = ' . $db->q($old_key) . ')');
}
else
{
$query->where($db->qn('key') . ' = ' . $db->q($old_key));
}

$db->setQuery($query);
$db->execute();

// Adding self to the association
if ($table->language !== '*')
{
$associations[$table->language] = (int) $table->$key;
}

if (count($associations) > 1)
{
// Adding new association for these items
$key = md5(json_encode($associations));
$query = $db->getQuery(true)
->insert('#__associations');

foreach ($associations as $id)
{
$query->values(((int) $id) . ',' . $db->quote($this->associationsContext) . ',' . $db->quote($key));
}

$db->setQuery($query);
$db->execute();
}
}

return true;
}

How this Method Works?

The save() is used for both saving new data and updating existing data.

First, it gets the table object using model's getTable(). In case of update, it loads the data based on the primary key.

Then, it binds the data, checks for sanitisation, and stores the data into the database.