Skip to content

Example Calls

Adam Anderly edited this page Sep 8, 2023 · 21 revisions

Usage Examples

Initialize the OData Client

    use SaintSystems\OData\ODataClient;

    // Using the example OData service from odata.org
    $odataServiceUrl = 'https://services.odata.org/V4/TripPinService';
    $odataClient = new ODataClient($odataServiceUrl);

Initialize the OData Client with an Authentication Delegate

    use SaintSystems\OData\ODataClient;

    // Need to authenticate the requests to the service? 
    // Pass in an Authentication Delegate Closure as the second argument
    // which will get passed the request object. The Closure will be
    // called prior to every request.

    // Access token retrieved from an oauth provider
    $accessToken = 'abc';
    $odataClient = new ODataClient($odataServiceUrl, function($request) {

        // OAuth Bearer Token Authentication
        $request->headers['Authorization'] = 'Bearer '.$accessToken;

        // OR Basic Authentication
        $username = 'foo';
        $password = 'bar';
        $request->headers['Authorization'] = 'Basic '.base64_encode($username.':'.$password);

    });

Get a collection of items

    // Retrieve all entities from the "People" Entity Set
    // Issues an HTTP GET request to http://services.odata.org/V4/TripPinService/People
    $people = $odataClient->from('People')->get();

Get a specific item from a collection by ID

    // Retrieve a specific entity by the Entity ID/Key
    // Issues an HTTP GET request to http://services.odata.org/V4/TripPinService/People('russellwhyte')
    $person = $odataClient->from('People')->find('russellwhyte');
    echo "Hello, I am $person->FirstName ";

Get a specific item from a collection by multi-part Entity ID/Composite Key

    // Retrieve a specific entity by the Entity multi-part ID/Key
    // Issues an HTTP GET request to http://services.odata.org/V4/TripPinService/People(username='russellwhyte',userid='1')
    $person = $odataClient->from('People')->find(['username' => 'russellwhyte', 'userid' => '1']);
    echo "Hello, I am $person->FirstName ";

Get only certain properties/columns from a collection

    // Select only the "FirstName" and "LastName" properties
    // Issues an HTTP GET request to http://services.odata.org/V4/TripPinService/People?$select=FirstName,LastName
    $people = $odataClient->from('People')->select('FirstName','LastName')->get();

Get a collection of items with an enum property

    // Retrieve all entities where gender is female
    $person = $odataClient->from('People')->where('Gender', '=', 'Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender\'Female\'')->get();

Top a collection

    // Return the first two entities from the "People" Entity Set
    $people = $odataClient->from('People')->take(2)->get();

Skip a collection

    // Skip the first two entities from the "People" Entity Set
    $people = $odataClient->from('People')->skip(2)->get();

Page through a collection

    // Request 8 records per page using the 'pageSize' method on the Builder
    // Call 'cursor' method which returns a LazyCollection generator
    $people = $odataClient->from('People')->pageSize(8)->cursor();

    $totalRecordCount = $people ->count(); // Will return 20, which is the total number of 'Person' records in the Person EntitySet

    $firstPerson = $people ->first(); // Will return the first person in the EntitySet (Russell Whyte)
    $lastPerson = $people ->last(); // Will return the last person in the EntitySet (Krista Kemp, from page 3 of the paginated results)

    // Skip the first 8 records (page 1) and get the first person on page 2 (the 9th Person)
    // This will issue a second request to the @odata.nextLink endpoint to retrieve the second page of results in order to get the 9th person in the EntitySet (Marshall Garay)
    $ninthPerson = $people ->skip(8)->first();

    // Iterating through the cursor LazyCollection will automatically issue 3 http requests
    // to the OData Endpoint as it's being iterated to retrieve all 3 pages (20 records) from the EntitySet
    $data->each(function($person) {
        echo $person->UserName;
    });

Filter a collection

    // Select only records whose FirstName = "Russell"
    // Issues an HTTP GET request to http://services.odata.org/V4/TripPinService/People?$filter=FirstName eq 'Russell'
    $people = $odataClient->from('People')->where('FirstName','=','Russell')->get();

    // Omitting the "=" operator defaults to "=" operator (synonymous with the request above)
    $people = $odataClient->from('People')->where('FirstName','Russell')->get();

    // Select records whose FirstName is Russel or whose LastName is Ketchum
    $people = $builder->from($entitySet)->where('FirstName','Russell')->orWhere('LastName','Ketchum')->get();

Sort a collection

    // Order all entities from the "People" Entity Set by Name (default asc)
    $people = $odataClient->from('People')->order('Name')->get();

    // Order all entities from the "People" Entity Set by descending Name
    $people = $odataClient->from('People')->order('Name', 'desc')->get();

    // Order all entities from the "People" Entity Set by asc ID and desc Name
    $people = $odataClient->from('People')->order(['Id', 'asc'], ['Name', 'desc'])->get();
    $people = $odataClient->from('People')->order(['column' => 'Id', 'direction' => 'asc'], ['column' => 'Name', 'direction' => 'desc'])->get();

    // Order all entities from the "People" Entity Set by asc ID and desc Name
    $order = array(['Id', 'asc'], ['Name', 'desc']);
    $order = array(['column' => 'Id', 'direction' => 'asc'], ['column' => 'Name', 'direction' => 'desc']);
    $people = $odataClient->from('People')->order($order)->get();

Expand a collection

    // Expand all entities from the "People" Entity by PersonGender
    $people = $odataClient->from('People')->expand('PersonGender')->get();

    // Expand all entities from the "People" Entity by PersonGender and PersonOccupation
    $people = $odataClient->from('People')->expand(['PersonGender', 'PersonOccupation'])->get();

Count a collection

    // Count all entities from the "People" Entity Set
    $countPeople = $odataClient->from('People')->count();

Chain Params

    // Retrieve a subset of entities from the "People" Entity Set
    $people = $odataClient->from($entitySet)
                          ->select('Name,Gender')
                          ->where('Gender', '=', 'Female')
                          ->order('Name', 'desc')
                          ->take(5);
                          ->get();