public function lock($userId = null)
{
if (!$this->getId()) {
throw new CannotLockNotLoadedRecord();
}
if (!$this->hasField('locked_on') && !$this->hasField('locked_by')) {
return $this;
}
$this->triggerEvent('onBeforeLock', array());
$db = $this->getDbo();
if ($this->hasField('locked_on')) {
$date = new \JDate();
$locked_on = $this->getFieldAlias('locked_on');
$this->{$locked_on} = $date->toSql(false, $db);
}
if ($this->hasField('locked_by')) {
if (empty($userId)) {
$userId = $this->container->platform->getUser()->id;
}
$locked_by = $this->getFieldAlias('locked_by');
$this->{$locked_by} = $userId;
}
$this->save();
$this->triggerEvent('onAfterLock');
return $this;
}