/**
* Checks that the object is valid and able to be stored.
*
* This method checks that the parent_id is non-zero and exists in the database.
* Note that the root node (parent_id = 0) cannot be manipulated with this class.
*
* @return boolean True if all checks pass.
*
* @since 1.7.0
*/
public function check()
{
try {
parent::check();
} catch (\Exception $e) {
$this->setError($e->getMessage());
return false;
}
$this->parent_id = (int) $this->parent_id;
// Set up a mini exception handler.
try {
// Check that the parent_id field is valid.
if ($this->parent_id == 0) {
throw new \UnexpectedValueException(sprintf('Invalid `parent_id` [%1$d] in %2$s::check()', $this->parent_id, \get_class($this)));
}
$query = $this->_db->getQuery(true)->select('1')->from($this->_tbl)->where($this->_tbl_key . ' = ' . $this->parent_id);
if (!$this->_db->setQuery($query)->loadResult()) {
throw new \UnexpectedValueException(sprintf('Invalid `parent_id` [%1$d] in %2$s::check()', $this->parent_id, \get_class($this)));
}
} catch (\UnexpectedValueException $e) {
// Validation error - record it and return false.
$this->setError($e);
return false;
}
return true;
}