Skip to content

Commit 49d85cd

Browse files
committed
Add relationship data structure helpers.
1 parent dc89d1b commit 49d85cd

File tree

3 files changed

+126
-15
lines changed

3 files changed

+126
-15
lines changed

src/HasModelDataTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ public function getData()
3535

3636
/**
3737
* Get a data field using a "dot notation" path.
38+
*
39+
* @inherit
3840
*/
39-
public function get($key, $default = null)
41+
public function get(string $key, $default = null)
4042
{
4143
// Since we are running under laravel, use laravel's helper.
4244

src/ModelInterface.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,12 @@
1010

1111
interface ModelInterface extends JsonSerializable
1212
{
13-
public function get($key, $default = null);
13+
/**
14+
* Get a model instance data item, using "dot" notation.
15+
*
16+
* @param string $key example 'parent_ids.2'
17+
* @param mixed $defuault
18+
* @returns mixed
19+
*/
20+
public function get(string $key, $default = null);
1421
}

src/OdooClient.php

Lines changed: 115 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,32 +46,23 @@ class OdooClient
4646
// (1, id, values)
4747
const RELATION_UPDATE = 1;
4848
//
49-
// Removes the record of id `id` from the set, then deletes it
50-
// (from the database).
51-
// Can not be used in create().
49+
// Removes the record of id `id` from the set, then deletes it.
5250
// (2, id, _)
5351
const RELATION_DELETE = 2;
5452
//
5553
// Removes the record of id `id` from the set, but does not delete it.
56-
// Can not be used on One2many. Can not be used in create().
5754
// (3, id, _)
5855
const RELATION_REMOVE_LINK = 3;
5956
//
60-
// Adds an existing record of id `id` to the set. Can not be used on One2many.
57+
// Adds an existing record of id `id` to the set.
6158
// (4, id, _)
6259
const RELATION_ADD_LINK = 4;
6360
//
64-
// Removes all records from the set, equivalent to using the
65-
// command 3 on every record explicitly.
66-
// Can not be used on One2many.
67-
// Can not be used in create().
61+
// Removes all records from the set.
6862
// (5, _, _)
6963
const RELATION_REMOVE_ALL_LINKS = 5;
7064
//
71-
// Replaces all existing records in the set by the ids list,
72-
// equivalent to using the command 5 followed by a command 4
73-
// for each id in ids.
74-
// Can not be used on One2many.
65+
// Replaces all existing links with a new set.
7566
// (6, _, ids)
7667
const RELATION_REPLACE_ALL_LINKS = 6;
7768

@@ -685,6 +676,7 @@ public function removeModelMapping(string $modelName)
685676
/**
686677
* Return a message with the base parameters for any object call.
687678
* Identified the login credentials, model and action.
679+
* TODO: this should go in the connector factory.
688680
*
689681
* @param string|null $modelName
690682
* @param string|null $action will be used only the $modelName is provided
@@ -711,6 +703,116 @@ public function getBaseObjectRequest(
711703
return $msg;
712704
}
713705

706+
/**
707+
* Adds a new record created from the provided value dict.
708+
*
709+
* @param array $values
710+
* @return array
711+
*/
712+
public function relationCreate(array $values)
713+
{
714+
return [[
715+
static::RELATION_CREATE, 0, $values
716+
]];
717+
}
718+
719+
/**
720+
* Updates an existing record of id `id` with the values in values.
721+
* Can not be used in create().
722+
*
723+
* @param int $resourceId the resource to update
724+
* @param array $values
725+
* @return array
726+
*
727+
* TODO: as well as an array of values, accept a model.
728+
* The model, ideally, would be able to provide a list of all the
729+
* fields that have changed so that only those are updated.
730+
* Extending that into relations within the field list would be
731+
* a bit more involved though.
732+
*/
733+
public function relationUpdate(int $resourceId, array $values)
734+
{
735+
return [[
736+
static::RELATION_UPDATE, $resourceId, $values
737+
]];
738+
}
739+
740+
/**
741+
* Removes the record of id `id` from the set, then deletes it
742+
* (from the database).
743+
* Can not be used in create().
744+
*
745+
* @param int $resourceId the resource to be removed from the database
746+
* @return array
747+
*/
748+
public function relationDelete(int $resourceId)
749+
{
750+
return [[
751+
static::RELATION_DELETE, $resourceId, 0
752+
]];
753+
}
754+
755+
/**
756+
* Removes the record of id `id` from the set, but does not delete it.
757+
* Can not be used on one2many.
758+
* Can not be used in create().
759+
*
760+
* @param int $resourceId the resource to be removed from the link
761+
* @return array
762+
*/
763+
public function relationRemoveLink(int $resourceId)
764+
{
765+
return [[
766+
static::RELATION_REMOVE_LINK, $resourceId, 0
767+
]];
768+
}
769+
770+
/**
771+
* Creates data structure for setting relationship details.
772+
* Adds an existing record of id `id` to the set.
773+
* Can not be used on one2many.
774+
*
775+
* @param int $resourceId the resource to be added to the link
776+
* @return array
777+
*/
778+
public function relationAddLink(int $resourceId)
779+
{
780+
return [[
781+
static::RELATION_ADD_LINK, $resourceId, 0
782+
]];
783+
}
784+
785+
/**
786+
* Removes all records from the set, equivalent to using the
787+
* command 3 on every record explicitly.
788+
* Can not be used on one2many.
789+
* Can not be used in create().
790+
*
791+
* @return array
792+
*/
793+
public function relationRemoveAllLinks()
794+
{
795+
return [[
796+
static::RELATION_REMOVE_ALL_LINKS, 0, 0
797+
]];
798+
}
799+
800+
/**
801+
* Replaces all existing records in the set by the ids list,
802+
* equivalent to using the command 5 followed by a command 4
803+
* for each id in ids.
804+
* Can not be used on one2many.
805+
*
806+
* @return array
807+
*/
808+
public function relationReplaceAllLinks(iterator $resourceIds)
809+
{
810+
return [[
811+
static::RELATION_REPLACE_ALL_LINKS, 0, $resourceIds
812+
]];
813+
}
814+
815+
714816
/**
715817
* Walk through the criteria array and convert scalar values to
716818
* XML-RPC objects, and nested arrays to array and struct objects.

0 commit comments

Comments
 (0)