php activerecord,ActiveRecord, yii\db\ActiveRecord

章锦
2023-12-01

attributes()

公共 方法

返回模型的所有的属性名称的列表。

默认实现将返回与此 AR 类关联的表的所有列名。

return

属性名称列表。

delete()

公共 方法

删除与此活动记录对应的表行。

此方法将按顺序执行以下步骤:

调用 beforeDelete()。

如果该方法返回 false,则跳过其余步骤;

从数据库删除记录;

return

删除的行数,如果由于某种原因删除失败,则为 false。

注意,即使删除执行成功,删除的行数也可能为 0。

throws

\Exception|\Throwable

万一删除失败,则抛出异常。

deleteAll()

公共 静态 方法

使用提供的条件删除表中的行。

例如,要删除所有状态为 3 的客户:

Customer::deleteAll('status = 3');

警告:如果未指定任何条件,则此方法将删除表中的所有行。

注意,此方法不会触发任何事件。如果需要触发 EVENT_BEFORE_DELETE 或 EVENT_AFTER_DELETE,

则首先需要 find 模型,然后再每个模型上调用 delete()。

例如,下面的例子和前面的例子作用是相同的:

$models = Customer::find()->where('status = 3')->all();

foreach ($models as $model) {

$model->delete();

}

对于大量模型,可以考虑使用 yii\db\ActiveQuery::each() 将内存使用限制在规定范围内。

public static $condition=null, $params= [])

$condition

将放在 DELETE SQL 的 WHERE 部分中的条件。

有关如何指定此参数,请阅读 yii\db\Query::where()。

$params

要绑定到查询的参数 (name => value)。

return

删除的行数

deleteInternal()

受保护 方法

删除 ActiveRecord 而不考虑事务。

return

删除的行数,如果由于某种原因删除失败,则为 false。

注意,即使删除执行成功,删除的行数也可能为 0。

equals()

公共 方法

返回一个给定值,指示给定的活动记录是否与当前记录相同。

通过比较两个活动记录的表名和主键值来进行比较。

如果其中一个记录 is new 也会被认为不相同。

public $record)

return

两个活动记录是否引用同一数据库表中的同一行。

filterCondition()

受保护 静态 方法

(自版本 2.0.15 可用)

在将数组条件分配给查询过滤器之前对其进行过滤。

此方法将确保数组条件仅过滤现有表列。

protected static $condition)

$condition

过滤条件。

return

过滤后的条件。

find()

公共 静态 方法

返回的 yii\db\ActiveQueryInterface 实例可以通过调用 one() 或 all() 之前调用

yii\db\ActiveQueryInterface 方法来进一步自定义,

以返回填充的 ActiveRecord 实例。例如,

// 找到 ID 为 1 的客户

$customer = Customer::find()->where(['id' => 1])->one();

// 查找所有活跃客户并按其年龄排序

$customers = Customer::find()

->where(['status' => 1])

->orderBy('age')

->all();

你可以覆盖此方法以返回自定义查询。例如,

class Customer extends ActiveRecord{

public static function find(){

// use CustomerQuery instead of the default ActiveQuery

return new CustomerQuery(get_called_class());

}

}

以下代码显示如何为所有查询应用默认条件:

class Customer extends ActiveRecord{

public static function find(){

return parent::find()->where(['deleted' => false]);

}

}

// 使用 andWhere()/orWhere() 应用默认条件

// SELECT FROM customer WHERE `deleted`=:deleted AND age>30

$customers = Customer::find()->andWhere('age>30')->all();

// 使用 where() 忽略默认条件

// SELECT FROM customer WHERE age>30

$customers = Customer::find()->where('age>30')->all();

findByCondition()

受保护 静态 方法

按给定的条件查找 ActiveRecord 实例。

此方法由 findOne() 和 findAll() 在内部调用。

findBySql()

公共 静态 方法

请注意,因为已经指定了 SQL 语句,

所以在创建的 yii\db\ActiveQuery 实例上调用其他查询修改方法(例如 where(),order()),

将不起作用,

但是,调用 with(),asArray() 或 indexBy() 仍然没问题。

