PHP Interface yii\db\ActiveRecordInterface

Since: 2.0
Author: Qiang Xue ([email protected])
Author: Carsten Brandt ([email protected])
Datei anzeigen Open project: yiisoft/yii2 Interface Usage Examples

Public Methods

Method Description
attributes ( ) : array Returns the list of all attribute names of the record.
delete ( ) : integer | boolean Deletes the record from the database.
deleteAll ( array $condition = null ) : integer Deletes records using the provided conditions.
equals ( static $record ) : boolean Returns a value indicating whether the given active record is the same as the current one.
find ( ) : yii\db\ActiveQueryInterface Creates an ActiveQueryInterface instance for query purpose.
findAll ( mixed $condition ) : array Returns a list of active record models that match the specified primary key value(s) or a set of column values.
findOne ( mixed $condition ) : static Returns a single active record model instance by a primary key or an array of column values.
getAttribute ( string $name ) : mixed Returns the named attribute value.
getDb ( ) : mixed Returns the connection used by this AR class.
getIsNewRecord ( ) : boolean Returns a value indicating whether the current record is new (not saved in the database).
getOldPrimaryKey ( boolean $asArray = false ) : mixed Returns the old primary key value(s).
getPrimaryKey ( boolean $asArray = false ) : mixed Returns the primary key value(s).
getRelation ( string $name, boolean $throwException = true ) : yii\db\ActiveQueryInterface Returns the relation object with the specified name.
hasAttribute ( string $name ) : boolean Returns a value indicating whether the record has an attribute with the specified name.
insert ( boolean $runValidation = true, array $attributes = null ) : boolean Inserts the record into the database using the attribute values of this record.
isPrimaryKey ( array $keys ) : boolean Returns a value indicating whether the given set of attributes represents the primary key for this model
link ( string $name, static $model, array $extraColumns = [] ) Establishes the relationship between two records.
populateRelation ( string $name, yii\db\ActiveRecordInterface | array | null $records ) Populates the named relation with the related records.
primaryKey ( ) : string[] Returns the primary key **name(s)** for this AR class.
save ( boolean $runValidation = true, array $attributeNames = null ) : boolean Saves the current record.
setAttribute ( string $name, mixed $value ) Sets the named attribute value.
unlink ( string $name, static $model, boolean $delete = false ) Destroys the relationship between two records.
update ( boolean $runValidation = true, array $attributeNames = null ) : integer | boolean Saves the changes to this active record into the database.
updateAll ( array $attributes, array $condition = null ) : integer Updates records using the provided attribute values and conditions.

Method Details

attributes() public method

Returns the list of all attribute names of the record.
public attributes ( ) : array
return array list of attribute names.

delete() public method

Deletes the record from the database.
public delete ( ) : integer | boolean
return integer | boolean the number of rows deleted, or `false` if the deletion is unsuccessful for some reason. Note that it is possible that the number of rows deleted is 0, even though the deletion execution is successful.

deleteAll() public static method

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: php Customer::deleteAll([status = 3]);
public static deleteAll ( array $condition = null ) : integer
$condition array the condition that matches the records that should get deleted. Please refer to [[QueryInterface::where()]] on how to specify this parameter. An empty condition will match all records.
return integer the number of rows deleted

equals() public method

Two [[getIsNewRecord()|new]] records are considered to be not equal.
public equals ( static $record ) : boolean
$record static record to compare to
return boolean whether the two active records refer to the same row in the same database table.

find() public static method

The returned ActiveQueryInterface instance can be further customized by calling methods defined in ActiveQueryInterface before one() or all() is called to return populated ActiveRecord instances. For example, php 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 [[BaseActiveRecord::hasOne()]] and [[BaseActiveRecord::hasMany()]] to create a relational query. You may override this method to return a customized query. For example, php 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: php 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 static find ( ) : yii\db\ActiveQueryInterface
return yii\db\ActiveQueryInterface the newly created [[ActiveQueryInterface]] instance.

findAll() public static method

The method accepts: - a scalar value (integer or string): query by a single primary key value and return an array containing the corresponding record (or an empty array if not found). - a non-associative array: query by a list of primary key values and return the corresponding records (or an empty array if none was found). Note that an empty condition will result in an empty result as it will be interpreted as a search for primary keys and not an empty WHERE condition. - an associative array of name-value pairs: query by a set of attribute values and return an array of records matching all of them (or an empty array if none was found). Note that ['id' => 1, 2] is treated as a non-associative array. This method will automatically call the all() method and return an array of [[ActiveRecordInterface|ActiveRecord]] instances. For example, php 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 static findAll ( mixed $condition ) : array
$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.

findOne() public static method

