CRUD Operations with the OData SDK for PHP

Last week I wrote a post about retrieving data from an OData feed by using classes that were generated by the OData SDK for PHP. This week I will go one step farther and look at how to perform CREATE, UPDATE, and DELETE operations (CRUD operations) with the generated classes. Of course, this means that I need access to a read-write service, so before writing PHP code for CRUD operations, I’ll walk you through the steps for creating a service that implements the OData protocol.

But first, the requirements…



I want to make note of a couple of things before starting in hopes of saving you some frustration as you work through this post. Note that there is a free option for each required Microsoft product in this list:

  1. You will need SQL Sever Express with the Northwind example database installed.
  2. You will need Visual Web Developer 2008 Express (free), Visual Studio 2008 SP1, Visual Web Developer 2010 Express, or Visual Studio 2010.
  3. You will need to enable the cURL and XSL extensions in your PHP installation.


Creating an OData Service

To create an OData service that is based on the Northwind example database, follow these steps:

1. If you have Visual Web Developer 2008 Express or Visual Studio 2008 SP1, follow the instructions in the Quickstart here: If you have Visual Web Developer 2010 Express or Visual Studio 2010, you can follow the Quickstart here:

2. After completing the Quickstart, change the project properties so that the service is handled by the local Web server, not the development sever that is part of Visual Studio. (If you are on Vista or Windows 7, you will need to restart Visual Studio with Administrator privileges to do this.)

     a. Right-click the project name in Solution Explorer and select Properties.

     b. Click the Web tab on the Properties page, select Use Local IIS Web Server, and click Create Virtual Directory.


At this point, you should be able to access the service metadata with the following URL: http://localhost/NorthwindService/Northwind.svc/$metadata. You should also be able to access Customers data with the following URL: http://localhost/NorthwindService/Northwind.svc/Customers, but note that accessing this data requires that the identity under which your Web Server is running must map to a valid SQL Server login. (For more information, check out one of my past posts: Understanding Windows Authentication.)


Generating Proxy Classes with the OData SDK for PHP

Now that I have an OData service, I need to generate proxy classes that allow me to write clean code to consume the service. To do that, I follow these steps (which are essentially the same steps in last week’s post):

1. Download the OData SDK for PHP here:

2. Follow the directions in the Installation and Configuration section of the User_Guide.htm file (in the doc directory of the SDK download).

Note: There is a small mistake in the user guide. You will need to capitalize the library path variable that you add to your php.ini file (step 4 in the Installation and Configuration section):

     ;Odata SDK for PHP Library Path
ODataphp_path = "C:\PHPLib\odataphp"

3. Don’t forget to re-start your Web server after making changes to your php.ini file.

4. (Optional) Add your PHP installation directory to your PATH environment variable. This will allow you to run PHP scripts from any directory.

Now I can generate a file (NorthwindProxies.php) that contains my proxy classes with the PHPDataSvcUtil.php tool from the command line:

php C:\PHPLib\odataphp\PHPDataSvcUtil.php /uri=http://localhost/NorthwindService/Northwind.svc /out=C:\PHPLib\odataphp\NorthwindProxies.php

This will generate the NorthwindProxies.php file in the C:\PHPLib\odataphp directory.


CRUD Operations with the Generated Classes

The generated NorthwindEntities class manages all the in-memory objects that represent tables, rows, relationships, etc. in the database. So to create a new object, I just create a new customer with the static CreateCustomers method, add it to the in-memory collection of Customers, then save the changes I’ve made to the in-memory collection:

$proxy = new NorthwindEntities();

//Create a Customer with the static CreateCustomers method. The parameters are CustomerID and CompanyName
$customer = Customers::CreateCustomers("BRIAN", "Brian’s Diner");

//Add the new customer to the in-memory collection of Customers
$proxy->AddObject('Customers', $customer);

//Save the in-memory changes to the database.

Updating is similar. Note that if an object is already in the in-memory collection, I don’t have to execute a query to get it. This code assumes that the object I want to update is not already in the in-memory collection:

$proxy = new NorthwindEntities();

//Get the object to be updated.
$response = $proxy->Execute ("Customers('BRIAN')");
$customer = $response->Result[0];

//Update the CompanyName property
$customer->CompanyName = "Microsoft";

//Add the object to the list of objects that need to be updated

//Save changes to the database

No surprises when deleting an object:

$proxy = new NorthwindEntities();

//Get the object to delete.
$response = $proxy->Execute ("Customers('BRIAN')");
$customer = $response->Result[0];

//The object is added to the list of objects to delete in the database

//SaveChanges persists the changes in the database

That’s it. And once again, I’ve revealed only a small part of the functionality that is available in the SDK. You can investigate more on your own by looking at the user guide (User_Guide.htm in the doc directory of the SDK download). If you have questions or if there is something you’d like to learn more about, please let me know.



Share this on Twitter