Back to Table class

Method checkIn

public bool
checkIn
(mixed $pk = null)
Method to check a row in if the necessary properties/fields exist.
Parameters
  • mixed $pk An optional primary key value to check out. If not set the instance property value is used.
Returns
  • bool True on success.
Since
  • 1.7.0
-
  • \UnexpectedValueException
Class: Table
Project: Joomla

Method checkIn - Source code

/**
 * Method to check a row in if the necessary properties/fields exist.
 *
 * Checking a row in will allow other users the ability to edit the row.
 *
 * @param   mixed  $pk  An optional primary key value to check out.  If not set the instance property value is used.
 *
 * @return  boolean  True on success.
 *
 * @since   1.7.0
 * @throws  \UnexpectedValueException
 */
public function checkIn($pk = null)
{
    // Pre-processing by observers
    $event = AbstractEvent::create('onTableBeforeCheckin', ['subject' => $this, 'pk' => $pk]);
    $this->getDispatcher()->dispatch('onTableBeforeCheckin', $event);
    // If there is no checked_out or checked_out_time field, just return true.
    if (!$this->hasField('checked_out') || !$this->hasField('checked_out_time')) {
        return true;
    }
    if (\is_null($pk)) {
        $pk = array();
        foreach ($this->_tbl_keys as $key) {
            $pk[$this->{$key}] = $this->{$key};
        }
    } elseif (!\is_array($pk)) {
        $pk = array($this->_tbl_key => $pk);
    }
    foreach ($this->_tbl_keys as $key) {
        $pk[$key] = empty($pk[$key]) ? $this->{$key} : $pk[$key];
        if ($pk[$key] === null) {
            throw new \UnexpectedValueException('Null primary key not allowed.');
        }
    }
    // Get column names.
    $checkedOutField = $this->getColumnAlias('checked_out');
    $checkedOutTimeField = $this->getColumnAlias('checked_out_time');
    $nullDate = $this->_supportNullValue ? 'NULL' : $this->_db->quote($this->_db->getNullDate());
    $nullID = $this->_supportNullValue ? 'NULL' : '0';
    // Check the row in by primary key.
    $query = $this->_db->getQuery(true)->update($this->_tbl)->set($this->_db->quoteName($checkedOutField) . ' = ' . $nullID)->set($this->_db->quoteName($checkedOutTimeField) . ' = ' . $nullDate);
    $this->appendPrimaryKeys($query, $pk);
    $this->_db->setQuery($query);
    // Check for a database error.
    $this->_db->execute();
    // Set table values in the object.
    $this->{$checkedOutField} = $this->_supportNullValue ? null : 0;
    $this->{$checkedOutTimeField} = $this->_supportNullValue ? null : '';
    // Post-processing by observers
    $event = AbstractEvent::create('onTableAfterCheckin', ['subject' => $this, 'pk' => $pk]);
    $this->getDispatcher()->dispatch('onTableAfterCheckin', $event);
    Factory::getApplication()->triggerEvent('onAfterCheckin', array($this->_tbl));
    return true;
}