The method accepts: - a scalar value (integer or string): query by a single primary key value and return the corresponding record (or null if not found). - a non-associative array: query by a list of primary key values and return the first record (or null if not found). - an associative array of name-value pairs: query by a set of attribute values and return a single record matching all of them (or 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 [[ActiveRecordInterface|ActiveRecord]] instance. For example, php 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 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 static findOne ( mixed $condition ) : static
$condition mixed primary key value or a set of column values
return static ActiveRecord instance matching the condition, or `null` if nothing matches.

getAttribute() public method

If this record is the result of a query and the attribute is not loaded, null will be returned.
See also: hasAttribute()
public getAttribute ( string $name ) : mixed
$name string the attribute name
return mixed the attribute value. `null` if the attribute is not set or does not exist.

getDb() public static method

Returns the connection used by this AR class.
public static getDb ( ) : mixed
return mixed the database connection used by this AR class.

getIsNewRecord() public method

Returns a value indicating whether the current record is new (not saved in the database).
public getIsNewRecord ( ) : boolean
return boolean whether the record is new and should be inserted when calling [[save()]].

getOldPrimaryKey() public method

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 getOldPrimaryKey ( boolean $asArray = false ) : mixed
$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 `false` (default), a scalar value will be returned for non-composite primary key.
return mixed the old primary key value. An array (column name => column value) is returned if the primary key is composite or `$asArray` is true. A string is returned otherwise (`null` will be returned if the key value is `null`).

getPrimaryKey() public method

Returns the primary key value(s).
public getPrimaryKey ( boolean $asArray = false ) : mixed
$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 `$asArray` is true. A string is returned otherwise (`null` will be returned if the key value is `null`).

getRelation() public method

A relation is defined by a getter method which returns an object implementing the ActiveQueryInterface (normally this would be a relational ActiveQuery object). It can be declared in either the ActiveRecord class itself or one of its behaviors.
public getRelation ( string $name, boolean $throwException = true ) : yii\db\ActiveQueryInterface
$name string the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive).
$throwException boolean whether to throw exception if the relation does not exist.
return yii\db\ActiveQueryInterface the relational query object

hasAttribute() public method

Returns a value indicating whether the record has an attribute with the specified name.
public hasAttribute ( string $name ) : boolean
$name string the name of the attribute
return boolean whether the record has an attribute with the specified name.

insert() public method

Usage example: php $customer = new Customer; $customer->name = $name; $customer->email = $email; $customer->insert();
public insert ( boolean $runValidation = true, array $attributes = null ) : boolean
$runValidation boolean whether to perform validation (calling [[\yii\base\Model::validate()|validate()]]) before saving the record. Defaults to `true`. If the validation fails, the record will not be saved to the database and this method will return `false`.
$attributes array list of attributes that need to be saved. Defaults to `null`, meaning all attributes that are loaded from DB will be saved.
return boolean whether the attributes are valid and the record is inserted successfully.

isPrimaryKey() public static method

Returns a value indicating whether the given set of attributes represents the primary key for this model
public static isPrimaryKey ( array $keys ) : boolean
$keys array the set of attributes to check
return boolean whether the given set of attributes represents the primary key for this model

populateRelation() public method

Note that this method does not check if the relation exists or not.
Since: 2.0.8
public populateRelation ( string $name, yii\db\ActiveRecordInterface | array | null $records )
$name string the relation name, e.g. `orders` for a relation defined via `getOrders()` method (case-sensitive).
$records yii\db\ActiveRecordInterface | array | null the related records to be populated into the relation.

primaryKey() public static method

Note that an array should be returned even when the record only has a single primary key. For the primary key **value** see ActiveRecordInterface::getPrimaryKey instead.
public static primaryKey ( ) : string[]
return string[] the primary key name(s) for this AR class.

save() public method

This method will call ActiveRecordInterface::insert when [[getIsNewRecord()|isNewRecord]] is true, or ActiveRecordInterface::update when [[getIsNewRecord()|isNewRecord]] is false. For example, to save a customer record: php $customer = new Customer; // or $customer = Customer::findOne($id); $customer->name = $name; $customer->email = $email; $customer->save();
public save ( boolean $runValidation = true, array $attributeNames = null ) : boolean
$runValidation boolean whether to perform validation (calling [[\yii\base\Model::validate()|validate()]]) before saving the record. Defaults to `true`. If the validation fails, the record will not be saved to the database and this method will return `false`.
$attributeNames array list of attribute names that need to be saved. Defaults to `null`, meaning all attributes that are loaded from DB will be saved.
return boolean whether the saving succeeded (i.e. no validation errors occurred).

setAttribute() public method

Sets the named attribute value.
See also: hasAttribute()
public setAttribute ( string $name, mixed $value )
$name string the attribute name.
$value mixed the attribute value.

update() public method

Usage example: php $customer = Customer::findOne($id); $customer->name = $name; $customer->email = $email; $customer->update();
public update ( boolean $runValidation = true, array $attributeNames = null ) : integer | boolean
$runValidation boolean whether to perform validation (calling [[\yii\base\Model::validate()|validate()]]) before saving the record. Defaults to `true`. If the validation fails, the record will not be saved to the database and this method will return `false`.
$attributeNames array list of attributes that need to be saved. Defaults to `null`, meaning all attributes that are loaded from DB will be saved.
return integer | boolean the number of rows affected, or `false` if validation fails or updating process is stopped for other reasons. Note that it is possible that the number of rows affected is 0, even though the update execution is successful.

updateAll() public static method

For example, to change the status to be 1 for all customers whose status is 2: php Customer::updateAll(['status' => 1], ['status' => '2']);
public static updateAll ( array $attributes, array $condition = null ) : integer
$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 [[QueryInterface::where()]] on how to specify this parameter. An empty condition will match all records.
return integer the number of rows updated