0 follower

Interface Yiisoft\ActiveRecord\ActiveRecordInterface

Implemented byYiisoft\ActiveRecord\AbstractActiveRecord, Yiisoft\ActiveRecord\ActiveRecord

Public Methods

Hide inherited methods

Method Description Defined By
assignOldValue() Sets the old value of the named property. Yiisoft\ActiveRecord\ActiveRecordInterface
assignOldValues() Sets the old property values. Yiisoft\ActiveRecord\ActiveRecordInterface
column() Yiisoft\ActiveRecord\ActiveRecordInterface
createQuery() Create Yiisoft\ActiveRecord\ActiveQueryInterface instance for the given model class. Yiisoft\ActiveRecord\ActiveRecordInterface
db() Returns the database connection used by the Active Record instance. Yiisoft\ActiveRecord\ActiveRecordInterface
delete() Deletes the table row corresponding to this active record. Yiisoft\ActiveRecord\ActiveRecordInterface
deleteAll() Deletes rows in the table using the provided conditions. Yiisoft\ActiveRecord\ActiveRecordInterface
equals() Returns a value indicating whether the given active record is the same as the current one. Yiisoft\ActiveRecord\ActiveRecordInterface
get() Returns the named property value. Yiisoft\ActiveRecord\ActiveRecordInterface
hasMany() Declares a has-many relation. Yiisoft\ActiveRecord\ActiveRecordInterface
hasOne() Declares a has-one relation. Yiisoft\ActiveRecord\ActiveRecordInterface
hasProperty() Returns a value indicating whether the record has a property with the specified name. Yiisoft\ActiveRecord\ActiveRecordInterface
insert() Inserts a row into the associated database table using the property values of this record. Yiisoft\ActiveRecord\ActiveRecordInterface
isChanged() Checks if any property returned by Yiisoft\ActiveRecord\ActiveRecordInterface::propertyNames() method has changed. Yiisoft\ActiveRecord\ActiveRecordInterface
isNew() Returns a value indicating whether the current record is new (not saved in the database). Yiisoft\ActiveRecord\ActiveRecordInterface
isPrimaryKey() Returns a value indicating whether the given set of property names represents the primary key for this active record. Yiisoft\ActiveRecord\ActiveRecordInterface
isPropertyChanged() Returns whether the named property has been changed using the not identical operator !==. Yiisoft\ActiveRecord\ActiveRecordInterface
isPropertyChangedNonStrict() Returns whether the named property has been changed using the not equal operator !=. Yiisoft\ActiveRecord\ActiveRecordInterface
isRelationPopulated() Check whether the named relation has been populated with records. Yiisoft\ActiveRecord\ActiveRecordInterface
link() Establishes the relationship between two records. Yiisoft\ActiveRecord\ActiveRecordInterface
loadDefaultValues() Loads default values from database table schema. Yiisoft\ActiveRecord\ActiveRecordInterface
markAsExisting() Marks this record as existing. The record should be updated when calling Yiisoft\ActiveRecord\ActiveRecordInterface::save(). Yiisoft\ActiveRecord\ActiveRecordInterface
markAsNew() Marks this record as new. The record should be inserted when calling Yiisoft\ActiveRecord\ActiveRecordInterface::save(). Yiisoft\ActiveRecord\ActiveRecordInterface
markPropertyChanged() Marks a property as changed. Yiisoft\ActiveRecord\ActiveRecordInterface
newValues() Returns the property values that have been modified since they're loaded or saved most recently. Yiisoft\ActiveRecord\ActiveRecordInterface
oldValue() Returns the old value of the named property. Yiisoft\ActiveRecord\ActiveRecordInterface
oldValues() Returns the old property values. Yiisoft\ActiveRecord\ActiveRecordInterface
populateProperties() Sets the property values in a massive way. Yiisoft\ActiveRecord\ActiveRecordInterface
populateRecord() Populates an active record object using a row of data from the database/storage. Yiisoft\ActiveRecord\ActiveRecordInterface
populateRelation() Populates the named relation with the related records. Yiisoft\ActiveRecord\ActiveRecordInterface
primaryKey() Returns the primary key names for this AR class. Yiisoft\ActiveRecord\ActiveRecordInterface
primaryKeyOldValue() Returns the old value of the primary key as a scalar. Yiisoft\ActiveRecord\ActiveRecordInterface
primaryKeyOldValues() Returns the old values of the primary key as an array with property names as keys and property values as values. Yiisoft\ActiveRecord\ActiveRecordInterface
primaryKeyValue() Returns the scalar value of the primary key. Yiisoft\ActiveRecord\ActiveRecordInterface
primaryKeyValues() Returns the values of the primary key as an array with property names as keys and property values as values. Yiisoft\ActiveRecord\ActiveRecordInterface
propertyNames() Returns the list of property names mapped to column names of the table associated with this active record class. Yiisoft\ActiveRecord\ActiveRecordInterface
propertyValues() Returns property values. Yiisoft\ActiveRecord\ActiveRecordInterface
query() Create Yiisoft\ActiveRecord\ActiveQueryInterface instance for the given model class. Yiisoft\ActiveRecord\ActiveRecordInterface
relatedRecords() Returns all populated related records. Yiisoft\ActiveRecord\ActiveRecordInterface
relation() Returns the relation object with the specified name. Yiisoft\ActiveRecord\ActiveRecordInterface
relationQuery() Returns the relation query object with the specified name. Yiisoft\ActiveRecord\ActiveRecordInterface
resetRelation() Resets relation data for the specified name. Yiisoft\ActiveRecord\ActiveRecordInterface
save() Saves the changes to this active record into the associated database table. Yiisoft\ActiveRecord\ActiveRecordInterface
set() Sets the named property value. Yiisoft\ActiveRecord\ActiveRecordInterface
tableName() Return the name of the table associated with this AR class. Yiisoft\ActiveRecord\ActiveRecordInterface
tableSchema() Returns the schema information of the DB table associated with this AR class. Yiisoft\ActiveRecord\ActiveRecordInterface
unlink() Destroys the relationship between two records. Yiisoft\ActiveRecord\ActiveRecordInterface
unlinkAll() Destroys the relationship in the current model. Yiisoft\ActiveRecord\ActiveRecordInterface
update() Saves the changes to this active record into the associated database table. Yiisoft\ActiveRecord\ActiveRecordInterface
updateAll() Updates the whole table using the provided property values and conditions. Yiisoft\ActiveRecord\ActiveRecordInterface
updateAllCounters() Updates the whole table using the provided counters and condition. Yiisoft\ActiveRecord\ActiveRecordInterface
updateCounters() Updates one or several counters for the current active record object. Yiisoft\ActiveRecord\ActiveRecordInterface
upsert() Insert a row into the associated database table if the record doesn't already exist (matching unique constraints) or update the record if it exists, with populating model by the returning record values. Yiisoft\ActiveRecord\ActiveRecordInterface

Method Details

Hide inherited methods

assignOldValue() public abstract method

Sets the old value of the named property.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::hasProperty().

public abstract assignOldValue( string $propertyName, mixed $value ): void
$propertyName string

The property name.

$value mixed
throws InvalidArgumentException

If the named property doesn't exist.

                public function assignOldValue(string $propertyName, mixed $value): void;

            
assignOldValues() public abstract method

Sets the old property values.

All existing old property values will be discarded.

public abstract assignOldValues( array|null $propertyValues null ): void
$propertyValues array|null

Old property values (name => value) to be set. If set to null this record is Yiisoft\ActiveRecord\ActiveRecordInterface::isNew().

                public function assignOldValues(?array $propertyValues = null): void;

            
column() public abstract method

public abstract column( string $propertyName ): \Yiisoft\Db\Schema\Column\ColumnInterface
$propertyName string
return \Yiisoft\Db\Schema\Column\ColumnInterface

The column instance of the property.

                public function column(string $propertyName): ColumnInterface;

            
createQuery() public abstract method

Create Yiisoft\ActiveRecord\ActiveQueryInterface instance for the given model class.

public abstract createQuery( Yiisoft\ActiveRecord\ActiveRecordInterface|string|null $modelClass null ): Yiisoft\ActiveRecord\ActiveQueryInterface
$modelClass Yiisoft\ActiveRecord\ActiveRecordInterface|string|null

The class name of the related record, or an instance of the related record. If null, the current model will be used.

                public function createQuery(self|string|null $modelClass = null): ActiveQueryInterface;

            
db() public abstract method

Returns the database connection used by the Active Record instance.

public abstract db( ): \Yiisoft\Db\Connection\ConnectionInterface

                public function db(): ConnectionInterface;

            
delete() public abstract method

Deletes the table row corresponding to this active record.

