Before Save
public function onBeforeSave{$table}( mixed &$src, JTable $table ) : bool
Fires before saving a record.
Description
This hook is triggered before creating or updating a record. External plugins can use this hook to bind the object that is going to be saved.
The dynamic portion of the hook name, $table
, refers to the name of the table calling the hook. This means that, for example, the room table will trigger an hook called onBeforeSaveRoom
. The name of the table is always equals to the base name of the file that declares the $table
instance.
It is possible to fetch the name of a table by accessing the following folder:
/administrator/components/com_vikrestaurants/tables/
TIP: in case of failure while saving the record, it is possible to throw an exception to abort the saving process and return a readable error message. The same can be accomplished by registering an error to the table and returning false
.
// throw an exception
throw new Exception('Missing required field', 404);
// or register the error
$table->setError('Missing required field');
return false;
Parameters
- &$src
-
(array|object) Either an array or an object specifying the table properties to bind.
- $table
-
(JTable) The table instance.
Return Value
Boolean. Use false to abort the saving process.
Example
/**
* Trigger hook to allow the plugins to bind the object that
* is going to be saved.
*
* @param mixed &$data The array/object to bind.
* @param JTable $table The table instance.
*
* @return bool False to abort the saving process.
*/
public function onBeforeSaveReservation(&$data, $table)
{
/**
* @todo it is possible to bind here the $data array/object
*/
return true;
}
Changelog
Version | Description |
---|---|
1.8 | Introduced. |