File tree 1 file changed +18
-2
lines changed
1 file changed +18
-2
lines changed Original file line number Diff line number Diff line change 2
2
3
3
namespace PHPFUI \ORM ;
4
4
5
+ /**
6
+ * get a related record
7
+ */
5
8
class RelatedRecord extends \PHPFUI \ORM \VirtualField
6
9
{
7
10
/**
8
- * @param array<string> $parameters
11
+ * @param array<string> $parameters recordClassName fieldName
9
12
*/
10
13
public function getValue (array $ parameters ) : mixed
11
14
{
12
15
$ class = \array_shift ($ parameters );
16
+ $ field = \array_shift ($ parameters );
17
+
18
+ if ($ field )
19
+ {
20
+ return new $ class ($ this ->currentRecord ->$ field );
21
+ }
13
22
14
23
return new $ class ($ this ->currentRecord [$ this ->fieldName . \PHPFUI \ORM ::$ idSuffix ]);
15
24
}
@@ -20,18 +29,25 @@ public function getValue(array $parameters) : mixed
20
29
public function setValue (mixed $ value , array $ parameters ) : void
21
30
{
22
31
$ class = \array_shift ($ parameters );
32
+ $ field = \array_shift ($ parameters );
23
33
24
34
if (! ($ value instanceof $ class ))
25
35
{
26
36
throw new \PHPFUI \ORM \Exception (__METHOD__ . ': Error - ' . \get_debug_type ($ value ) . ' is not an instance of ' . $ class );
27
37
}
38
+
28
39
$ primaryKeyValues = $ value ->getPrimaryKeyValues ();
29
40
30
41
if (1 != \count ($ primaryKeyValues ))
31
42
{
32
43
throw new \PHPFUI \ORM \Exception (__METHOD__ . ': Error - ' . \get_debug_type ($ value ) . ' does not have a single primary key ' );
33
44
}
34
45
35
- $ this ->currentRecord [$ this ->fieldName . \PHPFUI \ORM ::$ idSuffix ] = \array_shift ($ primaryKeyValues );
46
+ if (! $ field )
47
+ {
48
+ $ field = $ this ->fieldName . \PHPFUI \ORM ::$ idSuffix ;
49
+ }
50
+
51
+ $ this ->currentRecord [$ field ] = \array_shift ($ primaryKeyValues );
36
52
}
37
53
}
You can’t perform that action at this time.
0 commit comments