Skip to main content

ActiveModelBehavior

Trait ActiveModelBehavior 

Source
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§

Source

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()
    }
}
Source

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.

Source

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.

Source

fn before_delete<C>(self, db: &C) -> Result<Self, DbErr>
where C: ConnectionTrait,

Hook invoked before delete. Return an error to abort.

Source

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§

Source§

impl ActiveModelBehavior for sea_orm::rbac::entity::permission::ActiveModel

Available on crate feature rbac only.
Source§

impl ActiveModelBehavior for sea_orm::rbac::entity::resource::ActiveModel

Available on crate feature rbac only.
Source§

impl ActiveModelBehavior for sea_orm::rbac::entity::role::ActiveModel

Available on crate feature rbac only.
Source§

impl ActiveModelBehavior for sea_orm::rbac::entity::role_hierarchy::ActiveModel

Available on crate feature rbac only.
Source§

impl ActiveModelBehavior for sea_orm::rbac::entity::role_permission::ActiveModel

Available on crate feature rbac only.
Source§

impl ActiveModelBehavior for sea_orm::rbac::entity::user_override::ActiveModel

Available on crate feature rbac only.
Source§

impl ActiveModelBehavior for sea_orm::rbac::entity::user_role::ActiveModel

Available on crate feature rbac only.