Abstract Class yii\sphinx\ActiveRecord
Inheritance | yii\sphinx\ActiveRecord » yii\db\BaseActiveRecord » yii\base\Model » yii\base\Component » yii\base\Object |
---|---|
Implements | ArrayAccess, IteratorAggregate, yii\base\Arrayable, yii\base\Configurable, yii\db\ActiveRecordInterface |
Uses Traits | yii\base\ArrayableTrait |
Available since version | 2.0 |
Source Code | https://github.com/yiisoft/yii2-sphinx/blob/master/ActiveRecord.php |
ActiveRecord is the base class for classes representing relational data in terms of objects.
Warning: optimistic lock will NOT work in case of updating fields (not attributes) for the runtime indexes!
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$activeValidators | yii\validators\Validator[] | The validators applicable to the current $scenario. | yii\base\Model |
$attributes | array | Attribute values (name => value). | yii\base\Model |
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component | yii\base\Component |
$dirtyAttributes | array | The changed attribute values (name-value pairs) | yii\db\BaseActiveRecord |
$errors | array | Errors for all attributes or the specified attribute. | yii\base\Model |
$firstErrors | array | The first errors. | yii\base\Model |
$isNewRecord | boolean | Whether the record is new and should be inserted when calling save(). | yii\db\ActiveRecordInterface |
$iterator | ArrayIterator | An iterator for traversing the items in the list. | yii\base\Model |
$oldAttributes | array | The old attribute values (name-value pairs) | yii\db\BaseActiveRecord |
$oldPrimaryKey | mixed | The old primary key value. | yii\db\ActiveRecordInterface |
$primaryKey | mixed | The primary key value. | yii\db\ActiveRecordInterface |
$relatedRecords | array | An array of related records indexed by relation names. | yii\db\BaseActiveRecord |
$scenario | string | The scenario that this model is in. | yii\base\Model |
$snippet | string | yii\sphinx\ActiveRecord | |
$snippetSource | string | Snippet source string. | yii\sphinx\ActiveRecord |
$validators | ArrayObject|yii\validators\Validator[] | All the validators declared in the model. | yii\base\Model |
Public Methods
Method | Description | Defined By |
---|---|---|
__call() | Calls the named method which is not a class method. | yii\base\Object |
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component |
__construct() | Constructor. | yii\base\Object |
__get() | PHP getter magic method. | yii\db\BaseActiveRecord |
__isset() | Checks if a property value is null. | yii\db\BaseActiveRecord |
__set() | PHP setter magic method. | yii\db\BaseActiveRecord |
__unset() | Sets a component property to be null. | yii\db\BaseActiveRecord |
activeAttributes() | Returns the attribute names that are subject to validation in the current scenario. | yii\base\Model |
addError() | Adds a new error to the specified attribute. | yii\base\Model |
addErrors() | Adds a list of errors. | yii\base\Model |
afterDelete() | This method is invoked after deleting a record. | yii\db\BaseActiveRecord |
afterFind() | This method is called when the AR object is created and populated with the query result. | yii\db\BaseActiveRecord |
afterRefresh() | This method is called when the AR object is refreshed. | yii\db\BaseActiveRecord |
afterSave() | This method is called at the end of inserting or updating a record. | yii\db\BaseActiveRecord |
afterValidate() | This method is invoked after validation ends. | yii\base\Model |
attachBehavior() | Attaches a behavior to this component. | yii\base\Component |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component |
attributeHints() | Returns the attribute hints. | yii\base\Model |
attributeLabels() | Returns the attribute labels. | yii\base\Model |
attributes() | Returns the list of all attribute names of the model. | yii\sphinx\ActiveRecord |
beforeDelete() | This method is invoked before deleting a record. | yii\db\BaseActiveRecord |
beforeSave() | This method is called at the beginning of inserting or updating a record. | yii\db\BaseActiveRecord |
beforeValidate() | This method is invoked before validation starts. | yii\base\Model |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component |
callKeywords() | Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics. | yii\sphinx\ActiveRecord |
callSnippets() | Builds a snippet from provided data and query, using specified index settings. | yii\sphinx\ActiveRecord |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\db\BaseActiveRecord |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\db\BaseActiveRecord |
className() | Returns the fully qualified name of this class. | yii\base\Object |
clearErrors() | Removes errors for all attributes or a single attribute. | yii\base\Model |
createValidators() | Creates validator objects based on the validation rules specified in rules(). | yii\base\Model |
delete() | Deletes the index entry corresponding to this active record. | yii\sphinx\ActiveRecord |
deleteAll() | Deletes rows in the index using the provided conditions. | yii\sphinx\ActiveRecord |
detachBehavior() | Detaches a behavior from the component. | yii\base\Component |
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component |
equals() | Returns a value indicating whether the given active record is the same as the current one. | yii\sphinx\ActiveRecord |
extraFields() | Returns the list of fields that can be expanded further and returned by toArray(). | yii\base\ArrayableTrait |
fields() | Returns the list of fields that should be returned by default by toArray() when no specific fields are specified. | yii\base\ArrayableTrait |
find() | Creates an \yii\sphinx\ActiveQueryInterface instance for query purpose. | yii\sphinx\ActiveRecord |
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 |
findBySql() | Creates an yii\sphinx\ActiveQuery instance with a given SQL statement. | yii\sphinx\ActiveRecord |
findOne() | Returns a single active record model instance by a primary key or an array of column values. | yii\db\ActiveRecordInterface |
formName() | Returns the form name that this model class should use. | yii\base\Model |
generateAttributeLabel() | Generates a user friendly attribute label based on the give attribute name. | yii\base\Model |
getActiveValidators() | Returns the validators applicable to the current $scenario. | yii\base\Model |
getAttribute() | Returns the named attribute value. | yii\db\ActiveRecordInterface |
getAttributeHint() | Returns the text hint for the specified attribute. | yii\db\BaseActiveRecord |
getAttributeLabel() | Returns the text label for the specified attribute. | yii\db\BaseActiveRecord |
getAttributes() | Returns attribute values. | yii\base\Model |
getBehavior() | Returns the named behavior object. | yii\base\Component |
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component |
getDb() | Returns the Sphinx connection used by this AR class. | yii\sphinx\ActiveRecord |
getDirtyAttributes() | Returns the attribute values that have been modified since they are loaded or saved most recently. | yii\db\BaseActiveRecord |
getErrors() | Returns the errors for all attributes or a single attribute. | yii\base\Model |
getFirstError() | Returns the first error of the specified attribute. | yii\base\Model |
getFirstErrors() | Returns the first error of every attribute in the model. | yii\base\Model |
getIndexSchema() | Returns the schema information of the Sphinx index associated with this AR class. | yii\sphinx\ActiveRecord |
getIsNewRecord() | Returns a value indicating whether the current record is new (not saved in the database). | yii\db\ActiveRecordInterface |
getIterator() | Returns an iterator for traversing the attributes in the model. | yii\base\Model |
getOldAttribute() | Returns the old value of the named attribute. | yii\db\BaseActiveRecord |
getOldAttributes() | Returns the old attribute values. | yii\db\BaseActiveRecord |
getOldPrimaryKey() | Returns the old primary key value(s). | yii\db\ActiveRecordInterface |
getPrimaryKey() | Returns the primary key value(s). | yii\db\ActiveRecordInterface |
getRelatedRecords() | Returns all populated related records. | yii\db\BaseActiveRecord |
getRelation() | Returns the relation object with the specified name. | yii\db\ActiveRecordInterface |
getScenario() | Returns the scenario that this model is used in. | yii\base\Model |
getSnippet() | Returns current snippet value or generates new one from given match. | yii\sphinx\ActiveRecord |
getSnippetSource() | Returns the string, which should be used as a source to create snippet for this Active Record instance. | yii\sphinx\ActiveRecord |
getValidators() | Returns all the validators declared in rules(). | yii\base\Model |
hasAttribute() | Returns a value indicating whether the record has an attribute with the specified name. | yii\db\ActiveRecordInterface |
hasErrors() | Returns a value indicating whether there is any validation error. | yii\base\Model |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component |
hasMany() | Declares a has-many relation. | yii\db\BaseActiveRecord |
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Object |
hasOne() | Declares a has-one relation. | yii\db\BaseActiveRecord |
hasProperty() | Returns a value indicating whether a property is defined. | yii\base\Object |
indexName() | Declares the name of the Sphinx index associated with this AR class. | yii\sphinx\ActiveRecord |
init() | Initializes the object. | yii\db\BaseActiveRecord |
insert() | Inserts a row into the associated Sphinx index using the attribute values of this record. | yii\sphinx\ActiveRecord |
instantiate() | Creates an active record instance. | yii\db\BaseActiveRecord |
isAttributeActive() | Returns a value indicating whether the attribute is active in the current scenario. | yii\base\Model |
isAttributeChanged() | Returns a value indicating whether the named attribute has been changed. | yii\db\BaseActiveRecord |
isAttributeRequired() | Returns a value indicating whether the attribute is required. | yii\base\Model |
isAttributeSafe() | Returns a value indicating whether the attribute is safe for massive assignments. | yii\base\Model |
isPrimaryKey() | Returns a value indicating whether the given set of attributes represents the primary key for this model | yii\db\ActiveRecordInterface |
isRelationPopulated() | Check whether the named relation has been populated with records. | yii\db\BaseActiveRecord |
isTransactional() | Returns a value indicating whether the specified operation is transactional in the current $scenario. | yii\sphinx\ActiveRecord |
link() | Establishes the relationship between two records. | yii\db\ActiveRecordInterface |
load() | Populates the model with input data. | yii\base\Model |
loadMultiple() | Populates a set of models with the data from end user. | yii\base\Model |
markAttributeDirty() | Marks an attribute dirty. | yii\db\BaseActiveRecord |
off() | Detaches an existing event handler from this component. | yii\base\Component |
offsetExists() | Returns whether there is an element at the specified offset. | yii\db\BaseActiveRecord |
offsetGet() | Returns the element at the specified offset. | yii\base\Model |
offsetSet() | Sets the element at the specified offset. | yii\base\Model |
offsetUnset() | Sets the element value at the specified offset to null. | yii\db\BaseActiveRecord |
on() | Attaches an event handler to an event. | yii\base\Component |
onUnsafeAttribute() | This method is invoked when an unsafe attribute is being massively assigned. | yii\base\Model |
optimisticLock() | Returns the name of the column that stores the lock version for implementing optimistic locking. | yii\db\BaseActiveRecord |
populateRecord() | Populates an active record object using a row of data from the database/storage. | yii\sphinx\ActiveRecord |
populateRelation() | Populates the named relation with the related records. | yii\db\ActiveRecordInterface |
primaryKey() | Returns the primary key name for this AR class. | yii\sphinx\ActiveRecord |
refresh() | Repopulates this active record with the latest data. | yii\db\BaseActiveRecord |
rules() | Returns the validation rules for attributes. | yii\base\Model |
safeAttributes() | Returns the attribute names that are safe to be massively assigned in the current scenario. | yii\base\Model |
save() | Saves the current record. | yii\db\ActiveRecordInterface |
scenarios() | Returns a list of scenarios and the corresponding active attributes. | yii\base\Model |
setAttribute() | Sets the named attribute value. | yii\db\ActiveRecordInterface |
setAttributes() | Sets the attribute values in a massive way. | yii\base\Model |
setIsNewRecord() | Sets the value indicating whether the record is new. | yii\db\BaseActiveRecord |
setOldAttribute() | Sets the old value of the named attribute. | yii\db\BaseActiveRecord |
setOldAttributes() | Sets the old attribute values. | yii\db\BaseActiveRecord |
setScenario() | Sets the scenario for the model. | yii\base\Model |
setSnippet() | yii\sphinx\ActiveRecord | |
toArray() | Converts the model into an array. | yii\base\ArrayableTrait |
transactions() | Declares which operations should be performed within a transaction in different scenarios. | yii\sphinx\ActiveRecord |
trigger() | Triggers an event. | yii\base\Component |
unlink() | Destroys the relationship between two records. | yii\db\ActiveRecordInterface |
unlinkAll() | Destroys the relationship in current model. | yii\db\BaseActiveRecord |
update() | Saves the changes to this active record into the associated Sphinx index. | yii\sphinx\ActiveRecord |
updateAll() | Updates the whole table using the provided attribute values and conditions. | yii\sphinx\ActiveRecord |
updateAllCounters() | Updates the whole table using the provided counter changes and conditions. | yii\db\BaseActiveRecord |
updateAttributes() | Updates the specified attributes. | yii\db\BaseActiveRecord |
updateCounters() | Updates one or several counter columns for the current AR object. | yii\db\BaseActiveRecord |
validate() | Performs the data validation. | yii\base\Model |
validateMultiple() | Validates multiple models. | yii\base\Model |
Protected Methods
Method | Description | Defined By |
---|---|---|
createRelationQuery() | Creates a query instance for has-one or has-many relation. | yii\db\BaseActiveRecord |
fetchSnippet() | Builds up the snippet value from the given query. | yii\sphinx\ActiveRecord |
findByCondition() | Finds ActiveRecord instance(s) by the given condition. | yii\db\BaseActiveRecord |
resolveFields() | Determines which fields can be returned by toArray(). | yii\base\ArrayableTrait |
updateInternal() | yii\sphinx\ActiveRecord |
Events
Event | Type | Description | Defined By |
---|---|---|---|
EVENT_AFTER_DELETE | yii\base\Event | An event that is triggered after a record is deleted. | yii\db\BaseActiveRecord |
EVENT_AFTER_FIND | yii\base\Event | An event that is triggered after the record is created and populated with query result. | yii\db\BaseActiveRecord |
EVENT_AFTER_INSERT | yii\db\AfterSaveEvent | An event that is triggered after a record is inserted. | yii\db\BaseActiveRecord |
EVENT_AFTER_REFRESH | yii\base\Event | An event that is triggered after a record is refreshed. (available since version 2.0.8) | yii\db\BaseActiveRecord |
EVENT_AFTER_UPDATE | yii\db\AfterSaveEvent | An event that is triggered after a record is updated. | yii\db\BaseActiveRecord |
EVENT_AFTER_VALIDATE | yii\base\Event | An event raised at the end of validate() | yii\base\Model |
EVENT_BEFORE_DELETE | yii\base\ModelEvent | An event that is triggered before deleting a record. | yii\db\BaseActiveRecord |
EVENT_BEFORE_INSERT | yii\base\ModelEvent | An event that is triggered before inserting a record. | yii\db\BaseActiveRecord |
EVENT_BEFORE_UPDATE | yii\base\ModelEvent | An event that is triggered before updating a record. | yii\db\BaseActiveRecord |
EVENT_BEFORE_VALIDATE | yii\base\ModelEvent | An event raised at the beginning of validate(). | yii\base\Model |
EVENT_INIT | yii\base\Event | An event that is triggered when the record is initialized via init(). | yii\db\BaseActiveRecord |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
OP_ALL | 7 | All three operations: insert, update, delete. This is a shortcut of the expression: OP_INSERT | OP_UPDATE | OP_DELETE. | yii\sphinx\ActiveRecord |
OP_DELETE | 4 | The delete operation. This is mainly used when overriding transactions() to specify which operations are transactional. | yii\sphinx\ActiveRecord |
OP_INSERT | 1 | The insert operation. This is mainly used when overriding transactions() to specify which operations are transactional. | yii\sphinx\ActiveRecord |
OP_UPDATE | 2 | The update operation. This is mainly used when overriding transactions() to specify which operations are transactional. | yii\sphinx\ActiveRecord |
SCENARIO_DEFAULT | 'default' | The name of the default scenario. | yii\base\Model |
Property Details
$snippet public property
public string getSnippet ( $match = null, $options = [] )
public void setSnippet ( $snippet )
$snippetSource public read-only property
Snippet source string.
public string getSnippetSource ( )
Method Details
attributes() public method
Returns the list of all attribute names of the model.
The default implementation will return all column names of the table associated with this AR class.
public array attributes ( ) | ||
---|---|---|
return | array |
List of attribute names. |
callKeywords() public static method
Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics.
public static array callKeywords ( $text, $fetchStatistic = false ) | ||
---|---|---|
$text | string |
The text to break down to keywords. |
$fetchStatistic | boolean |
Whether to return document and hit occurrence statistics |
return | array |
Keywords and statistics |
callSnippets() public static method
Builds a snippet from provided data and query, using specified index settings.
public static string|array callSnippets ( $source, $match, $options = [] ) | ||
---|---|---|
$source | string|array |
Is the source data to extract a snippet from. It could be either a single string or array of strings. |
$match | string |
The full-text query to build snippets for. |
$options | array |
List of options in format: optionName => optionValue |
return | string|array |
Built snippet in case "source" is a string, list of built snippets in case "source" is an array. |
delete() public method
Deletes the index entry corresponding to this active record.
This method performs the following steps in order:
- call beforeDelete(). If the method returns false, it will skip the rest of the steps;
- delete the record from the index;
- call afterDelete().
In the above step 1 and 3, events named EVENT_BEFORE_DELETE and EVENT_AFTER_DELETE will be raised by the corresponding methods.
public integer|false delete ( ) | ||
---|---|---|
return | integer|false |
The number of rows deleted, or |
throws | yii\db\StaleObjectException |
if optimistic locking is enabled and the data being deleted is outdated. |
throws | Exception |
in case delete failed. |
deleteAll() public static method
Deletes rows in the index using the provided conditions.
For example, to delete all articles whose status is 3:
Article::deleteAll('status = 3');
public static integer deleteAll ( $condition = '', $params = [] ) | ||
---|---|---|
$condition | string|array |
The conditions that will be put in the WHERE part of the DELETE SQL. Please refer to yii\sphinx\Query::where() on how to specify this parameter. |
$params | array |
The parameters (name => value) to be bound to the query. |
return | integer |
The number of rows deleted |
equals() public method
Returns a value indicating whether the given active record is the same as the current one.
The comparison is made by comparing the index names and the primary key values of the two active records. If one of the records is new they are also considered not equal.
public boolean equals ( $record ) | ||
---|---|---|
$record | yii\sphinx\ActiveRecord |
Record to compare to |
return | boolean |
Whether the two active records refer to the same row in the same index. |
fetchSnippet() protected method
Builds up the snippet value from the given query.
protected string fetchSnippet ( $match, $options = [] ) | ||
---|---|---|
$match | string |
The full-text query to build snippets for. |
$options | array |
List of options in format: optionName => optionValue |
return | string |
Snippet value. |
find() public static method
Creates an \yii\sphinx\ActiveQueryInterface instance for query purpose.
The returned \yii\sphinx\ActiveQueryInterface instance can be further customized by calling methods defined in \yii\sphinx\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 static yii\sphinx\ActiveQuery find ( ) | ||
---|---|---|
return | yii\sphinx\ActiveQuery |
The newly created yii\sphinx\ActiveQuery instance. |
findBySql() public static method
Creates an yii\sphinx\ActiveQuery instance with a given SQL statement.
Note that because the SQL statement is already specified, calling additional query modification methods (such as where()
, order()
) on the created yii\sphinx\ActiveQuery instance will have no effect. However, calling with()
, asArray()
or indexBy()
is still fine.
Below is an example:
$customers = Article::findBySql("SELECT * FROM `idx_article` WHERE MATCH('development')")->all();
public static yii\sphinx\ActiveQuery findBySql ( $sql, $params = [] ) | ||
---|---|---|
$sql | string |
The SQL statement to be executed |
$params | array |
Parameters to be bound to the SQL statement during execution. |
return | yii\sphinx\ActiveQuery |
The newly created yii\sphinx\ActiveQuery instance |
getDb() public static method
Returns the Sphinx connection used by this AR class.
By default, the "sphinx" application component is used as the Sphinx connection. You may override this method if you want to use a different Sphinx connection.
public static yii\sphinx\Connection getDb ( ) | ||
---|---|---|
return | yii\sphinx\Connection |
The Sphinx connection used by this AR class. |
getIndexSchema() public static method
Returns the schema information of the Sphinx index associated with this AR class.
public static yii\sphinx\IndexSchema getIndexSchema ( ) | ||
---|---|---|
return | yii\sphinx\IndexSchema |
The schema information of the Sphinx index associated with this AR class. |
throws | yii\base\InvalidConfigException |
if the index for the AR class does not exist. |
getSnippet() public method
Returns current snippet value or generates new one from given match.
public string getSnippet ( $match = null, $options = [] ) | ||
---|---|---|
$match | string |
Snippet source query |
$options | array |
List of options in format: optionName => optionValue |
return | string |
Snippet value |
getSnippetSource() public method
Returns the string, which should be used as a source to create snippet for this Active Record instance.
Child classes must implement this method to return the actual snippet source text. For example:
public function getSnippetSource() { return $this->snippetSourceRelation->content; }
public string getSnippetSource ( ) | ||
---|---|---|
return | string |
Snippet source string. |
throws | yii\base\NotSupportedException |
if this is not supported by the Active Record class |
indexName() public static method
Declares the name of the Sphinx index associated with this AR class.
By default this method returns the class name as the index name by calling yii\helpers\Inflector::camel2id(). For example, 'Article' becomes 'article', and 'StockItem' becomes 'stock_item'. You may override this method if the index is not named after this convention.
public static string indexName ( ) | ||
---|---|---|
return | string |
The index name |
insert() public method
Inserts a row into the associated Sphinx index using the attribute values of this record.
This method performs the following steps in order:
- call beforeValidate() when
$runValidation
is true. If validation fails, it will skip the rest of the steps; - call afterValidate() when
$runValidation
is true. - call beforeSave(). If the method returns false, it will skip the rest of the steps;
- insert the record into index. If this fails, it will skip the rest of the steps;
- call afterSave();
In the above step 1, 2, 3 and 5, events EVENT_BEFORE_VALIDATE, EVENT_BEFORE_INSERT, EVENT_AFTER_INSERT and EVENT_AFTER_VALIDATE will be raised by the corresponding methods.
Only the \yii\sphinx\changedAttributes will be inserted.
For example, to insert an article record:
$article = new Article(); $article->id = $id; $article->genre_id = $genreId; $article->content = $content; $article->insert();
public boolean insert ( $runValidation = true, $attributes = null ) | ||
---|---|---|
$runValidation | boolean |
Whether to perform validation before saving the record. If the validation fails, the record will not be inserted. |
$attributes | array |
List of attributes that need to be saved. Defaults to null, meaning all attributes that are loaded from index will be saved. |
return | boolean |
Whether the attributes are valid and the record is inserted successfully. |
throws | Exception |
in case insert failed. |
isTransactional() public method
Returns a value indicating whether the specified operation is transactional in the current $scenario.
public boolean isTransactional ( $operation ) | ||
---|---|---|
$operation | integer |
The operation to check. Possible values are OP_INSERT, OP_UPDATE and OP_DELETE. |
return | boolean |
Whether the specified operation is transactional in the current $scenario. |
populateRecord() public static 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 is mainly used by yii\sphinx\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 populateRecord ( $record, $row ) | ||
---|---|---|
$record | yii\db\BaseActiveRecord |
The record to be populated. In most cases this will be an instance created by instantiate() beforehand. |
$row | array |
Attribute values (name => value) |
primaryKey() public static method
Returns the primary key name for this AR class.
The default implementation will return the primary key as declared in the Sphinx index, which is associated with this AR class.
Note that an array should be returned even for a table with single primary key.
public static string[] primaryKey ( ) | ||
---|---|---|
return | string[] |
The primary keys of the associated Sphinx index. |
setSnippet() public method
public void setSnippet ( $snippet ) | ||
---|---|---|
$snippet | string |
transactions() public method
Declares which operations should be performed within a transaction in different scenarios.
The supported DB operations are: OP_INSERT, OP_UPDATE and OP_DELETE, which correspond to the insert(), update() and delete() methods, respectively. By default, these methods are NOT enclosed in a transaction.
In some scenarios, to ensure data consistency, you may want to enclose some or all of them in transactions. You can do so by overriding this method and returning the operations that need to be transactional. For example,
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, ];
The above declaration specifies that in the "admin" scenario, the insert operation (insert()) should be done in a transaction; and in the "api" scenario, all the operations should be done in a transaction.
public array transactions ( ) | ||
---|---|---|
return | array |
The declarations of transactional operations. The array keys are scenarios names, and the array values are the corresponding transaction operations. |
update() public method
Saves the changes to this active record into the associated Sphinx index.
This method performs the following steps in order:
- call beforeValidate() when
$runValidation
is true. If validation fails, it will skip the rest of the steps; - call afterValidate() when
$runValidation
is true. - call beforeSave(). If the method returns false, it will skip the rest of the steps;
- save the record into index. If this fails, it will skip the rest of the steps;
- call afterSave();
In the above step 1, 2, 3 and 5, events EVENT_BEFORE_VALIDATE, EVENT_BEFORE_UPDATE, EVENT_AFTER_UPDATE and EVENT_AFTER_VALIDATE will be raised by the corresponding methods.
Only the \yii\sphinx\changedAttributes will be saved into database.
For example, to update an article record:
$article = Article::findOne($id); $article->genre_id = $genreId; $article->group_id = $groupId; $article->update();
Note that it is possible the update does not 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 ($this->update() !== false) { // update successful } else { // update failed }
public integer|false update ( $runValidation = true, $attributeNames = null ) | ||
---|---|---|
$runValidation | boolean |
Whether to perform validation before saving the record. If the validation fails, the record will not be inserted into the database. |
$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|false |
The number of rows affected, or |
throws | yii\db\StaleObjectException |
if optimistic locking is enabled and the data being updated is outdated. |
throws | Exception |
in case update failed. |
updateAll() public static method
Updates the whole table using the provided attribute values and conditions.
For example, to change the status to be 1 for all articles which status is 2:
Article::updateAll(['status' => 1], 'status = 2');
public static integer updateAll ( $attributes, $condition = '', $params = [] ) | ||
---|---|---|
$attributes | array |
Attribute values (name-value pairs) to be saved into the table |
$condition | string|array |
The conditions that will be put in the WHERE part of the UPDATE SQL. Please refer to yii\sphinx\Query::where() 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 |
updateInternal() protected method
See also \yii\sphinx\CActiveRecord::update().
protected void updateInternal ( $attributes = null ) | ||
---|---|---|
$attributes | ||
throws | yii\db\StaleObjectException |
© 2008–2017 by Yii Software LLC
Licensed under the three clause BSD license.
http://www.yiiframework.com/doc-2.0/yii-sphinx-activerecord.html