diff --git a/src/Entity.php b/src/Entity.php index 7cb4ff4..c8a1aea 100644 --- a/src/Entity.php +++ b/src/Entity.php @@ -124,6 +124,9 @@ protected function setAttributes(array $attributes) } }; } + elseif (str_contains($key,'@odata.bind') ) { + $this->attributes[$key] = $attribute; + } } } @@ -212,9 +215,10 @@ public function save() } $response = $this->query->post($attributes); - $this->setAttributes($response); - $this->query->navigateTo($entity_set->name ?? Pluralizer::plural($this->type->schema_type), $this->identifiers()); - + if(!empty($response)) { + $this->setAttributes($response); + $this->query->navigateTo($entity_set->name ?? Pluralizer::plural($this->type->schema_type), $this->identifiers()); + } } $this->dirty = []; diff --git a/src/SDK.php b/src/SDK.php index 5b0a96f..5f02ea6 100644 --- a/src/SDK.php +++ b/src/SDK.php @@ -67,6 +67,12 @@ public static function instance(string $APIUri, string $tenant, array $options = 'logs_requests' => false, 'language' => false, 'target_dir' => __DIR__, + + // Default Restrictions + 'change_tracking_default' => false, + 'delete_restrictions_default' => false, + 'update_restrictions_default' => false, + 'insert_restrictions_default' => false, ]; protected function __construct($APIUri, $tenant, $options) @@ -208,7 +214,12 @@ public function mapEntities(): void $json = json_decode(json_encode(simplexml_load_string($raw), JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR); - $this->setSchema(new Schema($json)); + $this->setSchema(new Schema($json, + $this->option('change_tracking_default'), + $this->option('delete_restrictions_default'), + $this->option('update_restrictions_default'), + $this->option('insert_restrictions_default'), + )); $this->classMap = new ClassMap($this->option('target_dir')); } diff --git a/src/Schema.php b/src/Schema.php index 316e4e5..4fab1ac 100644 --- a/src/Schema.php +++ b/src/Schema.php @@ -29,11 +29,27 @@ class Schema /** @var Collection|Action[] */ protected $actions; + protected bool $DefaultChangeTracking; + protected bool $DefaultDeleteRestrictions; + protected bool $DefaultUpdateRestrictions; + protected bool $DefaultInsertRestrictions; + /** @var array */ protected $raw = [], $overrides; - public function __construct(array $json) + public function __construct( + array $json, + bool $DefaultChangeTracking = false, + bool $DefaultDeleteRestrictions = false, + bool $DefaultUpdateRestrictions = false, + bool $DefaultInsertRestrictions = false + ) { + $this->DefaultChangeTracking = $DefaultChangeTracking; + $this->DefaultDeleteRestrictions = $DefaultDeleteRestrictions; + $this->DefaultUpdateRestrictions = $DefaultUpdateRestrictions; + $this->DefaultInsertRestrictions = $DefaultInsertRestrictions; + $this->version = $json['@attributes']['Version']; // check array keys are numeric, then we have multiple schemas @@ -162,7 +178,7 @@ public static function getType(string $type) $type = $matches[1] ?? $type; } - $type = str_replace(['Microsoft.NAV.', 'NAV.', 'ComplexTypes.','mscrm.'], '', $type); + $type = str_replace(['Microsoft.NAV.', 'NAV.', 'ComplexTypes.','mscrm.', 'Microsoft.Dynamics.CRM.'], '', $type); return $type; } @@ -230,4 +246,24 @@ public function propertyIsNullable(string $model, string $property, $default = f { return $this->propertyIs($model, $property, 'nullable', $default); } + + public function isDefaultChangeTracking(): bool + { + return $this->DefaultChangeTracking; + } + + public function isDefaultDeleteRestrictions(): bool + { + return $this->DefaultDeleteRestrictions; + } + + public function isDefaultUpdateRestrictions(): bool + { + return $this->DefaultUpdateRestrictions; + } + + public function isDefaultInsertRestrictions(): bool + { + return $this->DefaultInsertRestrictions; + } } \ No newline at end of file diff --git a/src/Schema/EntitySet.php b/src/Schema/EntitySet.php index d85d2e2..4b16884 100644 --- a/src/Schema/EntitySet.php +++ b/src/Schema/EntitySet.php @@ -53,27 +53,27 @@ public function __get($name) public function is(string $capability) { - return $this->annotations[$capability] ?? false; + return $this->annotations[$capability] ?? null; } public function changeTracked() { - return $this->is('ChangeTracking'); + return $this->is('ChangeTracking') ?? $this->getSchema()->isDefaultChangeTracking(); } public function deletable() { - return $this->is('DeleteRestrictions'); + return $this->is('DeleteRestrictions') ?? $this->getSchema()->isDefaultDeleteRestrictions(); } public function updatable() { - return $this->is('UpdateRestrictions'); + return $this->is('UpdateRestrictions') ?? $this->getSchema()->isDefaultUpdateRestrictions(); } public function insertable() { - return $this->is('InsertRestrictions'); + return $this->is('InsertRestrictions') ?? $this->getSchema()->isDefaultInsertRestrictions(); } } \ No newline at end of file diff --git a/src/Schema/NavigationProperty.php b/src/Schema/NavigationProperty.php index 9f26d87..e0df2d9 100644 --- a/src/Schema/NavigationProperty.php +++ b/src/Schema/NavigationProperty.php @@ -41,7 +41,7 @@ public function __construct($property, Schema $schema) $this->schema = $schema; $this->name = $this->schema->getAliases()[$property['@attributes']['Name']] ?? $property['@attributes']['Name']; $this->route = $property['@attributes']['Name']; - $this->type = $property['@attributes']['Type']; + $this->type = Schema::getType($property['@attributes']['Type']); if ($this->isCollection()) { $this->name = Pluralizer::plural($this->name); @@ -61,7 +61,7 @@ public function getEntityType() public function convert($value, Builder $query) { if ($this->isCollection()) { - $entity_set = $this->schema->getEntitySet($this->name); + $entity_set = $this->schema->getEntitySetByType($this->type); $expands = $query->getExpands()[$entity_set->name] ?? null; if ($expands instanceof Builder) { @@ -74,13 +74,19 @@ public function convert($value, Builder $query) } $query->navigateTo($this->name) - ->setExpands($expands ?? []); + ->setExpands($expands ?? []); return new EntityCollection($query, $entity_set, $value); } else { - $entity_type = $this->schema->getEntityType($this->name); + $entity_type = $this->schema->getEntityType($this->type); - $query->navigateTo($this->name, $value['id']); + $keys = []; + foreach ($entity_type->keys as $key) + { + $keys[] = $value[$key]; + } + + $query->navigateTo($this->name, $keys); return new Entity($value, $query, $entity_type); }