Skip to content

Commit fd57b01

Browse files
committed
Related Record Virtual field
1 parent eb20be7 commit fd57b01

File tree

5 files changed

+21
-8
lines changed

5 files changed

+21
-8
lines changed

Tests/Fixtures/Virtual/Gross.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ class Gross extends \PHPFUI\ORM\VirtualField
66
{
77
public function getValue(array $parameters) : mixed
88
{
9-
return \number_format($this->parentRecord->unit_price * $this->parentRecord->quantity - $this->parentRecord->discount, 2);
9+
return \number_format($this->currentRecord->unit_price * $this->currentRecord->quantity - $this->currentRecord->discount, 2);
1010
}
1111
}

src/PHPFUI/ORM/Children.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public function getValue(array $parameters) : mixed
2727
$childTable = new $child();
2828
$condition = new \PHPFUI\ORM\Condition();
2929

30-
foreach ($this->parentRecord->getPrimaryKeys() as $primaryKey => $junk)
30+
foreach ($this->currentRecord->getPrimaryKeys() as $primaryKey => $junk)
3131
{
32-
$condition->and($primaryKey, $this->parentRecord->{$primaryKey});
32+
$condition->and($primaryKey, $this->currentRecord->{$primaryKey});
3333
}
3434
$childTable->setWhere($condition);
3535

src/PHPFUI/ORM/ManyToMany.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public function getValue(array $parameters) : mixed
3333
$relatedTable->addJoin($junctionTableName, $relatedTableName . \PHPFUI\ORM::$idSuffix);
3434
$condition = new \PHPFUI\ORM\Condition();
3535

36-
foreach ($this->parentRecord->getPrimaryKeys() as $primaryKey => $junk)
36+
foreach ($this->currentRecord->getPrimaryKeys() as $primaryKey => $junk)
3737
{
38-
$condition->and($junctionTableName . '.' . $primaryKey, $this->parentRecord->{$primaryKey});
38+
$condition->and($junctionTableName . '.' . $primaryKey, $this->currentRecord->{$primaryKey});
3939
}
4040

4141
foreach ($relatedTable->getPrimaryKeys() as $primaryKey => $junk)

src/PHPFUI/ORM/RelatedRecord.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace PHPFUI\ORM;
4+
5+
class RelatedRecord extends \PHPFUI\ORM\VirtualField
6+
{
7+
public function getValue(array $parameters) : mixed
8+
{
9+
$class = \array_shift($parameters);
10+
11+
return new $class($this->currentRecord[$this->fieldName . \PHPFUI\ORM::$idSuffix]);
12+
}
13+
}

src/PHPFUI/ORM/VirtualField.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,17 @@
44

55
abstract class VirtualField
66
{
7-
public function __construct(protected \PHPFUI\ORM\Record $parentRecord, protected string $fieldName)
7+
public function __construct(protected \PHPFUI\ORM\Record $currentRecord, protected string $fieldName)
88
{
99
}
1010

1111
public function getValue(array $parameters) : mixed
1212
{
13-
throw new \PHPFUI\ORM\Exception("get not defined for {$this->parentRecord->getTableName()}.{$this->fieldName}");
13+
throw new \PHPFUI\ORM\Exception("get not defined for {$this->currentRecord->getTableName()}.{$this->fieldName}");
1414
}
1515

1616
public function setValue(mixed $value, array $parameters) : void
1717
{
18-
throw new \PHPFUI\ORM\Exception("set not defined for {$this->parentRecord->getTableName()}.{$this->fieldName}");
18+
throw new \PHPFUI\ORM\Exception("set not defined for {$this->currentRecord->getTableName()}.{$this->fieldName}");
1919
}
2020
}

0 commit comments

Comments
 (0)