The Zuora ORM provides a simple way for working with Zuora objects without writing ZOQL queries. Each zuora object has a corresponding "Model" which is used to interact with that object.
$accounts = Account::all();
$account = account::find(1);
var_dump($account->Name);
$accounts = Account::where('Status', '=', 'Active')->get();
foreach ($accounts as $account)
{
var_dump($account->name);
}
Of course, you may also use the query builder aggregate functions.
$count = Account::where('Status', '=', 100)->get()->count();
// Create a new user in the database...
$account = Account::create(array('name' => 'my test account'));
To update a model, you may retrieve it, change an attribute, and use the save
method:
$account = Account::find(1);
$account->status = 'Active';
$account->save();
To delete a model, simply call the delete
method on the instance:
$account = Account::find(1);
$account->delete();
For example, a Subscription
model have one Account
.
We may retrieve it using Zuora ORM's dynamic properties:
$account = Subscription::find(1)->account;
The SQL performed by this statement will be as follows:
select * from Subscription where Id = 1
select * from Account where Id = $subscription->Id
An example of a one-to-many relation is an account that "has many" subscriptions.
We can access the account's subscriptionss through the dynamic property:
$subscriptions = Account::find(1)->subscriptions;
If you need to add further constraints to which subscriptions are retrieved, you may call the subscriptions
method and continue chaining conditions:
$subscriptions = Account::find(1)->subscriptions()->where('Status', '=', 'Active')->get()->first();
When accessing the records for a model, you may wish to limit your results based on the existence of a relationship. For example, you wish to pull all accounts that have at least one subscription:
$accounts = Account::all()->filter(function($account) { return $account->subscription()->count() > 0; });
Zuora ORM allows you to access your relations via dynamic properties. Zuora ORM will automatically load the relationship for you, and is even smart enough to know whether to call the get
(for one-to-many relationships) or first
(for one-to-one relationships) method. It will then be accessible via a dynamic property by the same name as the relation.
$subscription = Subscription::find(1);
Instead of echoing the user's email like this:
echo $subscription->account()->get()->first()->Name;
It may be shortened to simply:
echo $subscription->account->Name;
Note: Relationships that return many results will return an instance of the
Illuminate\Support\Collection
class.
All multi-result sets returned by Zuora ORM, either via the get
method or a relationship
, will return a collection object. This object implements the IteratorAggregate
PHP interface so it can be iterated over like an array. However, this object also has a variety of other helpful methods for working with result sets.
Zuora ORM collections also contain a few helpful methods for looping and filtering the items they contain:
When filtering collections, the callback provided will be used as callback for array_filter.
$accounts = $accounts->filter(function($account)
{
return $account->hasSubcriptions();
});
$subscriptions = Account::find(1)->subscriptions;
$subscriptions->each(function($subscription)
{
//
});
$subscriptions = $subscriptions->sortBy(function($subscription)
{
return $subscription->CreatedDate;
});