Getting an exception with no message when adding object

Sep 14, 2009 at 9:23 AM

I'm trying to get up to speed with the libraries here and write a couple samples against a previous northwind data service which I used to learn some Silverlight development with.  Everything worked find creating the proxies from the command line and even with my first php cases doing some fairly complex queries.  I can't do a simple customer insert, however, despite using almost the exact same code as described in the user guide.

Here's my php:

 

require_once 'northwinddb.php';
require_once 'svt_functions.php';
define("NORTHWIND_SERVICE_URL", "http://localhost:999/Northwind.svc");

$proxy = new NorthwindEntities(NORTHWIND_SERVICE_URL);

$newcid = rand_str(5);
$newCustomer = Customers::CreateCustomers($newcid, "company_".$newcid);
// $newCustomer->ContactName = "cont_".$newcid;
// $newCustomer->Address = "add_".$newcid;
// $newCustomer->City = "cty_".$newcid;
// $newCustomer->Region = "reg_".$newcid;
// $newCustomer->PostalCode = rand_str(5, "0123456789");
// $newCustomer->Phone = rand_str(7, "0123456789");
// $newCustomer->Fax = rand_str(7, "0123456789");

echo "<p>Adding Customer: ".$newCustomer->CustomerID."</p>\n";

try
{
  $proxy->AddObject($newCustomer, 'Customers');
  $proxy->SaveChanges();
} 
catch (Exception $e) 
{
    echo 'Caught exception: ',  $e->getMessage(), "\n";
    var_dump($e->getTrace());
}

And here's the output:

Adding Customer: vwbv6

Caught exception: array(2) { [0]=> array(6) { ["file"]=> string(61) "C:\dev\phpdataservices1.0\framework\Context\ObjectContext.php" ["line"]=> int(123) ["function"]=> string(30) "ThrowExceptionIfNotValidObject" ["class"]=> string(13) "ObjectContext" ["type"]=> string(2) "->" ["args"]=> array(2) { [0]=> string(9) "Customers" [1]=> string(9) "AddObject" } } [1]=> array(6) { ["file"]=> string(33) "C:\Apache2.2\htdocs\svt\index.php" ["line"]=> int(31) ["function"]=> string(9) "AddObject" ["class"]=> string(13) "ObjectContext" ["type"]=> string(2) "->" ["args"]=> array(2) { [0]=> object(Customers)#4 (17) { ["_entityMap":protected]=> array(1) { ["Orders"]=> string(6) "Orders" } ["_entityKey":protected]=> array(1) { [0]=> string(10) "CustomerID" } ["_relLinks":protected]=> array(0) { } ["_baseURI":protected]=> string(0) "" ["CustomerID"]=> string(5) "vwbv6" ["CompanyName"]=> string(13) "company_vwbv6" ["ContactName"]=> NULL ["ContactTitle"]=> NULL ["Address"]=> NULL ["City"]=> NULL ["Region"]=> NULL ["PostalCode"]=> NULL ["Country"]=> NULL ["Phone"]=> NULL ["Fax"]=> NULL ["Orders"]=> NULL ["_objectID":protected]=> string(36) "0cd11295-23cb-c76a-3862-84c1e8162dc5" } [1]=> string(9) "Customers" } } }

Any ideas?  I looked at the ObjectContext.php file at line 123, and I just can't find a way that an exception would be thrown without a message.  Thanks for the help

 

Rob

Coordinator
Sep 15, 2009 at 6:12 AM
Edited Sep 15, 2009 at 6:14 AM

Hi Rob,

The way you are invoking the AddObject function is wrong, Instead of

 $proxy->AddObject($newCustomer, 'Customers');

you should invoke it as:

$proxy->AddObject('Customers', $newCustomer);


The first argument of AddObject is the EntityType and second is the entity instance.

Thanks & Regards,

Anu



Sep 15, 2009 at 6:36 AM

You're absolutely right.. that works (as does using the ->AddToCustomers() method which I didn't see before)..

 

What's interesting is the that the user's guide is wrong.  copies from the user's guide:

 

require_once 'MyProxyClass.php';

$proxy = new NorthwindEntities();

/* Create a Customers php Object */

$customer = Customers::CreateCustomers("CHAN9", "channel9");

/* inserting Customers object context tracking system */

$proxy->AddObject($customer, ‘Customers’);

/*SaveChange insert the object into data service*/

$proxy->SaveChanges(); 

 


Coordinator
Sep 15, 2009 at 8:44 AM

Thanks Rob, I have updated User guide with correct usage of AddObject

Regards,

Anu