public abstract delete( ): integer
return integer

The number of rows deleted.

Note that it's possible the number of rows deleted is 0, even though the deletion execution is successful.

throws Yiisoft\ActiveRecord\OptimisticLockException

If the instance implements Yiisoft\ActiveRecord\OptimisticLockInterface and the data being deleted is outdated.

throws Throwable

In case delete failed.

                public function delete(): int;

            
deleteAll() public abstract method

Deletes rows in the table using the provided conditions.

For example, to delete all customers whose status is 3:

$customer = new Customer();
$customer->deleteAll('status = 3');

Warning: If you don't specify any condition, this method will delete all rows in the table.

$customers = Customer::query()->where('status = 3')->all();
foreach ($customers as $customer) {
    $customer->delete();
}

For a large set of models you might consider using Yiisoft\ActiveRecord\ActiveQuery::each() to keep memory usage within limits.

public abstract deleteAll( array $condition = [] ): integer
$condition array

The conditions that will be put in the WHERE part of the DELETE SQL. Please refer to \Yiisoft\ActiveRecord\Query::where() on how to specify this parameter.

return integer

The number of rows deleted.

throws \Yiisoft\Db\Exception\Exception
throws \Yiisoft\Db\Exception\InvalidConfigException
throws Throwable

                public function deleteAll(array $condition = []): int;

            
equals() public abstract method

Returns a value indicating whether the given active record is the same as the current one.

The comparison is made by comparing the table names and the primary key values of the two active records. If one of the records Yiisoft\ActiveRecord\ActiveRecordInterface::isNew() they're also considered not equal.

public abstract equals( self $record ): boolean
$record self

Record to compare to.

return boolean

Whether the two active records refer to the same row in the same database table.

                public function equals(self $record): bool;

            
get() public abstract method

Returns the named property value.

If this record is the result of a query and the property isn't loaded, null will be returned.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::hasProperty().

public abstract get( string $propertyName ): mixed
$propertyName string

The property name.

return mixed

The property value. null if the property isn't set or doesn't exist.

                public function get(string $propertyName): mixed;

            
hasMany() public abstract method

Declares a has-many relation.

The declaration is returned in terms of a relational Yiisoft\ActiveRecord\ActiveQuery instance through which the related record can be queried and retrieved back.

A has-many relation means that there are multiple related records matching the criteria set by this relation, e.g., a customer has many orders.

For example, to declare the orders relation for Customer class, you can write the following code in the Customer class:

public function getOrdersQuery()
{
    return $this->hasMany(Order::className(), ['customer_id' => 'id']);
}

Note that the customer_id key in the $link parameter refers to a property name in the related class Order, while the 'id' value refers to a property name in the current active record class.

Call methods declared in Yiisoft\ActiveRecord\ActiveQuery to further customize the relation.

public abstract hasMany( Yiisoft\ActiveRecord\ActiveRecordInterface|string $modelClass, array $link ): Yiisoft\ActiveRecord\ActiveQueryInterface
$modelClass Yiisoft\ActiveRecord\ActiveRecordInterface|string

The class name of the related record, or an instance of the related record.

$link array

The primary-foreign key constraint. The keys of the array refer to the property names of the record associated with the $class model, while the values of the array refer to the corresponding property names in this active record class.

return Yiisoft\ActiveRecord\ActiveQueryInterface

The relational query object.

                public function hasMany(self|string $modelClass, array $link): ActiveQueryInterface;

            
hasOne() public abstract method

Declares a has-one relation.

The declaration is returned in terms of a relational Yiisoft\ActiveRecord\ActiveQuery instance through which the related record can be queried and retrieved back.

A has-one relation means that there is at most one related record matching the criteria set by this relation, e.g., a customer has one country.

For example, to declare the country relation for Customer class, you can write the following code in the Customer class:

public function getCountryQuery()
{
    return $this->hasOne(Country::className(), ['id' => 'country_id']);
}

Note that the id key in the $link parameter refers to a property name in the related class Country, while the country_id value refers to a property name in the current active record class.

Call methods declared in Yiisoft\ActiveRecord\ActiveQuery to further customize the relation.

public abstract hasOne( Yiisoft\ActiveRecord\ActiveRecordInterface|string $modelClass, array $link ): Yiisoft\ActiveRecord\ActiveQueryInterface
$modelClass Yiisoft\ActiveRecord\ActiveRecordInterface|string

