Implemented by | yii\db\ActiveRecord, yii\db\BaseActiveRecord, yii\elasticsearch\ActiveRecord, yii\mongodb\ActiveRecord, yii\mongodb\file\ActiveRecord, yii\redis\ActiveRecord, yii\sphinx\ActiveRecord |
---|---|
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/db/ActiveRecordInterface.php |
ActiveRecordInterface
Method | Description | Defined By |
---|---|---|
attributes() | Returns the list of all attribute names of the record. | yii\db\ActiveRecordInterface |
delete() | Deletes the record from the database. | yii\db\ActiveRecordInterface |
deleteAll() | Deletes records using the provided conditions. | yii\db\ActiveRecordInterface |
equals() | Returns a value indicating whether the given active record is the same as the current one. | yii\db\ActiveRecordInterface |
find() | Creates an yii\db\ActiveQueryInterface instance for query purpose. | yii\db\ActiveRecordInterface |
findAll() | Returns a list of active record models that match the specified primary key value(s) or a set of column values. | yii\db\ActiveRecordInterface |
findOne() | Returns a single active record model instance by a primary key or an array of column values. | yii\db\ActiveRecordInterface |
getAttribute() | Returns the named attribute value. | yii\db\ActiveRecordInterface |
getDb() | Returns the connection used by this AR class. | yii\db\ActiveRecordInterface |
getIsNewRecord() | Returns a value indicating whether the current record is new (not saved in the database). | yii\db\ActiveRecordInterface |
getOldPrimaryKey() | Returns the old primary key value(s). | yii\db\ActiveRecordInterface |
getPrimaryKey() | Returns the primary key value(s). | yii\db\ActiveRecordInterface |
getRelation() | Returns the relation object with the specified name. | yii\db\ActiveRecordInterface |
hasAttribute() | Returns a value indicating whether the record has an attribute with the specified name. | yii\db\ActiveRecordInterface |
insert() | Inserts the record into the database using the attribute values of this record. | yii\db\ActiveRecordInterface |
isPrimaryKey() | Returns a value indicating whether the given set of attributes represents the primary key for this model | yii\db\ActiveRecordInterface |
link() | Establishes the relationship between two records. | yii\db\ActiveRecordInterface |
populateRelation() | Populates the named relation with the related records. | yii\db\ActiveRecordInterface |
primaryKey() | Returns the primary key name(s) for this AR class. | yii\db\ActiveRecordInterface |
save() | Saves the current record. | yii\db\ActiveRecordInterface |
setAttribute() | Sets the named attribute value. | yii\db\ActiveRecordInterface |
unlink() | Destroys the relationship between two records. | yii\db\ActiveRecordInterface |
update() | Saves the changes to this active record into the database. | yii\db\ActiveRecordInterface |
updateAll() | Updates records using the provided attribute values and conditions. | yii\db\ActiveRecordInterface |
Returns the list of all attribute names of the record.
public abstract array attributes ( ) | ||
---|---|---|
return | array |
List of attribute names. |
Deletes the record from the database.
public abstract integer|boolean delete ( ) | ||
---|---|---|
return | integer|boolean |
The number of rows deleted, or |
Deletes records using the provided conditions.
WARNING: If you do not specify any condition, this method will delete ALL rows in the table.
For example, to delete all customers whose status is 3:
Customer::deleteAll([status = 3]);
public abstract static integer deleteAll ( $condition = null ) | ||
---|---|---|
$condition | array |
The condition that matches the records that should get deleted. Please refer to yii\db\QueryInterface::where() on how to specify this parameter. An empty condition will match all records. |
return | integer |
The number of rows deleted |
Returns a value indicating whether the given active record is the same as the current one.
Two new records are considered to be not equal.
public abstract boolean equals ( $record ) | ||
---|---|---|
$record | static |
Record to compare to |
return | boolean |
Whether the two active records refer to the same row in the same database table. |
Creates an yii\db\ActiveQueryInterface instance for query purpose.
The returned yii\db\ActiveQueryInterface instance can be further customized by calling methods defined in yii\db\ActiveQueryInterface before one()
or all()
is called to return populated ActiveRecord instances. For example,
// find the customer whose ID is 1 $customer = Customer::find()->where(['id' => 1])->one(); // find all active customers and order them by their age: $customers = Customer::find() ->where(['status' => 1]) ->orderBy('age') ->all();
This method is also called by yii\db\BaseActiveRecord::hasOne() and yii\db\BaseActiveRecord::hasMany() to create a relational query.
You may override this method to return a customized query. For example,
class Customer extends ActiveRecord { public static function find() { // use CustomerQuery instead of the default ActiveQuery return new CustomerQuery(get_called_class()); } }
The following code shows how to apply a default condition for all queries:
class Customer extends ActiveRecord { public static function find() { return parent::find()->where(['deleted' => false]); } } // Use andWhere()/orWhere() to apply the default condition // SELECT FROM customer WHERE `deleted`=:deleted AND age>30 $customers = Customer::find()->andWhere('age>30')->all(); // Use where() to ignore the default condition // SELECT FROM customer WHERE age>30 $customers = Customer::find()->where('age>30')->all();
public abstract static yii\db\ActiveQueryInterface find ( ) | ||
---|---|---|
return | yii\db\ActiveQueryInterface |
The newly created yii\db\ActiveQueryInterface instance. |
Returns a list of active record models that match the specified primary key value(s) or a set of column values.
The method accepts:
WHERE
condition.['id' => 1, 2]
is treated as a non-associative array.This method will automatically call the all()
method and return an array of ActiveRecord instances.
Note: As this is a short-hand method only, using more complex conditions, like ['!=', 'id', 1] will not work. If you need to specify more complex conditions, use find() in combination with where() instead.
See the following code for usage examples:
// find the customers whose primary key value is 10 $customers = Customer::findAll(10); // the above code is equivalent to: $customers = Customer::find()->where(['id' => 10])->all(); // find the customers whose primary key value is 10, 11 or 12. $customers = Customer::findAll([10, 11, 12]); // the above code is equivalent to: $customers = Customer::find()->where(['id' => [10, 11, 12]])->all(); // find customers whose age is 30 and whose status is 1 $customers = Customer::findAll(['age' => 30, 'status' => 1]); // the above code is equivalent to: $customers = Customer::find()->where(['age' => 30, 'status' => 1])->all();
public abstract static array findAll ( $condition ) | ||
---|---|---|
$condition | mixed |
Primary key value or a set of column values |
return | array |
An array of ActiveRecord instance, or an empty array if nothing matches. |
Returns a single active record model instance by a primary key or an array of column values.
The method accepts:
null
if not found).null
if not found).null
if not found). Note that ['id' => 1, 2]
is treated as a non-associative array.That this method will automatically call the one()
method and return an ActiveRecord instance.
Note: As this is a short-hand method only, using more complex conditions, like ['!=', 'id', 1] will not work. If you need to specify more complex conditions, use find() in combination with where() instead.
See the following code for usage examples:
// find a single customer whose primary key value is 10 $customer = Customer::findOne(10); // the above code is equivalent to: $customer = Customer::find()->where(['id' => 10])->one(); // find the customers whose primary key value is 10, 11 or 12. $customers = Customer::findOne([10, 11, 12]); // the above code is equivalent to: $customers = Customer::find()->where(['id' => [10, 11, 12]])->one(); // find the first customer whose age is 30 and whose status is 1 $customer = Customer::findOne(['age' => 30, 'status' => 1]); // the above code is equivalent to: $customer = Customer::find()->where(['age' => 30, 'status' => 1])->one();
public abstract static static findOne ( $condition ) | ||
---|---|---|
$condition | mixed |
Primary key value or a set of column values |
return | static |
ActiveRecord instance matching the condition, or |
Returns the named attribute value.
If this record is the result of a query and the attribute is not loaded, null
will be returned.
See also hasAttribute().
public abstract mixed getAttribute ( $name ) | ||
---|---|---|
$name | string |
The attribute name |
return | mixed |
The attribute value. |
Returns the connection used by this AR class.
public abstract static mixed getDb ( ) | ||
---|---|---|
return | mixed |
The database connection used by this AR class. |
Returns a value indicating whether the current record is new (not saved in the database).
public abstract boolean getIsNewRecord ( ) | ||
---|---|---|
return | boolean |
Whether the record is new and should be inserted when calling save(). |
Returns the old primary key value(s).
This refers to the primary key value that is populated into the record after executing a find method (e.g. find(), findOne()). The value remains unchanged even if the primary key attribute is manually assigned with a different value.
public abstract mixed getOldPrimaryKey ( $asArray = false ) | ||
---|---|---|
$asArray | boolean |
Whether to return the primary key value as an array. If true, the return value will be an array with column name as key and column value as value. If this is |
return | mixed |
The old primary key value. An array (column name => column value) is returned if the primary key is composite or |
Returns the primary key value(s).
public abstract mixed getPrimaryKey ( $asArray = false ) | ||
---|---|---|
$asArray | boolean |
Whether to return the primary key value as an array. If true, the return value will be an array with attribute names as keys and attribute values as values. Note that for composite primary keys, an array will always be returned regardless of this parameter value. |
return | mixed |
The primary key value. An array (attribute name => attribute value) is returned if the primary key is composite or |
Returns the relation object with the specified name.
A relation is defined by a getter method which returns an object implementing the yii\db\ActiveQueryInterface (normally this would be a relational yii\db\ActiveQuery object). It can be declared in either the ActiveRecord class itself or one of its behaviors.
public abstract yii\db\ActiveQueryInterface getRelation ( $name, $throwException = true ) | ||
---|---|---|
$name | string |
The relation name, e.g. |
$throwException | boolean |
Whether to throw exception if the relation does not exist. |
return | yii\db\ActiveQueryInterface |
The relational query object |
Returns a value indicating whether the record has an attribute with the specified name.
public abstract boolean hasAttribute ( $name ) | ||
---|---|---|
$name | string |
The name of the attribute |
return | boolean |
Whether the record has an attribute with the specified name. |
Inserts the record into the database using the attribute values of this record.
Usage example:
$customer = new Customer; $customer->name = $name; $customer->email = $email; $customer->insert();
public abstract boolean insert ( $runValidation = true, $attributes = null ) | ||
---|---|---|
$runValidation | boolean |
Whether to perform validation (calling validate()) before saving the record. Defaults to |
$attributes | array |
List of attributes that need to be saved. Defaults to |
return | boolean |
Whether the attributes are valid and the record is inserted successfully. |
Returns a value indicating whether the given set of attributes represents the primary key for this model
public abstract static boolean isPrimaryKey ( $keys ) | ||
---|---|---|
$keys | array |
The set of attributes to check |
return | boolean |
Whether the given set of attributes represents the primary key for this model |
Establishes the relationship between two records.
The relationship is established by setting the foreign key value(s) in one record to be the corresponding primary key value(s) in the other record. The record with the foreign key will be saved into database without performing validation.
If the relationship involves a junction table, a new row will be inserted into the junction table which contains the primary key values from both records.
This method requires that the primary key value is not null
.
public abstract void link ( $name, $model, $extraColumns = [] ) | ||
---|---|---|
$name | string |
The case sensitive name of the relationship, e.g. |
$model | static |
The record to be linked with the current one. |
$extraColumns | array |
Additional column values to be saved into the junction table. This parameter is only meaningful for a relationship involving a junction table (i.e., a relation set with yii\db\ActiveQueryInterface::via()). |
Populates the named relation with the related records.
Note that this method does not check if the relation exists or not.
public abstract void populateRelation ( $name, $records ) | ||
---|---|---|
$name | string |
The relation name, e.g. |
$records | yii\db\ActiveRecordInterface|array|null |
The related records to be populated into the relation. |
Returns the primary key name(s) for this AR class.
Note that an array should be returned even when the record only has a single primary key.
For the primary key value see getPrimaryKey() instead.
public abstract static string[] primaryKey ( ) | ||
---|---|---|
return | string[] |
The primary key name(s) for this AR class. |
Saves the current record.
This method will call insert() when isNewRecord is true, or update() when isNewRecord is false.
For example, to save a customer record:
$customer = new Customer; // or $customer = Customer::findOne($id); $customer->name = $name; $customer->email = $email; $customer->save();
public abstract boolean save ( $runValidation = true, $attributeNames = null ) | ||
---|---|---|
$runValidation | boolean |
Whether to perform validation (calling validate()) before saving the record. Defaults to |
$attributeNames | array |
List of attribute names that need to be saved. Defaults to |
return | boolean |
Whether the saving succeeded (i.e. no validation errors occurred). |
Sets the named attribute value.
See also hasAttribute().
public abstract void setAttribute ( $name, $value ) | ||
---|---|---|
$name | string |
The attribute name. |
$value | mixed |
The attribute value. |
Destroys the relationship between two records.
The record with the foreign key of the relationship will be deleted if $delete
is true. Otherwise, the foreign key will be set null
and the record will be saved without validation.
public abstract void unlink ( $name, $model, $delete = false ) | ||
---|---|---|
$name | string |
The case sensitive name of the relationship, e.g. |
$model | static |
The model to be unlinked from the current one. |
$delete | boolean |
Whether to delete the model that contains the foreign key. If false, the model's foreign key will be set |
Saves the changes to this active record into the database.
Usage example:
$customer = Customer::findOne($id); $customer->name = $name; $customer->email = $email; $customer->update();
public abstract integer|boolean update ( $runValidation = true, $attributeNames = null ) | ||
---|---|---|
$runValidation | boolean |
Whether to perform validation (calling validate()) before saving the record. Defaults to |
$attributeNames | array |
List of attributes that need to be saved. Defaults to |
return | integer|boolean |
The number of rows affected, or |
Updates records using the provided attribute values and conditions.
For example, to change the status to be 1 for all customers whose status is 2:
Customer::updateAll(['status' => 1], ['status' => '2']);
public abstract static integer updateAll ( $attributes, $condition = null ) | ||
---|---|---|
$attributes | array |
Attribute values (name-value pairs) to be saved for the record. Unlike update() these are not going to be validated. |
$condition | array |
The condition that matches the records that should get updated. Please refer to yii\db\QueryInterface::where() on how to specify this parameter. An empty condition will match all records. |
return | integer |
The number of rows updated |
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-db-activerecordinterface.html