Skip to main content

ActiveModel

Struct ActiveModel 

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

Source

pub fn from_model(model: M) -> Self

从已有模型创建 ActiveModel(所有字段初始为 Unchanged

适用于“加载 → 修改部分字段 → 更新“的工作流。

Source

pub fn set(&mut self, field: impl Into<String>, value: ActiveValue<Value>)

设置一个字段的值(标记为 Set

§示例
active.set("name", ActiveValue::Set("Alice".into()));
// 或简写(利用 From 自动转换):
active.set("name", "Alice".into());
Source

pub fn unset(&mut self, field: &str)

将字段标记为 Unchanged(从变更集中移除)

Source

pub fn get(&self, field: &str) -> Option<&ActiveValue<Value>>

获取某个字段的当前状态

Source

pub fn changed_fields(&self) -> Vec<(&str, &Value)>

获取所有变更字段列表(仅 Set 状态)

Source

pub fn as_mut_model(&mut self) -> &mut M

获取底层模型的可变引用

Source

pub fn as_model(&self) -> &M

获取底层模型的不可变引用

Source

pub fn into_model(self) -> M

消耗包装器,返回底层模型

Trait Implementations§

Source§

impl<M: Model> ActiveModelTrait for ActiveModel<M>
where M::PrimaryKey: Into<Value>,

Source§

fn table_name(&self) -> &str

获取表名
Source§

fn pk_value(&self) -> Option<Value>

获取主键值(用于 WHERE 条件)
Source§

fn for_each_changed<F>(&self, f: F)
where F: FnMut(&str, &ActiveValue<Value>),

遍历所有已设置(Set)的字段 Read more
Source§

impl<M: Clone + Model> Clone for ActiveModel<M>

Source§

fn clone(&self) -> ActiveModel<M>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<M: Debug + Model> Debug for ActiveModel<M>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

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 A

Source§

fn pk_name_for_update(&self) -> &str

获取主键列名(用于 UPDATE 的 WHERE 条件)
Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

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

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more