PHP Class EMongoDocument, MongoYii

The active record itself
Inheritance: extends EMongoModel
Exibir arquivo Open project: sammaye/mongoyii Class Usage Examples

Public Methods

Method Description
__call ( string $name, array $parameters ) : EMongoDocument | mixed This, in addition to EMongoModels edition, will also call scopes on the model
__construct ( string $scenario = 'insert' ) Sets up our model and set the field cache just like in EMongoModel
aggregate ( array $pipeline ) : mixed This is an aggregate helper on the model Note: This does not return the model but instead the result array directly from MongoDB.
cache ( integer $duration, CCacheDependency | ICacheDependency $dependency = null, integer $queryCount = 1 ) : EMongoDocument Sets the parameters for query caching.
collectionName ( ) : string Returns the collection name as a string
count ( EMongoCriteria | array $criteria = [] ) : integer Count() allows you to count all the documents returned by a certain condition, it is analogous to $db->collection->find()->count() and basically does exactly that.
defaultScope ( ) : array Sets the default scope
delete ( ) : boolean Deletes this record
deleteAll ( array | EMongoCriteria $criteria = [], array $options = [] ) : mixed Delete all records matching a criteria
deleteByPk ( string | MongoId $pk, array | EMongoCriteria $criteria = [], array $options = [] ) : mixed Delete record by pk
distinct ( string $key, array $query = [] ) : mixed A distinct helper on the model, this is not the same as the aggregation framework distinct
ensureIndexes ( unknown $indexes ) Allows a user to ensure a number of indexes using the format:
equals ( EMongoDocument $record ) : boolean Compares current active record with another one.
exists ( array $criteria = [] ) : boolean Checks if a record exists in the database
find ( array | EMongoCriteria $criteria = [], array | string[] $fields = [] ) : EMongoCursor | EMongoDocument[] Find some records
findAll ( array $criteria = [], array | string[] $fields = [] ) : EMongoCursor | EMongoDocument[] Alias of find
findAllByAttributes ( array $criteria = [], array | string[] $fields = [] ) : EMongoCursor | EMongoDocument[] Alias of find
findAllByPk ( mixed $pk, array | string[] $fields = [] ) : EMongoCursor | EMongoDocument[] Finds all records based on $pk
findAndModify ( $criteria = [], $updateDoc = [], $fields = [], $options = [] ) : null | EMongoDocument Find one record, modifies, and returns it.
findByPk ( string | MongoId $pk, array | string[] $fields = [] ) : EMongoDocument | null An alias for findBy_id() that relates to Yiis own findByPk
findBy_id ( string | MongoId $_id, array | string[] $fields = [] ) : EMongoDocument | null Finds one by _id
findOne ( array | EMongoCriteria $criteria = [], array | string[] $fields = [] ) : EMongoDocument | null Find one record
getAttributeLabel ( string $attribute ) : string Returns the text label for the specified attribute.
getCollection ( ) : MongoCollection Gets the collection for this model
getDbCriteria ( boolean $createIfNull = true ) : array gets and if null sets the db criteria for this model
getIsNewRecord ( ) : boolean Returns if the current record is new.
getLastError ( ) : array A bit deceptive, this actually gets the last response from either save() or update(). The reason it is called this is because MongoDB calls it this and so it seems better to have unity on that front.
getLatest ( ) : null | EMongoDocument Is basically a find one of the last version to be saved
getMongoId ( string | MongoId $value = null ) : MongoId Returns MongoId based on $value
getPrimaryKey ( string | MongoId $value = null ) : MongoId Returns the value of the primary key
getProjectedFields ( ) : array | string[] Gets a list of the projected fields for the model
incrementVersion ( ) : boolean Forceably increments the version of this document
insert ( array $attributes = null ) : boolean Inserts this record
mapreduce ( MongoCode $map, MongoCode $reduce, MongoCode $finalize = null, array $out = [], array $query = [], array $options = [] ) : mixed A mapreduce helper for this model
mergeCriteria ( array $oldCriteria, array $newCriteria ) : array Merges two criteria objects. Best used for scopes
mergeDbCriteria ( array | EMongoCriteria $newCriteria ) : array Merges the current DB Criteria with the inputted one
model ( string $className = __CLASS__ ) : EMongoDocument Returns the static model of the specified AR class.
onAfterDelete ( CEvent $event )
onAfterFind ( CEvent $event )
onAfterSave ( CEvent $event )
onBeforeDelete ( CEvent $event )
onBeforeFind ( CEvent $event )
onBeforeSave ( CEvent $event )
populateRecord ( array $attributes, boolean $callAfterFind = true, boolean $partial = false ) : EMongoDocument | null Creates an active record with the given attributes.
populateRecords ( array $data, boolean $callAfterFind = true, string $index = null ) : array Returns an array of records populated by incoming data
refresh ( ) : boolean Refreshes the data from the database
resetScope ( boolean $resetDefault = true ) : EMongoDocument Resets the scopes applied to the model clearing the _criteria variable
save ( boolean $runValidation = true, array $attributes = null ) : boolean Saves this record
saveAttributes ( array $attributes ) : boolean Saves only a specific subset of attributes as defined by the param
saveCounters ( array $counters, null $lower = null, null $upper = null ) : boolean
scopes ( ) : array The scope attached to this model
search ( array $query = [], array $project = [], boolean $partialMatch = false, array $sort = [] ) : EMongoDataProvider Gives basic searching abilities for things like CGridView
setAttribute ( string $name, mixed $value ) : boolean Sets the attribute of the model
setDbCriteria ( array $criteria ) : array Sets the db criteria for this model
setIsNewRecord ( boolean $value ) Sets if the record is new.
setProjectedFields ( array $fields ) Sets the projected fields of the model
setVersion ( mixed $n ) : boolean Forceably sets the version of this document
trace ( string $func ) Produces a trace message for functions in this class
update ( array $attributes = null ) : boolean Updates this record
updateAll ( array | EMongoCriteria $criteria = [], array $updateDoc = [], array $options = ['multiple' => true] ) : boolean Update all records matching a criteria
updateByPk ( string | MongoId $pk, array $updateDoc = [], array | EMongoCriteria $criteria = [], array $options = [] ) : boolean Update record by PK
version ( ) : string Gets the version of this document
versionField ( ) : string Denotes the field tob e used to house the version number
versioned ( ) : boolean Denotes whether or not this document is versioned

