Maslosoft Mangan Documentation
MongoDB Object Persister
Active Document
Active document is a model approach that allows to invoke model operations directly on it's instance.
Mangan has pre-composed class, which have all
required active document methods: Document
along
with it's a bit lighter counterpart for embedded documents: EmbeddedDocument
The methods available in Document
Method | Documentation |
---|---|
getCollection() |
Get working mongo collection instance. Should not be called manually in most cases. |
getCollectionName() |
This method must return collection name for use with this model. By default it uses full class name, with slashes replaced by dots. If |
getDbCriteria() |
Returns the Criteria associated with this model. |
setDbCriteria() |
Set new criteria, previous criteria will be destroyed. |
getDefaultLanguage() |
Get default language used for I18N operations. If not previously set, will fall back to |
setDefaultLanguage() |
Set default language used for I18N operations. This language
will be used if the The value should be language code, for example |
getErrors() |
Get validation errors. To obtain results, first |
setErrors() |
Set errors. Usually should not be called manually. |
getLang() |
Get current working language code |
setLang() |
Set language code. This changes current model language. After setting language model attributes will store values in different locale. Language code must be previously set by
For initial call, when there are no data set yet, |
getLanguages() |
Get available languages |
setLanguages() |
Set available languages. This method accepts one parameter, array contaning language codes prefably in short ISO form. Example valid array and method calls:
For initial call, when there are no data set yet, |
getOwner() |
Get document owner. This might return |
setOwner() |
Set class owner |
getRawI18N() |
Get i18n values with all languages. This returns all language values of all class fields. Class field names are keys for arrays of language values, with language codes as a keys. Example returned variable: |
setRawI18N() |
Set i18n values in all languages.
This method must keep |
getRoot() |
Get document root. This will traverse through objects hierarchy, untill owner-less document is found. This might return same object instance if there is no owner. The return value is guaranteed to be document instance. |
getScenario() |
Get current scenario |
setScenario() |
Set current scenario |
__construct() |
Constructor |
addAspect() |
Add aspect |
count() |
Counts all documents satisfying the specified condition. |
countByAttributes() |
Counts all documents found by attribute values. Example: |
delete() |
Deletes the database document corresponding to this |
deleteAll() |
Deletes documents with the specified criteria. Optional See |
deleteAllByPk() |
Deletes documents with the specified primary keys. Additional See |
deleteByPk() |
Deletes document with the specified primary key. Additional See |
deleteOne() |
Deletes one document with the specified primary keys. Optional Does not raise See |
exists() |
Checks whether there is document satisfying the specified condition. |
find() |
Finds a single Document with the specified condition. |
findAll() |
Finds all documents satisfying the specified condition. |
findAllByAttributes() |
Finds all documents with the specified attributes. |
findAllByPk() |
Finds all documents with the specified primary keys. In MongoDB world every document has '_id' unique field, so with this method that field is in use as PK by default. See {@link find()} for detailed explanation about $condition. |
findAndModify() |
Find and modify single document atomically. By default, this function will return updated document, ie document with applied Modifier operations. To return document before applied updates, set parameter
This function will raise events and signals before operation on current model. The events and signals after operation will be performed
on the returned model, depending on |
findByAttributes() |
Finds document with the specified attributes. Attributes should be specified as key-value pairs. This allows easier syntax for simple queries. Example: |
findByPk() |
Finds document with the specified primary key. Primary key by default
is defined by For composite use key-value with column names as keys and values for values. Example for simple pk:
Composite pk: |
hasAspect() |
Check if has aspect |
insert() |
Inserts a document into the collection based on this active document attributes. Note, validation is not performed in this method. You may call After the record is inserted to DB successfully, its scenario will be set to be 'update'. |
model() |
Returns the empty model of the specified Document class. It is provided for invoking class-level methods, espacially userfull for finders. Example usage: |
refresh() |
Repopulates this active document with the latest data. |
removeAspect() |
Remove aspect |
replace() |
Replaces the current document. NOTE: This will overwrite entire document. Any filtered out properties will be removed as well. The record is inserted as a documnent into the database collection, if exists it will be replaced. Validation will be performed before saving the record. If the validation fails,
the record will not be saved. You can call If the validation fails, the record will not be saved to database. |
save() |
Saves the current document. The document is inserted into collection if it is not already saved. The check whether to update or insert document is dony by primary key. Validation will be performed before saving the record. If the validation fails,
the record will not be saved. You can call If the record is saved its scenario will be set to be 'update'.
And if its primary key is of type of If the validation fails, the record will not be saved to database. |
update() |
Updates the document represented by this active document.
All loaded attributes will be saved to the database.
Note, validation is not performed in this method. You may call |
updateAll() |
Atomic, in-place update method. |
updateOne() |
Updates one document with the specified criteria and attributes This is more raw update:
|
upsert() |
Updates or inserts the current document. This will try to update existing fields. Will keep already stored data if present in document. If document does not exist, a new one will be inserted. |
validate() |
Validate model attributes according to current scenario (if applicable) The |
withCursor() |
Whenever to use cursor |