The class name of the related record, or an instance of the related record.

$link array

The primary-foreign key constraint. The keys of the array refer to the property names of the record associated with the $class model, while the values of the array refer to the corresponding property names in this active record class.

return Yiisoft\ActiveRecord\ActiveQueryInterface

The relational query object.

                public function hasOne(self|string $modelClass, array $link): ActiveQueryInterface;

            
hasProperty() public abstract method

Returns a value indicating whether the record has a property with the specified name.

public abstract hasProperty( string $name ): boolean
$name string

The name of the property.

                public function hasProperty(string $name): bool;

            
insert() public abstract method

Inserts a row into the associated database table using the property values of this record.

You may specify the properties to be inserted as list of name or name-value pairs. If name-value pair specified, the corresponding property values will be modified.

Only the Yiisoft\ActiveRecord\ActiveRecordInterface::newValues() changed property values will be inserted into a database.

If the table's primary key is auto incremental and is null during insertion, it will be populated with the actual value after insertion.

For example, to insert a customer record:

$customer = new Customer();
$customer->name = $name;
$customer->email = $email;
$customer->insert();

To insert a customer record with specific properties:

$customer->insert(['name' => $name, 'email' => $email]);
public abstract insert( array|null $properties null ): void
$properties array|null

List of property names or name-values pairs that need to be saved. Defaults to null, meaning all changed property values will be saved.

throws \Yiisoft\Db\Exception\InvalidCallException

If the record \Yiisoft\ActiveRecord\ActiveRecordInterface::isNew() is not new.

throws \Yiisoft\Db\Exception\InvalidConfigException
throws Throwable

In case insert failed.

                public function insert(?array $properties = null): void;

            
isChanged() public abstract method

Checks if any property returned by Yiisoft\ActiveRecord\ActiveRecordInterface::propertyNames() method has changed.

A new active record instance is considered changed if any property has been set including default values.

public abstract isChanged( ): boolean

                public function isChanged(): bool;

            
isNew() public abstract method

Returns a value indicating whether the current record is new (not saved in the database).

public abstract isNew( ): boolean
return boolean

Whether the record is new and should be inserted when calling Yiisoft\ActiveRecord\ActiveRecordInterface::save().

                public function isNew(): bool;

            
isPrimaryKey() public abstract method

Returns a value indicating whether the given set of property names represents the primary key for this active record.

public abstract isPrimaryKey( array $keys ): boolean
$keys array

The set of property names to check.

return boolean

Whether The given set of property names represents the primary key for this active record.

                public function isPrimaryKey(array $keys): bool;

            
isPropertyChanged() public abstract method

Returns whether the named property has been changed using the not identical operator !==.

public abstract isPropertyChanged( string $name ): boolean
$name string

The name of the property.

return boolean

Whether the property value has been changed.

                public function isPropertyChanged(string $name): bool;

            
isPropertyChangedNonStrict() public abstract method

Returns whether the named property has been changed using the not equal operator !=.

public abstract isPropertyChangedNonStrict( string $name ): boolean
$name string

The name of the property.

return boolean

Whether the property value has been changed non-strictly.

                public function isPropertyChangedNonStrict(string $name): bool;

            
isRelationPopulated() public abstract method

Check whether the named relation has been populated with records.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::relationQuery().

public abstract isRelationPopulated( string $name ): boolean
$name string

The relation name, for example, orders (case-sensitive).

return boolean

Whether relation has been populated with records.

                public function isRelationPopulated(string $name): bool;

            
link() public abstract method

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 a 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 isn't null.

public abstract link( string $relationName, self $linkModel, array $extraColumns = [] ): void
$relationName string

The relation name, for example, orders (case-sensitive).

$linkModel self

The record to be linked with the current one.

$extraColumns array

More column values to be saved into the junction table. This parameter is only meaningful for a relationship involving a junction table (that's a relation set with Yiisoft\ActiveRecord\ActiveQueryInterface::via()).

loadDefaultValues() public abstract method

Loads default values from database table schema.

You may call this method to load default values after creating a new instance:

// class Customer extends ActiveRecord
$customer = new Customer();
$customer->loadDefaultValues();
public abstract loadDefaultValues( boolean $skipIfSet true ): Yiisoft\ActiveRecord\ActiveRecordInterface
$skipIfSet boolean

Whether existing value should be preserved. This will only set defaults for properties that are null.

return Yiisoft\ActiveRecord\ActiveRecordInterface

The active record instance itself.

throws \Yiisoft\Db\Exception\Exception
throws \Yiisoft\Db\Exception\InvalidConfigException

                public function loadDefaultValues(bool $skipIfSet = true): static;

            
markAsExisting() public abstract method

Marks this record as existing. The record should be updated when calling Yiisoft\ActiveRecord\ActiveRecordInterface::save().

Note: all current properties will be considered unchanged after calling this method.

See also:

public abstract markAsExisting( ): void

                public function markAsExisting(): void;

            
markAsNew() public abstract method
public abstract markAsNew( ): void

                public function markAsNew(): void;

            
markPropertyChanged() public abstract method

Marks a property as changed.

This method may be called to force updating a record when calling Yiisoft\ActiveRecord\ActiveRecordInterface::update(), even if there is no change being made to the record.

public abstract markPropertyChanged( string $name ): void
$name string

The property name.

                public function markPropertyChanged(string $name): void;

            
newValues() public abstract method

Returns the property values that have been modified since they're loaded or saved most recently.

The comparison of new and old values uses ===.

public abstract newValues( array|null $propertyNames null ): array
$propertyNames array|null

The names of the properties whose values may be returned if they're changed recently. If null, Yiisoft\ActiveRecord\ActiveRecordInterface::propertyNames() will be used.

return array

The changed property values (name-value pairs).

                public function newValues(?array $propertyNames = null): array;

            
oldValue() public abstract method

Returns the old value of the named property.

If this record is the result of a query and the property is not loaded, null will be returned.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::hasProperty().

public abstract oldValue( string $propertyName ): mixed
$propertyName string

The property name.

return mixed

The old property value. null if the property is not loaded before or doesn't exist.

                public function oldValue(string $propertyName): mixed;

            
oldValues() public abstract method

Returns the old property values.

public abstract oldValues( ): array
return array

The old property values (name-value pairs).

                public function oldValues(): array;

            
populateProperties() public abstract method

Sets the property values in a massive way.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::propertyNames().

public abstract populateProperties( array $values ): void
$values array

Property values (name => value) to be assigned to the model.

                public function populateProperties(array $values): void;

            
populateRecord() public abstract method

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's mainly used by Yiisoft\ActiveRecord\ActiveQuery to populate the query results into active records.

public abstract populateRecord( array|object $row ): Yiisoft\ActiveRecord\ActiveRecordInterface
$row array|object

Property values (name => value).

throws \Yiisoft\Db\Exception\Exception
throws \Yiisoft\Db\Exception\InvalidConfigException

                public function populateRecord(array|object $row): static;

            
populateRelation() public abstract method

Populates the named relation with the related records.

Note that this method doesn't check if the relation exists or not.

public abstract populateRelation( string $name, array|array[]|self|self[]|null $records ): void
$name string

The relation name, for example, orders (case-sensitive).

$records array|array[]|self|self[]|null

The related records to be populated into the relation.

                public function populateRelation(string $name, array|self|null $records): void;

            
primaryKey() public abstract method

Returns the primary key names for this AR class.

The default implementation will return the primary keys as declared in the DB table that's associated with this AR class.

If the DB table doesn't declare any primary key, you should override this method to return the property names that you want to use as primary keys for this active record class.

Note that an array should be returned even for a table with a single primary key.

public abstract primaryKey( ): string[]
return string[]

The primary keys of the associated database table.

                public function primaryKey(): array;

            
primaryKeyOldValue() public abstract method

Returns the old value of the primary key as a scalar.

This refers to the primary key value that is populated into the record after data is retrieved from the database by an instance of Yiisoft\ActiveRecord\ActiveQueryInterface.

The value remains unchanged while the record will not be \Yiisoft\ActiveRecord\ActiveRecordInterface::update() updated.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::primaryKeyOldValues().

public abstract primaryKeyOldValue( ): float|integer|string|null
throws \Yiisoft\Db\Exception\Exception

If multiple primary keys or no primary key.

                public function primaryKeyOldValue(): float|int|string|null;

            
primaryKeyOldValues() public abstract method

Returns the old values of the primary key as an array with property names as keys and property values as values.

This refers to the primary key values that is populated into the record after data is retrieved from the database by an instance of Yiisoft\ActiveRecord\ActiveQueryInterface.

The value remains unchanged while the record will not be \Yiisoft\ActiveRecord\ActiveRecordInterface::update() updated.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::primaryKeyOldValues().

public abstract primaryKeyOldValues( ): array
throws \Yiisoft\Db\Exception\Exception

If no primary key or multiple primary keys.

                public function primaryKeyOldValues(): array;

            
primaryKeyValue() public abstract method

Returns the scalar value of the primary key.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::primaryKeyValues().

public abstract primaryKeyValue( ): float|integer|string|null
throws \Yiisoft\Db\Exception\Exception

If multiple primary keys or no primary key.

                public function primaryKeyValue(): float|int|string|null;

            
primaryKeyValues() public abstract method

Returns the values of the primary key as an array with property names as keys and property values as values.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::primaryKeyValue().

public abstract primaryKeyValues( ): array

                public function primaryKeyValues(): array;

            
propertyNames() public abstract method

Returns the list of property names mapped to column names of the table associated with this active record class.

public abstract propertyNames( ): array
return array

List of property names.

                public function propertyNames(): array;

            
propertyValues() public abstract method

Returns property values.

public abstract propertyValues( array|null $names null, array $except = [] ): array
$names array|null

List of property names whose value needs to be returned. Defaults to null, meaning all properties listed in Yiisoft\ActiveRecord\ActiveRecordInterface::propertyNames() will be returned.

$except array

List of property names whose value shouldn't be returned.

return array

Property values (name => value).

throws \Yiisoft\Db\Exception\Exception
throws \Yiisoft\Db\Exception\InvalidConfigException

                public function propertyValues(?array $names = null, array $except = []): array;

            
query() public abstract static method

Create Yiisoft\ActiveRecord\ActiveQueryInterface instance for the given model class.

public abstract static query( Yiisoft\ActiveRecord\ActiveRecordInterface|string|null $modelClass null ): Yiisoft\ActiveRecord\ActiveQueryInterface
$modelClass Yiisoft\ActiveRecord\ActiveRecordInterface|string|null

The class name of the related record, or an instance of the related record. If null, the current model class will be used.

                public static function query(self|string|null $modelClass = null): ActiveQueryInterface;

            
relatedRecords() public abstract method

Returns all populated related records.

See also Yiisoft\ActiveRecord\ActiveRecordInterface::relationQuery().

public abstract relatedRecords( ): array
return array

An array of related records indexed by relation names.

                public function relatedRecords(): array;

            
relation() public abstract method

Returns the relation object with the specified name.

public abstract relation( string $name ): array|array[]|self|self[]|null
$name string

The relation name, for example, orders (case-sensitive).

return array|array[]|self|self[]|null

The relation object.

                public function relation(string $name): self|array|null;

            
relationQuery() public abstract method

Returns the relation query object with the specified name.

A relation is defined by a getter method which returns an object implementing the Yiisoft\ActiveRecord\ActiveQueryInterface (normally this would be a relational Yiisoft\ActiveRecord\ActiveQuery object).

Relations can be defined using Yiisoft\ActiveRecord\ActiveRecordInterface::hasOne() and Yiisoft\ActiveRecord\ActiveRecordInterface::hasMany() methods. For example:

public function relationQuery(string $name): ActiveQueryInterface
{
    return match ($name) {
        'orders' => $this->hasMany(Order::class, ['customer_id' => 'id']),
        'country' => $this->hasOne(Country::class, ['id' => 'country_id']),
        default => parent::relationQuery($name),
    };
}
public abstract relationQuery( string $name ): Yiisoft\ActiveRecord\ActiveQueryInterface
$name string

The relation name, for example, orders (case-sensitive).

return Yiisoft\ActiveRecord\ActiveQueryInterface

The relational query object.

throws InvalidArgumentException

                public function relationQuery(string $name): ActiveQueryInterface;

            
resetRelation() public abstract method

Resets relation data for the specified name.

public abstract resetRelation( string $name ): void
$name string

The relation name, for example, orders (case-sensitive).

                public function resetRelation(string $name): void;

            
save() public abstract method

Saves the changes to this active record into the associated database table.

You may specify the properties to be updated as list of name or name-value pairs. If name-value pair specified, the corresponding property values will be modified.

Only the Yiisoft\ActiveRecord\ActiveRecordInterface::newValues() changed property values will be saved into a database.

This method will call Yiisoft\ActiveRecord\ActiveRecordInterface::insert() when Yiisoft\ActiveRecord\ActiveRecordInterface::isNew() is true, or Yiisoft\ActiveRecord\ActiveRecordInterface::update() when Yiisoft\ActiveRecord\ActiveRecordInterface::isNew() is false.

For example, to save a customer record:

$customer = new Customer();
$customer->name = $name;
$customer->email = $email;
$customer->save();

To save a customer record with specific properties:

$customer->save(['name' => $name, 'email' => $email]);
public abstract save( array|null $properties null ): void
$properties array|null

List of property names or name-values pairs that need to be saved. Defaults to null, meaning all changed property values will be saved.

                public function save(?array $properties = null): void;

            
set() public abstract method

Sets the named property value.

public abstract set( string $propertyName, mixed $value ): void
$propertyName string

The property name.

$value mixed
throws InvalidArgumentException

If the named property doesn't exist.

                public function set(string $propertyName, mixed $value): void;

            
tableName() public abstract method

Return the name of the table associated with this AR class.

final class User extends ActiveRecord
{
    public string const TABLE_NAME = 'user';

    public function tableName(): string
    {
         return self::TABLE_NAME;
    }
}
public abstract tableName( ): string

                public function tableName(): string;

            
tableSchema() public abstract method

Returns the schema information of the DB table associated with this AR class.

public abstract tableSchema( ): \Yiisoft\Db\Schema\TableSchemaInterface
return \Yiisoft\Db\Schema\TableSchemaInterface

The schema information of the DB table associated with this AR class.

throws \Yiisoft\Db\Exception\InvalidConfigException

If the table for the AR class doesn't exist.

                public function tableSchema(): TableSchemaInterface;

            
unlink() public abstract method

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 unlink( string $relationName, self $linkedModel, boolean $delete false ): void
$relationName string

The relation name, for example, orders (case-sensitive).

$linkedModel self

The active record to be unlinked from the current one.

$delete boolean

Whether to delete the active record that contains the foreign key. If false, the active record's foreign key will be set null and saved. If true, the active record containing the foreign key will be deleted.

unlinkAll() public abstract method

Destroys the relationship in the current model.

The active 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 model will be saved without validation.

To destroy the relationship without removing records, make sure your keys can be set to null.

public abstract unlinkAll( string $relationName, boolean $delete false ): void
$relationName string

The case-sensitive name of the relationship.

$delete boolean

Whether to delete the model that contains the foreign key.

throws \Yiisoft\Db\Exception\Exception
throws ReflectionException
throws Throwable

                public function unlinkAll(string $relationName, bool $delete = false): void;

            
update() public abstract method

Saves the changes to this active record into the associated database table.

You may specify the properties to be updated as list of name or name-value pairs. If name-value pair specified, the corresponding property values will be modified.

The method will then save the specified properties into a database.

Only the Yiisoft\ActiveRecord\ActiveRecordInterface::newValues() changed property values will be saved into a database.

For example, to update a customer record:

$customer = Customer::query()->findByPk(1);
$customer->name = $name;
$customer->email = $email;
$customer->update();

To update a customer record with specific properties:

$customer->update(['name' => $name, 'email' => $email]);

Note that it's possible the update doesn't affect any row in the table. In this case, this method will return 0. For this reason, you should use the following code to check if update() is successful or not:

if ($customer->update() !== 0) {
    // update successful
} else {
    // update failed
}
public abstract update( array|null $properties null ): integer
$properties array|null

List of property names or name-values pairs that need to be saved. Defaults to null, meaning all changed property values will be saved.

return integer

The number of rows affected.

throws \Yiisoft\Db\Exception\InvalidCallException

If the record \Yiisoft\ActiveRecord\ActiveRecordInterface::isNew() is new.

throws Yiisoft\ActiveRecord\OptimisticLockException

If the instance implements Yiisoft\ActiveRecord\OptimisticLockInterface and the data being updated is outdated.

throws Throwable

In case update failed.

                public function update(?array $properties = null): int;

            
updateAll() public abstract method

Updates the whole table using the provided property values and conditions.

For example, to change the status to be 1 for all customers whose status is 2:

$customer = new Customer();
$customer->updateAll(['status' => 1], 'status = 2');

Warning: If you don't specify any condition, this method will update all rows in the table.

$customers = Customer::query()->where('status = 2')->all();
foreach ($customers as $customer) {
    $customer->status = 1;
    $customer->update();
}

For a large set of models you might consider using Yiisoft\ActiveRecord\ActiveQuery::each() to keep memory usage within limits.

public abstract updateAll( array $propertyValues, array|string $condition = [], array|\Yiisoft\Db\Expression\ExpressionInterface|string|null $from null, array $params = [] ): integer
$propertyValues array

Property values (name-value pairs) to be saved into the table.

$condition array|string

The conditions that will be put in the WHERE part of the UPDATE SQL. Please refer to \Yiisoft\ActiveRecord\Query::where() on how to specify this parameter.

$from array|\Yiisoft\Db\Expression\ExpressionInterface|string|null

The FROM part of the UPDATE SQL. Please refer to \Yiisoft\ActiveRecord\QueryPartsInterface::from() on how to specify this parameter.

$params array

The parameters (name => value) to be bound to the query.

return integer

The number of rows updated.

throws \Yiisoft\Db\Exception\InvalidConfigException
throws Throwable

if the models can't be unlinked.

throws \Yiisoft\Db\Exception\Exception

                public function updateAll(array $propertyValues, array|string $condition = [], array|ExpressionInterface|string|null $from = null, array $params = []): int;

            
updateAllCounters() public abstract method

Updates the whole table using the provided counters and condition.

For example, to increment all customers' age by 1:

$customer = new Customer();
$customer->updateAllCounters(['age' => 1]);

Note that this method will not trigger any events.

public abstract updateAllCounters( array $counters, array|string $condition '', array|\Yiisoft\Db\Expression\ExpressionInterface|string|null $from null, array $params = [] ): integer
$counters array

The counters to be updated (property name => increment value). Use negative values if you want to decrement the counters.

$condition array|string

The conditions that will be put in the WHERE part of the UPDATE SQL. Please refer to \Yiisoft\ActiveRecord\Query::where() on how to specify this parameter.

$from array|\Yiisoft\Db\Expression\ExpressionInterface|string|null

The FROM part of the UPDATE SQL. Please refer to \Yiisoft\ActiveRecord\QueryPartsInterface::from() on how to specify this parameter.

$params array

The parameters (name => value) to be bound to the query.

Do not name the parameters as :bp0, :bp1, etc., because they are used internally by this method.

return integer

The number of rows updated.

                public function updateAllCounters(
    array $counters,
    array|string $condition = '',
    array|ExpressionInterface|string|null $from = null,
    array $params = [],
): int;

            
updateCounters() public abstract method

Updates one or several counters for the current active record object.

Note that this method differs from Yiisoft\ActiveRecord\ActiveRecordInterface::updateAllCounters() in that it only saves counters for the current active record object.

An example usage is as follows:

$post = new Post();
$post->updateCounters(['view_count' => 1]);

See also Yiisoft\ActiveRecord\ActiveRecordInterface::updateAllCounters().

public abstract updateCounters( array $counters ): void
$counters array

The counters to be updated (property name => increment value), use negative values if you want to decrement the counters.

                public function updateCounters(array $counters): void;

            
upsert() public abstract method

Insert a row into the associated database table if the record doesn't already exist (matching unique constraints) or update the record if it exists, with populating model by the returning record values.

Only the Yiisoft\ActiveRecord\ActiveRecordInterface::newValues() changed property values will be inserted or updated.

If the table's primary key is auto incremental and is null during execution, it will be populated with the actual value after insertion or update.

For example, to upsert a customer record:

$customer = new Customer();
$customer->name = $name;
$customer->email = $email; // unique property
$customer->created_at = new DateTimeImmutable();
$customer->upsert();

To upsert a customer record with specific properties:

$customer->upsert(
    ['name' => $name, 'email' => $email, 'created_at' => new DateTimeImmutable()],
    ['name', 'email', 'updated_at' => new DateTimeImmutable()],
);
public abstract upsert( array|null $insertProperties null, array|boolean $updateProperties true ): void
$insertProperties array|null

List of property names or name-values pairs that need to be inserted. Defaults to null, meaning all changed property values will be inserted.

$updateProperties array|boolean

List of property names or name-values pairs that need to be updated if the record already exists. Also available a boolean value:

  • true the record values will be updated to match the insert property values;
  • false no update will be performed if the record already exist.
throws \Yiisoft\Db\Exception\InvalidConfigException
throws Throwable

In case query failed.

                public function upsert(?array $insertProperties = null, array|bool $updateProperties = true): void;