pub struct ActiveModel<M: Model> { /* private fields */ }Expand description
通用 ActiveModel 包装器
为任意 Model 类型提供 dirty tracking:
- 从
Model创建时,所有字段初始为Unchanged - 从
Default创建时(新建记录),所有字段初始为NotSet
业务模型通过 set() 方法标记变更字段,
然后传给 update / save 执行持久化。
§示例
ⓘ
let user = User::find_by_id(1, &mut conn).await?;
let mut active = ActiveModel::from_model(user);
active.set("email", "new@example.com".into()); // ActiveValue::Set
update(&mut conn, active).await?;Implementations§
Source§impl<M: Model> ActiveModel<M>
impl<M: Model> ActiveModel<M>
Sourcepub fn from_model(model: M) -> Self
pub fn from_model(model: M) -> Self
从已有模型创建 ActiveModel(所有字段初始为 Unchanged)
适用于“加载 → 修改部分字段 → 更新“的工作流。
Sourcepub fn changed_fields(&self) -> Vec<(&str, &Value)>
pub fn changed_fields(&self) -> Vec<(&str, &Value)>
获取所有变更字段列表(仅 Set 状态)
Sourcepub fn as_mut_model(&mut self) -> &mut M
pub fn as_mut_model(&mut self) -> &mut M
获取底层模型的可变引用
Sourcepub fn into_model(self) -> M
pub fn into_model(self) -> M
消耗包装器,返回底层模型
Trait Implementations§
Source§impl<M: Model> ActiveModelTrait for ActiveModel<M>
impl<M: Model> ActiveModelTrait for ActiveModel<M>
Auto Trait Implementations§
impl<M> Freeze for ActiveModel<M>where
M: Freeze,
impl<M> RefUnwindSafe for ActiveModel<M>where
M: RefUnwindSafe,
impl<M> Send for ActiveModel<M>
impl<M> Sync for ActiveModel<M>
impl<M> Unpin for ActiveModel<M>where
M: Unpin,
impl<M> UnsafeUnpin for ActiveModel<M>where
M: UnsafeUnpin,
impl<M> UnwindSafe for ActiveModel<M>where
M: UnwindSafe,
Blanket Implementations§
Source§impl<A> ActiveModelExt for Awhere
A: ActiveModelTrait,
impl<A> ActiveModelExt for Awhere
A: ActiveModelTrait,
Source§fn pk_name_for_update(&self) -> &str
fn pk_name_for_update(&self) -> &str
获取主键列名(用于 UPDATE 的 WHERE 条件)
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more