Callbacks in PHP ActiveRecord¶
Introduction¶
Callbacks in PHP ActiveRecord are methods that allow you to execute custom code at specific points in a model's lifecycle. They provide hooks to intervene in various stages of record creation, updating, and deletion, as well as to implement data validation. Understanding and using callbacks effectively enhances the flexibility and customizability of your models.
Why Use Callbacks?¶
Customization: Callbacks enable you to customize and extend the behavior of your models without modifying core code.
Data Integrity: They help enforce data integrity by allowing you to run validation checks and perform data transformations.
Business Logic: Callbacks let you encapsulate complex business logic within your models, improving maintainability.
Callbacks Overview¶
-
after_construct
Theafter_construct
callback is triggered immediately after a new model instance is created, but before it's populated with data. It's useful for performing tasks like setting default attribute values or initializing resources.Example:
class User extends ActiveRecord\Model { public function after_construct() { $this->status = 'active'; // Set a default value for 'status' } }
-
before_create
Thebefore_create
callback runs just before a new record is saved to the database. It's ideal for actions such as data manipulation or validation before data insertion.Example:
class User extends ActiveRecord\Model { public function before_create() { $this->password = password_hash($this->password, PASSWORD_DEFAULT); // Hash the password before saving } }
-
after_create
Theafter_create
callback is executed after a new record has been successfully created and saved to the database. It's suitable for post-creation actions, like sending notifications or updating related records.Example:
class User extends ActiveRecord\Model { public function after_create() { // Send a welcome email to the user Mailer::sendWelcomeEmail($this->email); } }
-
before_update
Thebefore_update
callback is invoked just before an existing record is updated in the database. It allows you to modify data or perform actions on the model instance before saving changes.Example:
class User extends ActiveRecord\Model { public function before_update() { // Update the 'updated_at' timestamp $this->updated_at = date('Y-m-d H:i:s'); } }
-
after_update
Theafter_update
callback is triggered after an existing record has been successfully updated in the database. It can be used for tasks like logging changes or sending notifications.Example:
class User extends ActiveRecord\Model { public function after_update() { Logger::logUserUpdate($this->id); // Log the user's update activity } }
-
before_destroy
Thebefore_destroy
callback is invoked just before a record is deleted from the database. It allows you to perform any necessary cleanup or validation before deletion.Example:
class User extends ActiveRecord\Model { public function before_destroy() { if ($this->hasPendingOrders()) { throw new Exception('Cannot delete user with pending orders'); } } }
-
after_destroy
Theafter_destroy
callback is triggered after a record has been successfully deleted from the database. It can be used for tasks like updating related records or sending notifications.Example:
class User extends ActiveRecord\Model { public function after_destroy() { // Clear user association from orders Order::where('user_id', $this->id)->update(array('user_id' => null)); } }