Protected Methods

Method Description
afterDelete ( )
afterFind ( )
afterSave ( )
beforeDelete ( ) : boolean
beforeFind ( )
beforeSave ( ) : boolean
instantiate ( array $document ) : EMongoDocument Instantiates a model from an array

Method Details

__call() public method

This, in addition to EMongoModels edition, will also call scopes on the model
See also: EMongoModel::__call()
public __call ( string $name, array $parameters ) : EMongoDocument | mixed
$name string
$parameters array
return EMongoDocument | mixed

__construct() public method

It will also set the default scope on the model so be aware that if you want the default scope to not be applied you will need to run resetScope() straight after making this model
public __construct ( string $scenario = 'insert' )
$scenario string

afterDelete() protected method

protected afterDelete ( )

afterFind() protected method

protected afterFind ( )

afterSave() protected method

protected afterSave ( )

aggregate() public method

This is an aggregate helper on the model Note: This does not return the model but instead the result array directly from MongoDB.
public aggregate ( array $pipeline ) : mixed
$pipeline array
return mixed

beforeDelete() protected method

protected beforeDelete ( ) : boolean
return boolean

beforeFind() protected method

protected beforeFind ( )

beforeSave() protected method

protected beforeSave ( ) : boolean
return boolean

cache() public method

This is a shortcut method to {@link CDbConnection::cache()}. It changes the query caching parameter of the {@link dbConnection} instance.
public cache ( integer $duration, CCacheDependency | ICacheDependency $dependency = null, integer $queryCount = 1 ) : EMongoDocument
$duration integer the number of seconds that query results may remain valid in cache. If this is 0, the caching will be disabled.
$dependency CCacheDependency | ICacheDependency the dependency that will be used when saving the query results into cache.
$queryCount integer number of MongoDB queries that need to be cached after calling this method. Defaults to 1, meaning that the next MongoDB query will be cached.
return EMongoDocument the active record instance itself.

collectionName() public method

Returns the collection name as a string
public collectionName ( ) : string
return string

count() public method

..
public count ( EMongoCriteria | array $criteria = [] ) : integer
$criteria EMongoCriteria | array
return integer

defaultScope() public method

Sets the default scope
public defaultScope ( ) : array
return array - An array which represents a single scope within the scope() function

delete() public method

Deletes this record
public delete ( ) : boolean
return boolean

deleteAll() public method

Delete all records matching a criteria
public deleteAll ( array | EMongoCriteria $criteria = [], array $options = [] ) : mixed
$criteria array | EMongoCriteria
$options array
return mixed

deleteByPk() public method

Delete record by pk
public deleteByPk ( string | MongoId $pk, array | EMongoCriteria $criteria = [], array $options = [] ) : mixed
$pk string | MongoId
$criteria array | EMongoCriteria
$options array
return mixed

distinct() public method

A distinct helper on the model, this is not the same as the aggregation framework distinct
public distinct ( string $key, array $query = [] ) : mixed
$key string
$query array
return mixed

ensureIndexes() public method

ensureIndexes(array( array(array('email' => 1), array('unique' => true)) )) where the 0 offset in each nested array is the fields for the index and the 1 offset is the options. You don't have to define options for the index. or: ensureIndexes(array( array('email' => 1, 'address' => -1) ))
public ensureIndexes ( unknown $indexes )
$indexes unknown

equals() public method

The comparison is made by comparing table name and the primary key values of the two active records.
public equals ( EMongoDocument $record ) : boolean
$record EMongoDocument - record to compare to
return boolean - whether the two active records refer to the same row in the database table.

exists() public method

Checks if a record exists in the database
public exists ( array $criteria = [] ) : boolean
$criteria array
return boolean

find() public method

Find some records
public find ( array | EMongoCriteria $criteria = [], array | string[] $fields = [] ) : EMongoCursor | EMongoDocument[]
$criteria array | EMongoCriteria
$fields array | string[]
return EMongoCursor | EMongoDocument[]

findAll() public method

Alias of find
public findAll ( array $criteria = [], array | string[] $fields = [] ) : EMongoCursor | EMongoDocument[]
$criteria array
$fields array | string[]
return EMongoCursor | EMongoDocument[]

findAllByAttributes() public method

Alias of find
public findAllByAttributes ( array $criteria = [], array | string[] $fields = [] ) : EMongoCursor | EMongoDocument[]
$criteria array
$fields array | string[]
return EMongoCursor | EMongoDocument[]

findAllByPk() public method

Finds all records based on $pk
public findAllByPk ( mixed $pk, array | string[] $fields = [] ) : EMongoCursor | EMongoDocument[]
$pk mixed - String, MongoID or array of strings or MongoID values (one can mix strings and MongoID in the array)
$fields array | string[]
return EMongoCursor | EMongoDocument[]

findAndModify() public method

Find one record, modifies, and returns it.
public findAndModify ( $criteria = [], $updateDoc = [], $fields = [], $options = [] ) : null | EMongoDocument
return null | EMongoDocument

findByPk() public method

An alias for findBy_id() that relates to Yiis own findByPk
public findByPk ( string | MongoId $pk, array | string[] $fields = [] ) : EMongoDocument | null
$pk string | MongoId
$fields array | string[]
return EMongoDocument | null

findBy_id() public method

Finds one by _id
public findBy_id ( string | MongoId $_id, array | string[] $fields = [] ) : EMongoDocument | null
$_id string | MongoId
$fields array | string[]
return EMongoDocument | null

findOne() public method

Find one record
public findOne ( array | EMongoCriteria $criteria = [], array | string[] $fields = [] ) : EMongoDocument | null
$criteria array | EMongoCriteria
$fields array | string[]
return EMongoDocument | null

getAttributeLabel() public method

This method overrides the parent implementation by supporting returning the label defined in relational object. In particular, if the attribute name is in the form of "post.author.name", then this method will derive the label from the "author" relation's "name" attribute.
See also: CModel::generateAttributeLabel()
public getAttributeLabel ( string $attribute ) : string
$attribute string - the attribute name
return string - the attribute label

getCollection() public method

Gets the collection for this model
public getCollection ( ) : MongoCollection
return MongoCollection

getDbCriteria() public method

gets and if null sets the db criteria for this model
public getDbCriteria ( boolean $createIfNull = true ) : array
$createIfNull boolean
return array

getIsNewRecord() public method

Whether the record is new and should be inserted when calling {@link save}. This property is automatically set in constructor and {@link populateRecord}. Defaults to false, but it will be set to true if the instance is created using the new operator.
public getIsNewRecord ( ) : boolean
return boolean

getLastError() public method

A bit deceptive, this actually gets the last response from either save() or update(). The reason it is called this is because MongoDB calls it this and so it seems better to have unity on that front.
public getLastError ( ) : array
return array

getLatest() public method

Is basically a find one of the last version to be saved
public getLatest ( ) : null | EMongoDocument
return null | EMongoDocument

getMongoId() public method

Returns MongoId based on $value
Deprecation: This function will become deprecated in favour of consistently using the getPrimaryKey() function instead. Atm, however, the getPrimaryKey function actually chains onto this method. If you see this and are wondering about what you should do if you want custom primary keys etc just use the getPrimaryKey function as you would the getMongoId function. These two functions should never have been separate for they are the same essentially. As to what version this will become deprecated:- I dunno. It will not be soon since it will be a functionality breaker...
public getMongoId ( string | MongoId $value = null ) : MongoId
$value string | MongoId
return MongoId

getPrimaryKey() public method

Returns the value of the primary key
public getPrimaryKey ( string | MongoId $value = null ) : MongoId
$value string | MongoId
return MongoId

getProjectedFields() public method

Gets a list of the projected fields for the model
public getProjectedFields ( ) : array | string[]
return array | string[]

incrementVersion() public method

Forceably increments the version of this document
public incrementVersion ( ) : boolean
return boolean

insert() public method

Inserts this record
public insert ( array $attributes = null ) : boolean
$attributes array
return boolean

instantiate() protected method

Instantiates a model from an array
protected instantiate ( array $document ) : EMongoDocument
$document array
return EMongoDocument

mapreduce() public method

A mapreduce helper for this model
public mapreduce ( MongoCode $map, MongoCode $reduce, MongoCode $finalize = null, array $out = [], array $query = [], array $options = [] ) : mixed
$map MongoCode
$reduce MongoCode
$finalize MongoCode
$out array
$query array
$options array // All other options for input to the command
return mixed

mergeCriteria() public method

Merges two criteria objects. Best used for scopes
public mergeCriteria ( array $oldCriteria, array $newCriteria ) : array
$oldCriteria array
$newCriteria array
return array

mergeDbCriteria() public method

Merges the current DB Criteria with the inputted one
public mergeDbCriteria ( array | EMongoCriteria $newCriteria ) : array
$newCriteria array | EMongoCriteria
return array

model() public static method

The model returned is a static instance of the AR class. It is provided for invoking class-level methods (something similar to static class methods.) EVERY derived AR class must override this method as follows,
public static function model($className=__CLASS__)
{
    return parent::model($className);
}
public static model ( string $className = __CLASS__ ) : EMongoDocument
$className string
return EMongoDocument

onAfterDelete() public method

public onAfterDelete ( CEvent $event )
$event CEvent

onAfterFind() public method

public onAfterFind ( CEvent $event )
$event CEvent

onAfterSave() public method

public onAfterSave ( CEvent $event )
$event CEvent

onBeforeDelete() public method

public onBeforeDelete ( CEvent $event )
$event CEvent

onBeforeFind() public method

public onBeforeFind ( CEvent $event )
$event CEvent

onBeforeSave() public method

public onBeforeSave ( CEvent $event )
$event CEvent

populateRecord() public method

This method is internally used by the find methods. Null is returned if the input data is false.
public populateRecord ( array $attributes, boolean $callAfterFind = true, boolean $partial = false ) : EMongoDocument | null
$attributes array - attribute values (column name=>column value)
$callAfterFind boolean whether to call {@link afterFind} after the record is populated.
$partial boolean
return EMongoDocument | null - the newly created active record. The class of the object is the same as the model class.

populateRecords() public method

Returns an array of records populated by incoming data
public populateRecords ( array $data, boolean $callAfterFind = true, string $index = null ) : array
$data array
$callAfterFind boolean
$index string
return array - Array of the records

refresh() public method

Refreshes the data from the database
public refresh ( ) : boolean
return boolean

resetScope() public method

Resets the scopes applied to the model clearing the _criteria variable
public resetScope ( boolean $resetDefault = true ) : EMongoDocument
$resetDefault boolean
return EMongoDocument

save() public method

If an attributes specification is sent in it will only validate and save those attributes
public save ( boolean $runValidation = true, array $attributes = null ) : boolean
$runValidation boolean
$attributes array
return boolean

saveAttributes() public method

Saves only a specific subset of attributes as defined by the param
public saveAttributes ( array $attributes ) : boolean
$attributes array
return boolean

saveCounters() public method

See also: http://www.yiiframework.com/doc/api/1.1/CActiveRecord#saveCounters-detail
public saveCounters ( array $counters, null $lower = null, null $upper = null ) : boolean
$counters array
$lower null - define a lower that the counter should not pass. IS NOT ATOMIC
$upper null
return boolean

scopes() public method

It is very much like how Yii normally uses scopes except the params are slightly different.
public scopes ( ) : array
return array

setAttribute() public method

Sets the attribute of the model
public setAttribute ( string $name, mixed $value ) : boolean
$name string
$value mixed
return boolean

setDbCriteria() public method

Sets the db criteria for this model
public setDbCriteria ( array $criteria ) : array
$criteria array
return array

setIsNewRecord() public method

Whether the record is new and should be inserted when calling {@link save}.
See also: EMongoDocument::getIsNewRecord()
public setIsNewRecord ( boolean $value )
$value boolean

setProjectedFields() public method

Sets the projected fields of the model
public setProjectedFields ( array $fields )
$fields array

setVersion() public method

Forceably sets the version of this document
public setVersion ( mixed $n ) : boolean
$n mixed
return boolean

trace() public method

Produces a trace message for functions in this class
public trace ( string $func )
$func string

update() public method

Updates this record
public update ( array $attributes = null ) : boolean
$attributes array
return boolean

updateAll() public method

Update all records matching a criteria
public updateAll ( array | EMongoCriteria $criteria = [], array $updateDoc = [], array $options = ['multiple' => true] ) : boolean
$criteria array | EMongoCriteria
$updateDoc array
$options array
return boolean

updateByPk() public method

Update record by PK
public updateByPk ( string | MongoId $pk, array $updateDoc = [], array | EMongoCriteria $criteria = [], array $options = [] ) : boolean
$pk string | MongoId
$updateDoc array
$criteria array | EMongoCriteria
$options array
return boolean

version() public method

Gets the version of this document
public version ( ) : string
return string

versionField() public method

Denotes the field tob e used to house the version number
public versionField ( ) : string
return string

versioned() public method

Denotes whether or not this document is versioned
public versioned ( ) : boolean
return boolean