Skip to content

Commit c3d8bd0

Browse files
committed
Delete Children on delete
1 parent 822de6c commit c3d8bd0

File tree

8 files changed

+55
-44
lines changed

8 files changed

+55
-44
lines changed

Tests/Fixtures/Record/Order.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class Order extends \Tests\Fixtures\Record\Definition\Order
66
{
77
protected static array $virtualFields = [
8-
'OrderDetailChildren' => [\PHPFUI\ORM\Children::class, \Tests\Fixtures\Table\OrderDetail::class, 'order_detail_id'],
8+
'orderDetailChildren' => [\PHPFUI\ORM\Children::class, \Tests\Fixtures\Table\OrderDetail::class, 'order_detail_id'],
99
'order_date' => [\PHPFUI\ORM\Cast::class, \Carbon\Carbon::class],
1010
'paid_date' => [\PHPFUI\ORM\Cast::class, \Carbon\Carbon::class],
1111
'shipped_date' => [\PHPFUI\ORM\Cast::class, \Carbon\Carbon::class],

Tests/Unit/ChildrenTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public function testChildren() : void
88
{
99
$order = new \Tests\Fixtures\Record\Order(44);
1010
$this->assertTrue($order->loaded());
11-
$orderDetails = $order->OrderDetailChildren;
11+
$orderDetails = $order->orderDetailChildren;
1212
$this->assertCount(3, $orderDetails);
1313
$this->assertEquals(25.0 * 18.0, $orderDetails->current()->gross);
1414
}

Tests/Unit/DeleteTest.php

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,20 @@ public function testRecordDelete() : void
1717
$this->assertEquals(29, $table->count());
1818
}
1919

20+
public function testDeleteChildren() : void
21+
{
22+
$order = new \Tests\Fixtures\Record\Order(31);
23+
$this->assertCount(3, $order->orderDetailChildren);
24+
$transaction = new \PHPFUI\ORM\Transaction();
25+
$orderDetailTable = new \Tests\Fixtures\Table\OrderDetail();
26+
$orderDetailTable->setWhere(new \PHPFUI\ORM\Condition('order_id', 31));
27+
$this->assertCount(3, $orderDetailTable);
28+
$order->delete();
29+
$this->assertCount(0, $orderDetailTable);
30+
$this->assertTrue($transaction->rollBack());
31+
$this->assertCount(3, $orderDetailTable);
32+
}
33+
2034
public function testTableDelete() : void
2135
{
2236
$table = new \Tests\App\Table\Customer();

docs/5. Virtual Fields.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,15 +83,17 @@ class Order extends \Tests\App\Record\Definition\Order
8383
{
8484
protected static array $virtualFields = [
8585
// the OrderDetailChildren will be returned in order_detail_id order. Leave off the third array element to let SQL determine the order if you don't care.
86-
'OrderDetailChildren' => [\PHPFUI\ORM\Children::class, \Tests\App\Table\OrderDetail::class, 'order_detail_id'],
86+
'orderDetailChildren' => [\PHPFUI\ORM\Children::class, \Tests\App\Table\OrderDetail::class, 'order_detail_id'],
8787
];
8888
}
8989
```
9090

91+
By default, child records will be automatically deleted when the parent record is deleted. You can disable this functionality for a specific record class by setting the static property $deleteChildren to false, or using your own Children class.
92+
9193
### Usage
9294
```php
9395
$order = new \App\Record\Order(31);
94-
foreach ($order->OrderDetailChildren as $orderDetail)
96+
foreach ($order->orderDetailChildren as $orderDetail)
9597
{
9698
echo "Gross {$orderDetail->gross} for product {$orderDetail->product->product_name}\n";
9799
}

src/PHPFUI/ORM/Children.php

+21-12
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,23 @@
1212
* Example:
1313
* ```php
1414
* protected static array $virtualFields = [
15-
* 'OrderDetailChildren' => [\PHPFUI\ORM\Children::class, \Tests\App\Table\OrderDetail::class, 'data_allocated', 'desc'],
15+
* 'orderDetailChildren' => [\PHPFUI\ORM\Children::class, \Tests\App\Table\OrderDetail::class, 'data_allocated', 'desc'],
1616
* ];
1717
* ```
1818
*/
1919
class Children extends \PHPFUI\ORM\VirtualField
2020
{
21+
public function delete(array $parameters) : void
22+
{
23+
$this->getTable(\array_shift($parameters))->delete();
24+
}
25+
2126
/**
22-
* @param array<string, string[]> $parameters containing **\PHPFUI\ORM\Children::class** followed by the child table, then the optional parameters of an order by column and sort order (defaults to ASC).
27+
* @param array<string, string> $parameters containing **\PHPFUI\ORM\Children::class** followed by the child table, then the optional parameters of an order by column and sort order (defaults to ASC).
2328
*/
2429
public function getValue(array $parameters) : mixed
2530
{
26-
$child = \array_shift($parameters);
27-
$childTable = new $child();
28-
$condition = new \PHPFUI\ORM\Condition();
29-
30-
foreach ($this->currentRecord->getPrimaryKeys() as $primaryKey => $junk)
31-
{
32-
$condition->and($primaryKey, $this->currentRecord->{$primaryKey});
33-
}
34-
$childTable->setWhere($condition);
35-
31+
$childTable = $this->getTable(\array_shift($parameters));
3632
$orderBy = \array_shift($parameters);
3733
$sort = \array_shift($parameters) ?? 'asc';
3834

@@ -43,4 +39,17 @@ public function getValue(array $parameters) : mixed
4339

4440
return $childTable->getRecordCursor();
4541
}
42+
43+
protected function getTable(string $class) : \PHPFUI\ORM\Table
44+
{
45+
$childTable = new $class();
46+
$condition = new \PHPFUI\ORM\Condition();
47+
48+
foreach ($this->currentRecord->getPrimaryKeys() as $primaryKey => $junk)
49+
{
50+
$condition->and($primaryKey, $this->currentRecord->{$primaryKey});
51+
}
52+
53+
return $childTable->setWhere($condition);
54+
}
4655
}

src/PHPFUI/ORM/Migration.php

+1
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@ private function alter(string $type, string $table, string $field, string $extra
487487
private function getFieldInfo(string $table, string $fieldName) : ?\PHPFUI\ORM\Schema\Field
488488
{
489489
$fields = \PHPFUI\ORM::describeTable($table);
490+
490491
foreach ($fields as $field)
491492
{
492493
if ($field->name == $fieldName)

src/PHPFUI/ORM/Record.php

+8-23
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ abstract class Record extends DataObject
2727

2828
protected static bool $autoIncrement = false;
2929

30+
protected static bool $deleteChildren = true;
31+
3032
protected static array $displayTransforms = [];
3133

3234
protected bool $empty = true;
@@ -284,19 +286,16 @@ public function create() : int
284286
*/
285287
public function delete() : bool
286288
{
287-
// delete child records for primary key only
288-
if (1 == \count(static::$primaryKeys))
289+
if (static::$deleteChildren)
289290
{
290-
$primaryKey = \array_key_first(static::$primaryKeys);
291-
292-
foreach (static::$virtualFields as $relationship => $status)
291+
foreach (static::$virtualFields as $field => $relationship)
293292
{
294-
$childTable = $this->getChildTable($relationship);
293+
$relationshipClass = \array_shift($relationship);
295294

296-
if ($childTable)
295+
if (\PHPFUI\ORM\Children::class == $relationshipClass)
297296
{
298-
$childTable->setWhere(new \PHPFUI\ORM\Condition($primaryKey, $this->current[$primaryKey]));
299-
$childTable->delete();
297+
$relationshipObject = new \PHPFUI\ORM\Children($this, $field);
298+
$relationshipObject->delete($relationship);
300299
}
301300
}
302301
}
@@ -828,20 +827,6 @@ private function buildWhere(array|int|string $key, array &$input) : string
828827
return $sql;
829828
}
830829

831-
private function getChildTable(string $relationship) : ?\PHPFUI\ORM\Table
832-
{
833-
$children = \str_ends_with($relationship, 'Children');
834-
835-
if (! $children)
836-
{
837-
return null;
838-
}
839-
$recordType = \substr($relationship, 0, \strlen($relationship) - 8);
840-
$type = '\\' . \PHPFUI\ORM::$tableNamespace . '\\' . $recordType;
841-
842-
return new $type();
843-
}
844-
845830
/**
846831
* Inserts current data into table
847832
*

src/PHPFUI/ORM/Table.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -173,11 +173,6 @@ public function addJoin(string $table, string | \PHPFUI\ORM\Condition $on = '',
173173
return $this;
174174
}
175175

176-
public function cleanField(string $fieldName) : string
177-
{
178-
return \preg_replace('/[^[a-zA-Z_][a-zA-Z0-9_.$@-]{0,63}$]/', '', $fieldName); // string invalid characters since we can't use a placeholder in order and group by
179-
}
180-
181176
public function addOrderBy(string $field, string $ascending = 'ASC') : static
182177
{
183178
if (\strlen($field))
@@ -265,6 +260,11 @@ public static function capitalSplit(string $key) : string
265260
return $output;
266261
}
267262

263+
public function cleanField(string $fieldName) : string
264+
{
265+
return \preg_replace('/[^[a-zA-Z_][a-zA-Z0-9_.$@-]{0,63}$]/', '', $fieldName); // string invalid characters since we can't use a placeholder in order and group by
266+
}
267+
268268
/**
269269
* Returns the count for the limited query.
270270
*/

0 commit comments

Comments
 (0)