Skip to content

Commit e1256bc

Browse files
committed
Better RelatedRecord virtual field
1 parent 5c90472 commit e1256bc

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/PHPFUI/ORM/RelatedRecord.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22

33
namespace PHPFUI\ORM;
44

5+
/**
6+
* get a related record
7+
*/
58
class RelatedRecord extends \PHPFUI\ORM\VirtualField
69
{
710
/**
8-
* @param array<string> $parameters
11+
* @param array<string> $parameters recordClassName fieldName
912
*/
1013
public function getValue(array $parameters) : mixed
1114
{
1215
$class = \array_shift($parameters);
16+
$field = \array_shift($parameters);
17+
18+
if ($field)
19+
{
20+
return new $class($this->currentRecord->$field);
21+
}
1322

1423
return new $class($this->currentRecord[$this->fieldName . \PHPFUI\ORM::$idSuffix]);
1524
}
@@ -20,18 +29,25 @@ public function getValue(array $parameters) : mixed
2029
public function setValue(mixed $value, array $parameters) : void
2130
{
2231
$class = \array_shift($parameters);
32+
$field = \array_shift($parameters);
2333

2434
if (! ($value instanceof $class))
2535
{
2636
throw new \PHPFUI\ORM\Exception(__METHOD__ . ': Error - ' . \get_debug_type($value) . ' is not an instance of ' . $class);
2737
}
38+
2839
$primaryKeyValues = $value->getPrimaryKeyValues();
2940

3041
if (1 != \count($primaryKeyValues))
3142
{
3243
throw new \PHPFUI\ORM\Exception(__METHOD__ . ': Error - ' . \get_debug_type($value) . ' does not have a single primary key');
3344
}
3445

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);
3652
}
3753
}

0 commit comments

Comments
 (0)