pub trait ActiveModelBehavior: ActiveModelTrait {
// Provided methods
fn new() -> Self { ... }
fn before_save<'life0, 'async_trait, C>(
self,
db: &'life0 C,
insert: bool,
) -> Pin<Box<dyn Future<Output = Result<Self, DbErr>> + Send + 'async_trait>>
where C: ConnectionTrait + 'async_trait,
Self: Send + 'async_trait,
'life0: 'async_trait { ... }
fn after_save<'life0, 'async_trait, C>(
model: <Self::Entity as EntityTrait>::Model,
db: &'life0 C,
insert: bool,
) -> Pin<Box<dyn Future<Output = Result<<Self::Entity as EntityTrait>::Model, DbErr>> + Send + 'async_trait>>
where C: ConnectionTrait + 'async_trait,
Self: Send + 'async_trait,
'life0: 'async_trait { ... }
fn before_delete<'life0, 'async_trait, C>(
self,
db: &'life0 C,
) -> Pin<Box<dyn Future<Output = Result<Self, DbErr>> + Send + 'async_trait>>
where C: ConnectionTrait + 'async_trait,
Self: Send + 'async_trait,
'life0: 'async_trait { ... }
fn after_delete<'life0, 'async_trait, C>(
self,
db: &'life0 C,
) -> Pin<Box<dyn Future<Output = Result<Self, DbErr>> + Send + 'async_trait>>
where C: ConnectionTrait + 'async_trait,
Self: Send + 'async_trait,
'life0: 'async_trait { ... }
}Expand description
Lifecycle hooks for an ActiveModelTrait.
Every entity must have an impl of this trait — even the empty
impl ActiveModelBehavior for ActiveModel {} you see in the examples is
required so that the default new / before_save / after_save /
before_delete / after_delete implementations are wired up. Override
the hooks to enforce invariants, populate computed columns, or run side
effects around saves and deletes.
use sea_orm::entity::prelude::*;
impl ActiveModelBehavior for ActiveModel {
async fn before_save<C>(mut self, _db: &C, insert: bool) -> Result<Self, DbErr>
where
C: ConnectionTrait,
{
if insert && self.created_at.is_not_set() {
self.created_at = Set(chrono::Utc::now());
}
self.updated_at = Set(chrono::Utc::now());
Ok(self)
}
}Provided Methods§
Sourcefn new() -> Self
fn new() -> Self
Build a fresh ActiveModel. Defaults to ActiveModelTrait::default
(every column NotSet); override to pre-fill columns:
fn new() -> Self {
Self {
status: Set(Status::New),
..ActiveModelTrait::default()
}
}Sourcefn before_save<'life0, 'async_trait, C>(
self,
db: &'life0 C,
insert: bool,
) -> Pin<Box<dyn Future<Output = Result<Self, DbErr>> + Send + 'async_trait>>where
C: ConnectionTrait + 'async_trait,
Self: Send + 'async_trait,
'life0: 'async_trait,
fn before_save<'life0, 'async_trait, C>(
self,
db: &'life0 C,
insert: bool,
) -> Pin<Box<dyn Future<Output = Result<Self, DbErr>> + Send + 'async_trait>>where
C: ConnectionTrait + 'async_trait,
Self: Send + 'async_trait,
'life0: 'async_trait,
Hook invoked before insert, update, and save. insert is true
for inserts. Return an error to abort the operation.
Sourcefn after_save<'life0, 'async_trait, C>(
model: <Self::Entity as EntityTrait>::Model,
db: &'life0 C,
insert: bool,
) -> Pin<Box<dyn Future<Output = Result<<Self::Entity as EntityTrait>::Model, DbErr>> + Send + 'async_trait>>where
C: ConnectionTrait + 'async_trait,
Self: Send + 'async_trait,
'life0: 'async_trait,
fn after_save<'life0, 'async_trait, C>(
model: <Self::Entity as EntityTrait>::Model,
db: &'life0 C,
insert: bool,
) -> Pin<Box<dyn Future<Output = Result<<Self::Entity as EntityTrait>::Model, DbErr>> + Send + 'async_trait>>where
C: ConnectionTrait + 'async_trait,
Self: Send + 'async_trait,
'life0: 'async_trait,
Hook invoked after insert, update, and save succeed. Receives
(and may transform) the resulting Model.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementors§
impl ActiveModelBehavior for sea_orm::rbac::entity::permission::ActiveModel
rbac only.impl ActiveModelBehavior for sea_orm::rbac::entity::resource::ActiveModel
rbac only.impl ActiveModelBehavior for sea_orm::rbac::entity::role::ActiveModel
rbac only.impl ActiveModelBehavior for sea_orm::rbac::entity::role_hierarchy::ActiveModel
rbac only.impl ActiveModelBehavior for sea_orm::rbac::entity::role_permission::ActiveModel
rbac only.impl ActiveModelBehavior for sea_orm::rbac::entity::user_override::ActiveModel
rbac only.impl ActiveModelBehavior for sea_orm::rbac::entity::user_role::ActiveModel
rbac only.