forked from mekras/odata-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_entry.php
59 lines (51 loc) · 1.98 KB
/
create_entry.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<?php
/**
* This example should be executed in console
*
* Requires dev dependencies!
*/
namespace Comindware\Tracker\API\Examples;
use Http\Discovery\HttpClientDiscovery;
use Http\Discovery\MessageFactoryDiscovery;
use Mekras\OData\Client\Document\EntryDocument;
use Mekras\OData\Client\EDM\Primitive;
use Mekras\OData\Client\OData;
use Mekras\OData\Client\URI\Uri;
use Mekras\OData\Client\Service;
require __DIR__ . '/../../vendor/autoload.php';
// For write access to services.odata.org we need not get special URI first
$httpClient = HttpClientDiscovery::find();
$requestFactory = MessageFactoryDiscovery::find();
$response = $httpClient->sendRequest(
$requestFactory->createRequest(
'GET',
'http://services.odata.org/V3/(S(readwrite))/OData/OData.svc/'
)
);
$rootUri = 'http://services.odata.org' . $response->getHeaderLine('Location');
// Now use obtained URI to create service.
$service = new Service($rootUri, $httpClient, $requestFactory);
// Create container document. "ODataDemo.Product" — entity type we want to create.
$document = $service->getDocumentFactory()->createEntityDocument('ODataDemo.Product');
$entry = $document->getEntry();
$entry->addTitle('Foo');
$entry->getProperties()->add('ID', mt_rand(50, 10000), Primitive::INT32);
$entry->getProperties()->add('ReleaseDate', new \DateTime(), Primitive::DATETIME);
$entry->getProperties()->add('Rating', 4, Primitive::INT16);
$entry->getProperties()->add('Price', 14.5, Primitive::DOUBLE);
$uri = new Uri();
$uri->collection('Products');
// Send request to server.
$document = $service->sendRequest(OData::CREATE, $uri, $document);
if (!$document instanceof EntryDocument) {
die("Invalid response!\n");
}
// Let see our newly created entity.
$entry = $document->getEntry();
foreach ($entry->getProperties() as $property) {
$value = $property->getValue();
if ($value instanceof \DateTimeInterface) {
$value = $value->format(DATE_RFC2822);
}
printf("%s: %s\n", $property->getName(), $value);
}