下面举个例子:

$customers = Customer::findBySql('SELECT * FROM customer')->all();

public static $sql, $params= [])

$sql

要执行的 SQL 语句

$params

在执行期间绑定到 SQL 语句的参数。

getDb()

公共 静态 方法

返回此 AR 类使用的数据库连接。

默认情况下,"db" 组件用作数据库连接。

如果要使用其他数据库连接,可以重写此方法。

getTableSchema()

公共 静态 方法

返回与此 AR 类关联的 DB 表的结构信息。

insert()

公共 方法

使用此属性的记录值将行插入关联的数据库表中。

此方法按顺序执行以下步骤:

当 $runValidation 为 true 时调用 beforeValidate()。

如果 beforeValidate() 返回 false,则跳过其余步骤;

当 $runValidation 为 true 时调用 afterValidate()。

如果验证失败,则跳过其余步骤;

插入记录到数据库。如果插入记录失败,则跳过其余步骤;

如果表的主键是自动增量并且在插入期间为 null,

则插入后将填充实际值。

例如,要插入 customer 记录:

$customer = new Customer;

$customer->name = $name;

$customer->email = $email;

$customer->insert();

public $runValidation=true, $attributes=null)

$runValidation

是否在保存记录之前执行验证(调用 validate())。

默认为 true,即执行验证。如果验证失败,则记录不会保存到数据库中,

并且此方法将返回 false。

$attributes

需要保存的属性列表。

默认为 null,表示将保存从 DB 加载的所有属性。

return

属性是否有效,以及是否成功插入记录。

throws

\Exception|\Throwable

如果插入失败,则抛出异常。

insertInternal()

受保护 方法

在不考虑事务的情况下将 ActiveRecord 插入到 DB 中。

protected $attributes=null)

$attributes

需要保存的属性列表。

默认为 null,表示将保存从 DB 加载的所有属性。

return

是否成功插入记录。

isTransactional()

公共 方法

返回一个值,该值表示指定的操作在当前 $scenario 中是否为事务性操作。

loadDefaultValues()

公共 方法

从数据库表结构加载默认值。

你可以在创建新实例后调用此方法以加载默认值:

// class Customer extends \yii\db\ActiveRecord

$customer = new Customer();

$customer->loadDefaultValues();

public $skipIfSet=true)

$skipIfSet

是否应保留现有值。

这只会为 null 属性设置默认值。

return

模型实例本身。

populateRecord()

公共 静态 方法

Populates an active record object using a row of data from the database/storage.

This is an internal method meant to be called to create active record objects after

fetching data from the database. It is mainly used by yii\db\ActiveQuery to populate

the query results into active records.

When calling this method manually you should call afterFind() on the created

record to trigger the afterFind Event.

public static void $record, $row)

$record

The record to be populated. In most cases this will be an instance

created by instantiate() beforehand.

$row

Attribute values (name => value)

primaryKey()

公共 静态 方法

返回此 AR 类的主键名称。

默认实现将返回与此 AR 类

关联的 DB 表中声明的主键。

如果 DB 表中为声明任何主键,

则应重写此方法,

以返回要用作此 AR 类的主键属性。

请注意,即使对于具有单个主键的表,也应返回一个数组。

public static

return

相关数据表的主键。

refresh()

公共 方法

Repopulates this active record with the latest data.

If the refresh is successful, an EVENT_AFTER_REFRESH event will be triggered.

This event is available since version 2.0.8.

return

Whether the row still exists in the database. If true, the latest data

will be populated to this active record. Otherwise, this record will remain unchanged.

tableName()

公共 静态 方法

声明与此 AR 类关联的数据库表的名称。

默认情况下,此方法通过使用前缀 yii\db\Connection::$tablePrefix 调用 yii\helpers\Inflector::camel2id() 来返回类名作为表名。

例如,如果 yii\db\Connection::$tablePrefix 是 tbl_,

则 Customer 变为 tbl_customer,OrderItem 变为 tbl_order_item

如果未按约定命名表,则可以重写此方法。

public static

return

表名

transactions()

公共 方法

