Skip to content

Manipulate zuora objects and their relationships without writing a single line of ZOQL.

License

Notifications You must be signed in to change notification settings

spira/Zuora-Orm

 
 

Repository files navigation

Zuora ORM

StyleCI Build Status Latest Stable Version Total Downloads Latest Unstable Version License

Introduction

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.

Basic Usage

Retrieving All Models

$accounts = Account::all();

Retrieving A Record By Primary Key

$account = account::find(1);

var_dump($account->Name);

Querying Using Zuora ORM Models

$accounts = Account::where('Status', '=', 'Active')->get();

foreach ($accounts as $account)
{
	var_dump($account->name);
}

Zuora ORM Aggregates

Of course, you may also use the query builder aggregate functions.

$count = Account::where('Status', '=', 100)->get()->count();

Insert, Update, Delete

Using The Model Create Method

// Create a new user in the database...
$account = Account::create(array('name' => 'my test account'));

Updating A Retrieved Model

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();

Deleting An Existing Model

To delete a model, simply call the delete method on the instance:

$account = Account::find(1);

$account->delete();

Relationships

One To One

Retrieve A One To One Relation

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

One To Many

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();

Querying Relations

Querying Relations When Selecting

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; });

Dynamic Properties

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.

Collections

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.

Iterating Collections

Zuora ORM collections also contain a few helpful methods for looping and filtering the items they contain:

Filtering Collections

When filtering collections, the callback provided will be used as callback for array_filter.

$accounts = $accounts->filter(function($account)
{
	return $account->hasSubcriptions();
});

Applying A Callback To Each Collection Object

$subscriptions = Account::find(1)->subscriptions;

$subscriptions->each(function($subscription)
{
	//
});

Sorting A Collection By A Value

$subscriptions = $subscriptions->sortBy(function($subscription)
{
	return $subscription->CreatedDate;
});

About

Manipulate zuora objects and their relationships without writing a single line of ZOQL.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%