PersistedModel has a large set of methods for creating, updating, and deleting data.
Model data is also called a model instance; in database terminology, conceptually a model corresponds to a table, and a model instance corresponds to a row or record in the table.
Note: For information on model read operations, see Querying data.
Creating data (model instances)
Use the following PersistedModel methods to add data, that is to insert or create instances:
- create - creates a new model instance (record).
- upsert - checks if the instance (record) exists, based on the designated ID property, which must have a unique value; if the instance already exists, the method updates that instance. Otherwise, it inserts a new instance.
-
findOrCreate - Find one instance matching the filter object provided as the first parameter. If found, returns the object. If not found, creates a new instance (record).
Important: Be sure to include a
where
clause in the filter object. Without thewhere
, thefindOrCreate
finds and returns the first record in the collection, without error, which can lead to unintended behavior.
</div>
- save - Save model instance. If the instance doesn’t have an ID, then calls create instead. Triggers: validate, save, update, or create.
Updating data (model instances)
Static method (called on the Model object):
- updateAll - updates multiple instances (records) that match the specified where clause.
Important:
The where clause used with updateAll()
is slightly different than that for queries. Omit { where : ... }
from the where clause.
Simply provide the condition as the first argument.
For more information, see Where filter.
Instance methods (called on a single model instance):
- updateAttribute - Update a single attribute (property).
- updateAttributes - Update set of attributes (properties). Performs validation before updating.
Performing bulk updates
REVIEW COMMENT from Rand
Describe why you would perform bulk updates. Used with sync, for example.</div>
Deleting data
Static methods (called on the Model object):
- destroyAll - Delete all model instances that match the optional Where filter.
- destroyById - Delete the model instance with the specified ID.
Important:
The where clause with destroyAll()
is slightly different than that for queries. Omit { where : ... }
from the where clause. Simply provide the condition as the first argument.
For more information, see Where filter.