Skip to content

Commit

Permalink
Merge pull request #11 from viosys/feature/make-saving-to-cds-possible
Browse files Browse the repository at this point in the history
fix bugs that where introduced with the introduction of the crm api
  • Loading branch information
breaker92 authored May 24, 2024
2 parents 0aa389b + 1441aa4 commit b3c14f8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 16 deletions.
10 changes: 7 additions & 3 deletions src/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ protected function setAttributes(array $attributes)
}
};
}
elseif (str_contains($key,'@odata.bind') ) {
$this->attributes[$key] = $attribute;
}
}
}

Expand Down Expand Up @@ -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 = [];
Expand Down
13 changes: 12 additions & 1 deletion src/SDK.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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'));
}
Expand Down
40 changes: 38 additions & 2 deletions src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
}
10 changes: 5 additions & 5 deletions src/Schema/EntitySet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
16 changes: 11 additions & 5 deletions src/Schema/NavigationProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down

0 comments on commit b3c14f8

Please sign in to comment.