PHP 인터페이스 yii\db\ActiveRecordInterface

부터: 2.0
저자: Qiang Xue ([email protected])
저자: Carsten Brandt ([email protected])
파일 보기 프로젝트 열기: yiisoft/yii2 0 사용 예제들

공개 메소드들

메소드 설명
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.

메소드 상세

attributes() 공개 메소드

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

delete() 공개 메소드

Deletes the record from the database.
public delete ( ) : integer | boolean
리턴 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() 공개 정적인 메소드

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.
리턴 integer the number of rows deleted

equals() 공개 메소드

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

find() 공개 정적인 메소드

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
리턴 yii\db\ActiveQueryInterface the newly created [[ActiveQueryInterface]] instance.

findAll() 공개 정적인 메소드

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
리턴 array an array of ActiveRecord instance, or an empty array if nothing matches.

findOne() 공개 정적인 메소드

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
리턴 static ActiveRecord instance matching the condition, or `null` if nothing matches.

getAttribute() 공개 메소드

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

getDb() 공개 정적인 메소드

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

getIsNewRecord() 공개 메소드

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

getOldPrimaryKey() 공개 메소드

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.
리턴 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() 공개 메소드

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.
리턴 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() 공개 메소드

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.
리턴 yii\db\ActiveQueryInterface the relational query object

hasAttribute() 공개 메소드

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
리턴 boolean whether the record has an attribute with the specified name.

insert() 공개 메소드

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.
리턴 boolean whether the attributes are valid and the record is inserted successfully.

isPrimaryKey() 공개 정적인 메소드

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
리턴 boolean whether the given set of attributes represents the primary key for this model

populateRelation() 공개 메소드

Note that this method does not check if the relation exists or not.
부터: 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() 공개 정적인 메소드

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[]
리턴 string[] the primary key name(s) for this AR class.

save() 공개 메소드

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.
리턴 boolean whether the saving succeeded (i.e. no validation errors occurred).

setAttribute() 공개 메소드

Sets the named attribute value.
또한 보기: hasAttribute()
public setAttribute ( string $name, mixed $value )
$name string the attribute name.
$value mixed the attribute value.

update() 공개 메소드

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.
리턴 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() 공개 정적인 메소드

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.
리턴 integer the number of rows updated