PHP Class yii\sphinx\ActiveRecord

Warning: optimistic lock will NOT work in case of updating fields (not attributes) for the runtime indexes!
Since: 2.0
Author: Paul Klimov ([email protected])
Inheritance: extends yii\db\BaseActiveRecord
Datei anzeigen Open project: yiisoft/yii2-sphinx Class Usage Examples

Public Methods

Method Description
attributes ( ) : array Returns the list of all attribute names of the model.
callKeywords ( string $text, boolean $fetchStatistic = false ) : array Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics.
callSnippets ( string | array $source, string $match, array $options = [] ) : string | array Builds a snippet from provided data and query, using specified index settings.
delete ( ) : integer | false Deletes the index entry corresponding to this active record.
deleteAll ( string | array $condition = '', array $params = [] ) : integer Deletes rows in the index using the provided conditions.
equals ( ActiveRecord $record ) : boolean Returns a value indicating whether the given active record is the same as the current one.
find ( ) : ActiveQuery
findBySql ( string $sql, array $params = [] ) : ActiveQuery Creates an ActiveQuery instance with a given SQL statement.
getDb ( ) : Connection Returns the Sphinx connection used by this AR class.
getIndexSchema ( ) : IndexSchema Returns the schema information of the Sphinx index associated with this AR class.
getSnippet ( string $match = null, array $options = [] ) : string Returns current snippet value or generates new one from given match.
getSnippetSource ( ) : string Returns the string, which should be used as a source to create snippet for this Active Record instance.
indexName ( ) : string Declares the name of the Sphinx index associated with this AR class.
insert ( boolean $runValidation = true, array $attributes = null ) : boolean Inserts a row into the associated Sphinx index using the attribute values of this record.
isTransactional ( integer $operation ) : boolean Returns a value indicating whether the specified operation is transactional in the current [[scenario]].
populateRecord ( $record, $row )
primaryKey ( ) : string[] Returns the primary key name for this AR class.
setSnippet ( string $snippet )
transactions ( ) : array Declares which operations should be performed within a transaction in different scenarios.
update ( boolean $runValidation = true, array $attributeNames = null ) : integer | false Saves the changes to this active record into the associated Sphinx index.
updateAll ( array $attributes, string | array $condition = '', array $params = [] ) : integer Updates the whole table using the provided attribute values and conditions.

Protected Methods

Method Description
fetchSnippet ( string $match, array $options = [] ) : string Builds up the snippet value from the given query.
updateInternal ( $attributes = null )

Private Methods

Method Description
insertInternal ( $attributes = null )

Method Details

attributes() public method

The default implementation will return all column names of the table associated with this AR class.
public attributes ( ) : array
return array list of attribute names.

callKeywords() public static method

Returns tokenized and normalized forms of the keywords, and, optionally, keyword statistics.
public static callKeywords ( string $text, boolean $fetchStatistic = false ) : array
$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 callSnippets ( string | array $source, string $match, array $options = [] ) : string | array
$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

This method performs the following steps in order: 1. call [[beforeDelete()]]. If the method returns false, it will skip the rest of the steps; 2. delete the record from the index; 3. 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 delete ( ) : integer | false
return integer | false the number of rows deleted, or `false` if the deletion is unsuccessful for some reason. Note that it is possible the number of rows deleted is 0, even though the deletion execution is successful.

deleteAll() public static method

For example, to delete all articles whose status is 3: php Article::deleteAll('status = 3');
public static deleteAll ( string | array $condition = '', array $params = [] ) : integer
$condition string | array the conditions that will be put in the WHERE part of the DELETE SQL. Please refer to [[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

The comparison is made by comparing the index names and the primary key values of the two active records. If one of the records [[isNewRecord|is new]] they are also considered not equal.
public equals ( ActiveRecord $record ) : boolean
$record 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 fetchSnippet ( string $match, array $options = [] ) : string
$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

public static find ( ) : ActiveQuery
return ActiveQuery the newly created [[ActiveQuery]] instance.

findBySql() public static method

Note that because the SQL statement is already specified, calling additional query modification methods (such as where(), order()) on the created ActiveQuery instance will have no effect. However, calling with(), asArray() or indexBy() is still fine. Below is an example: php $customers = Article::findBySql("SELECT * FROM idx_article WHERE MATCH('development')")->all();
public static findBySql ( string $sql, array $params = [] ) : ActiveQuery
$sql string the SQL statement to be executed
$params array parameters to be bound to the SQL statement during execution.
return ActiveQuery the newly created [[ActiveQuery]] instance

getDb() public static method

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 getDb ( ) : Connection
return 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 getIndexSchema ( ) : IndexSchema
return IndexSchema the schema information of the Sphinx index associated with this AR class.

getSnippet() public method

Returns current snippet value or generates new one from given match.
public getSnippet ( string $match = null, array $options = [] ) : string
$match string snippet source query
$options array list of options in format: optionName => optionValue
return string snippet value

getSnippetSource() public method

Child classes must implement this method to return the actual snippet source text. For example: php public function getSnippetSource() { return $this->snippetSourceRelation->content; }
public getSnippetSource ( ) : string
return string snippet source string.

indexName() public static method

By default this method returns the class name as the index name by calling [[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 indexName ( ) : string
return string the index name

insert() public method

This method performs the following steps in order: 1. call [[beforeValidate()]] when $runValidation is true. If validation fails, it will skip the rest of the steps; 2. call [[afterValidate()]] when $runValidation is true. 3. call [[beforeSave()]]. If the method returns false, it will skip the rest of the steps; 4. insert the record into index. If this fails, it will skip the rest of the steps; 5. 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 [[changedAttributes|changed attribute values]] will be inserted. For example, to insert an article record: php $article = new Article(); $article->id = $id; $article->genre_id = $genreId; $article->content = $content; $article->insert();
public insert ( boolean $runValidation = true, array $attributes = null ) : boolean
$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.

isTransactional() public method

Returns a value indicating whether the specified operation is transactional in the current [[scenario]].
public isTransactional ( integer $operation ) : boolean
$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

public static populateRecord ( $record, $row )

primaryKey() public static method

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 primaryKey ( ) : string[]
return string[] the primary keys of the associated Sphinx index.

setSnippet() public method

public setSnippet ( string $snippet )
$snippet string

transactions() public method

The supported DB operations are: [[OP_INSERT]], [[OP_UPDATE]] and [[OP_DELETE]], which correspond to the ActiveRecord::insert, ActiveRecord::update and ActiveRecord::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, php 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 (ActiveRecord::insert) should be done in a transaction; and in the "api" scenario, all the operations should be done in a transaction.
public transactions ( ) : array
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

This method performs the following steps in order: 1. call [[beforeValidate()]] when $runValidation is true. If validation fails, it will skip the rest of the steps; 2. call [[afterValidate()]] when $runValidation is true. 3. call [[beforeSave()]]. If the method returns false, it will skip the rest of the steps; 4. save the record into index. If this fails, it will skip the rest of the steps; 5. 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 [[changedAttributes|changed attribute values]] will be saved into database. For example, to update an article record: php $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: php if ($this->update() !== false) { update successful } else { update failed }
public update ( boolean $runValidation = true, array $attributeNames = null ) : integer | false
$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 `false` if validation fails or [[beforeSave()]] stops the updating process.

updateAll() public static method

For example, to change the status to be 1 for all articles which status is 2: php Article::updateAll(['status' => 1], 'status = 2');
public static updateAll ( array $attributes, string | array $condition = '', array $params = [] ) : integer
$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 [[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: CActiveRecord::update()
protected updateInternal ( $attributes = null )