From 24f63c9b822996c1c8004c82d0d82e2389ac0a8c Mon Sep 17 00:00:00 2001 From: Billy Chan Date: Mon, 18 Oct 2021 16:31:35 +0800 Subject: [PATCH] ActiveModelBehavior (SeaQL/sea-orm#161) --- .../03-expanded-entity-structure.md | 35 +++++++++++++------ 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/SeaORM/docs/03-generate-entity/03-expanded-entity-structure.md b/SeaORM/docs/03-generate-entity/03-expanded-entity-structure.md index ff305608ccf..55533e8a3fb 100644 --- a/SeaORM/docs/03-generate-entity/03-expanded-entity-structure.md +++ b/SeaORM/docs/03-generate-entity/03-expanded-entity-structure.md @@ -153,22 +153,37 @@ Handler for different actions on an `ActiveModel`. impl ActiveModelBehavior for ActiveModel { /// Create a new ActiveModel with default values. Also used by `Default::default()`. fn new() -> Self { - ::default() + Self { + uuid: Set(Uuid::new_v4()), + ..ActiveModelTrait::default() + } + } + + /// Will be triggered before insert / update + fn before_save(self, insert: bool) -> Result { + if self.price.as_ref() <= &0.0 { + Err(DbErr::Custom(format!( + "[before_save] Invalid Price, insert: {}", + insert + ))) + } else { + Ok(self) + } } - /// Will be called before saving - fn before_save(self) -> Self { - self + /// Will be triggered after insert / update + fn after_save(self, insert: bool) -> Result { + Ok(self) } - /// Will be called after saving - fn after_save(self) -> Self { - self + /// Will be triggered before delete + fn before_delete(self) -> Result { + Ok(self) } - /// Will be called before deleting - fn before_delete(self) -> Self { - self + /// Will be triggered after delete + fn after_delete(self) -> Result { + Ok(self) } } ```