声明应在不同场景的事务中执行那些 DB 操作。

支持的 DB 操作为 OP_INSERT,OP_UPDATE 以及 OP_DELETE,

分别对应 insert(),update() 以及 delete() 方法,

默认情况下,这些方法不包含在数据库事务中。

在某些情况下,为保持数据一致性,

你可能希望将其中的部分或全部包含在事务中。

你可以通过重写此方法并返回需要进行事务处理的操作来完成此操作。例如,

return [

'admin' => self::OP_INSERT,

'api' => self::OP_INSERT | self::OP_UPDATE | self::OP_DELETE,

// the above is equivalent to the following:

// 'api' => self::OP_ALL,

];

上述声明指定在 "admin" 场景中,

插入操作 (insert()) 应该在事务中完成;

在 "api" 场景中,所有的操作都应该在事务中完成。

return

事务操作声明。数组的键是场景名称,

数组值是相应的事务操作。

update()

公共 方法

将对此活动记录的更改保存到关联的数据库表中。

此方法将按顺序执行一下步骤:

当 $runValidation 为 true 时,调用 beforeValidate()。

如果 beforeValidate() 返回 false,则跳过其余步骤;

当 $runValidation 为 true 时,调用 afterValidate()。

如果验证失败,则跳过其余步骤;

保存记录到数据中。如果保存失败,则跳过余下步骤;

例如,要更新 customer 记录:

$customer = Customer::findOne($id);

$customer->name = $name;

$customer->email = $email;

$customer->update();

注意,更新可能不会影响表中的任何行。

在该情况下,此方法有可能返回 0。

因此,应使用以下代码检查 update() 是否成功:

if ($customer->update() !== false) {

// update successful

} else {

// update failed

}

public $runValidation=true, $attributeNames=null)

$runValidation

是否在保存记录之前执行验证 (调用 validate())。

默认为 true,即执行验证。如果验证失败,则记录不会保存到数据库中,

并且此方法将返回 false。

$attributeNames

需要保存的属性列表。

默认为 null,表示将保存从 DB 加载的所有属性。

return

受影响的行数,如果验证失败或 beforeSave() 停止更新过程,

则为 false。

throws

\Exception|\Throwable

万一更新失败,则抛出异常。

updateAll()

公共 静态 方法

使用提供的属性值和条件更新整个表。

比如,要将所有状态为 2 的客户的状态更改为 1:

Customer::updateAll(['status' => 1], 'status = 2');

警告:如果未指定任何条件,则此方法将更新表中的所有行。

注意,此方法不会触发任何事件,如果需要触发 EVENT_BEFORE_UPDATE 或 EVENT_AFTER_UPDATE ,

则首先需要 find 模型,然后在每个模型上调用 update()。

例如,下面的例子和前面的例子作用是相同的:

$models = Customer::find()->where('status = 2')->all();

foreach ($models as $model) {

$model->status = 1;

$model->update(false); // skipping validation as no user input is involved

}

对于大量模型,可以考虑使用 yii\db\ActiveQuery::each() 将内存使用限制在规定范围内。

public static $attributes, $condition='', $params= [])

$attributes

要保存在表中的属性值(键值对)

$condition

将放在 UPDATE SQL 的 WHERE 部分中的条件。

有关如何指定此参数,请阅读 yii\db\Query::where()。

$params

要绑定到查询的参数 (name => value)。

return

更新的行数

updateAllCounters()

公共 静态 方法

使用提供的计数器更改条件更新整个表。

例如,要将所有客户的年龄增加 1,

Customer::updateAllCounters(['age' => 1]);

注意,此方法不会触发任何事件。

public static $counters, $condition='', $params= [])

$counters

要更新的计数器 (attribute name => increment value)。

如果要递减计数器,请使用负值。

$condition

将放在 UPDATE SQL 的 WHERE 部分中的条件。

有关如何指定此参数,请阅读 yii\db\Query::where()。

$params

要绑定到查询的参数 (name => value)。

不要将参数命名为 :bp0,:bp1 等,因为它们是由这个方法内部使用的。

return

更新的行数

 类似资料: