Updating multiple objects¶
This operation updates one or more objects in a database table. The objects to be updated can be identified with:
objectId
values- A query string (a where clause)
- Result of a previously ran bulkCreate or object retrieval operation in the same transaction.
To add this operation to a Unit Of Work instance, use the following methods. The methods are split into categories based on how the the objects are identified in the API:
// This operation explicitly lists the objectId values of the objects
// which will be updated in the database. These objectId values must
// be present in the objectsToUpdate argument. Each referenced
// object must be stored in the database table with the "tableName"
// name. Backendless identifies the objects in the database and
// updates them with the values from the changes argument.
// The "changes" argument is a collection of key/value pairs. Each
// key in the collection must correspond to a column/property of the
// updated objects. The value corresponding to the key is the update
// the objects will receive.
unitOfWorkInstance.BulkUpdate( String tableName,
List<String> objectsToUpdate,
Dictionary<String, Object> changes );
// This operation runs a query expressed through the whereClause
// argument. The query is executed in a table with the "tableName"
// name. For more information see the API documentation about
// querying with whereClause. Objects which match the query
// are updated with the values from the "changes" argument.
// The "changes" argument is a collection of key/value pairs.
// Each key in the collection must correspond to a column/property
// of the updated objects. The value corresponding to the key is
// the update the objects will receive
unitOfWorkInstance.BulkUpdate( String tableName,
String whereClause,
Dictionary<String, Object> changes );
// Update the objects identified by the objectIdsForChanges argument,
// which is a result of another operation in the same transaction.
// These operations can be either "Retrieving objects" or "Saving
// multiple objects". Objects identified by "objectIdsForChanges"
// are updated in the database with the values from the "changes"
// collection.
unitOfWorkInstance.BulkUpdate( OpResult objectIdsForChanges, Dictionary<String, Object> changes );
Return Value¶
The operation returns an OpResult
object which represents the result of this operation - number of objects updated in the database.
Example¶
Consider the example below. The example adds an operation to update multiple objects in the database. The change is to set the orderStatus
property to "completed"
for all orders which were delivered. This is the what the data table looks like before the transaction runs:
// compose a list of objects to be updated
// here we hard-code the objectId values,
// but in your code these values may be
// coming either from a query or from your
// data model on the client side.
List<String> objectsForChanges = new List<String>();
objectsForChanges.Add( "37ADFFA8-39E2-73A5-FF44-651EF5C4E900" );
objectsForChanges.Add( "996E2034-4CC6-0BC4-FF01-E92077819700" );
objectsForChanges.Add( "EB68A9E1-DC24-C961-FFC4-2EF9227BF700" );
// create a unit of work object
UnitOfWork unitOfWork = new UnitOfWork();
// compose the changes object. The change it
// has it to update the "orderStatus" column
// with the value "completed"
Dictionary<String, Object> changes = new Dictionary<String, Object>();
changes[ "orderStatus" ] = "completed";
// add the "BulkUpdate" operation to the transaction
unitOfWork.BulkUpdate( "Order", objectsForChanges, changes );
// run the transaction
unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
//Transaction completed
},
fault =>
{
//Server reported an error
} ) );
// create a unit of work object
UnitOfWork unitOfWork = new UnitOfWork();
// set the where clause. It will identify all
// objects where the "orderStatus" column has the
// value of "delivery pending".
String whereClause = "orderStatus = 'delivery pending'";
// compose the changes object. The change it
// has it to update the "orderStatus" column
// with the value "completed"
Dictionary<String, Object> changes = new Dictionary<String, Object>();
changes[ "orderStatus" ] = "completed";
// add the "BulkUpdate" operation to the transaction
unitOfWork.BulkUpdate( "Order", whereClause, changes );
// run the transaction
unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
//Transaction completed
},
fault =>
{
//Server reported an error
} ) );
// create a unit of work object
UnitOfWork unitOfWork = new UnitOfWork();
// compose a query. It will identify all Order
// objects where the "orderStatus" column has the
// value of "delivery pending".
DataQueryBuilder queryBuilder = DataQueryBuilder.Create();
queryBuilder.SetWhereClause( "orderStatus = 'delivery pending'" );
queryBuilder.SetPageSize( 100 );
// fetch the order objects with the query.
OpResult pendingOrdersResult = unitOfWork.Find( "Order", queryBuilder );
// compose the changes object. The change it
// has it to update the "orderStatus" column
// with the value "completed"
Dictionary<String, Object> changes = new Dictionary<String, Object>();
changes[ "orderStatus" ] = "completed";
// add the "BulkUpdate" operation to the transaction
// apply the "changes" to the result of the "Find" operation
unitOfWork.BulkUpdate( pendingOrdersResult, changes );
// run the transaction
unitOfWork.Execute( new AsyncCallback<UnitOfWorkResult>(
callback =>
{
//Transaction completed
},
fault =>
{
//Server reported an error
} ) );
This is what the objects look like in the database after the transaction runs: