Skip to content

Commit

Permalink
Added support for relations (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonrietdijk authored Nov 20, 2023
1 parent a4f8bd4 commit a5a1b2d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,22 @@ $customers = Customer::query()

See the `QueryBuilder` class for all available methods.

## Relations

Any relations published on a page can be accessed as well using the resource.

```php
$salesOrder = SalesOrder::query()->first();

// Get the lines via the "relation" method.
$salesLines = $salesOrder->relation('Relation_Name', SalesLine::class)->get();

// Or use the "lines" helper on the SalesOrder.
$salesLines = $salesOrder->lines('Relation_Name')->get();
```

Note that the `relation` method itself returns an instance of a query builder. This means that you can add additional where-clauses like you would be able to on a regular resource.

## Creating records

Create a new record.
Expand Down
5 changes: 5 additions & 0 deletions src/OData/BaseResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ public function newQuery(): QueryBuilder
return new QueryBuilder($this->client(), $this->connection, $this->endpoint, static::class);
}

public function relation(string $relation, string $class): QueryBuilder
{
return new QueryBuilder($this->client(), $this->connection, $this->getResourceUrl().'/'.$relation, $class);
}

public static function fake(): void
{
foreach (config('dynamics.connections') as $connection => $data) {
Expand Down
6 changes: 6 additions & 0 deletions src/OData/Pages/SalesOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
namespace JustBetter\DynamicsClient\OData\Pages;

use JustBetter\DynamicsClient\OData\BaseResource;
use JustBetter\DynamicsClient\Query\QueryBuilder;

class SalesOrder extends BaseResource
{
public array $primaryKey = [
'Document_Type',
'No',
];

public function lines(string $relation): QueryBuilder
{
return $this->relation($relation, SalesLine::class);
}
}

0 comments on commit a5a1b2d

Please sign in to comment.