pub trait ActiveModelBehavior: ActiveModelTrait {
// Provided methods
fn new() -> Self { ... }
fn before_save<C>(self, db: &C, insert: bool) -> Result<Self, DbErr>
where C: ConnectionTrait { ... }
fn after_save<C>(
model: <Self::Entity as EntityTrait>::Model,
db: &C,
insert: bool,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>
where C: ConnectionTrait { ... }
fn before_delete<C>(self, db: &C) -> Result<Self, DbErr>
where C: ConnectionTrait { ... }
fn after_delete<C>(self, db: &C) -> Result<Self, DbErr>
where C: ConnectionTrait { ... }
}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 {
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<C>(self, db: &C, insert: bool) -> Result<Self, DbErr>where
C: ConnectionTrait,
fn before_save<C>(self, db: &C, insert: bool) -> Result<Self, DbErr>where
C: ConnectionTrait,
Hook invoked before insert, update, and save. insert is true
for inserts. Return an error to abort the operation.
Sourcefn after_save<C>(
model: <Self::Entity as EntityTrait>::Model,
db: &C,
insert: bool,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>where
C: ConnectionTrait,
fn after_save<C>(
model: <Self::Entity as EntityTrait>::Model,
db: &C,
insert: bool,
) -> Result<<Self::Entity as EntityTrait>::Model, DbErr>where
C: ConnectionTrait,
Hook invoked after insert, update, and save succeed. Receives
(and may transform) the resulting Model.
Sourcefn before_delete<C>(self, db: &C) -> Result<Self, DbErr>where
C: ConnectionTrait,
fn before_delete<C>(self, db: &C) -> Result<Self, DbErr>where
C: ConnectionTrait,
Hook invoked before delete. Return an error to abort.
Sourcefn after_delete<C>(self, db: &C) -> Result<Self, DbErr>where
C: ConnectionTrait,
fn after_delete<C>(self, db: &C) -> Result<Self, DbErr>where
C: ConnectionTrait,
Hook invoked after delete succeeds.
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.