Skip to content

Commit ef5015a

Browse files
committed
Use trait to get all raw properties
This might be just temporary until some solutions comes out of cakephp/cakephp#8059.
1 parent 54b19c0 commit ef5015a

File tree

6 files changed

+55
-32
lines changed

6 files changed

+55
-32
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ class CooksTable extends Table {
5353
'assistant_chef' => 'App\Model\Entity\AssistantChef',
5454
]
5555
]);
56+
57+
// Optionally, set the default type. If none is defined, the
58+
// first one (i.e. `chef`) will be used.
59+
$this->entityClass('App\Model\Entity\AssistantChef');
5660
}
5761
}
5862
```
@@ -63,6 +67,21 @@ Then create a class for every type of entity:
6367
- Baker
6468
- AssistantChef
6569

70+
The entity that was previously defined to be the 'default' one will need to use the `StiAwareTrait`:
71+
72+
```
73+
<?php // src/Model/Entity/AssistantChef.php
74+
namespace App\Model\Entity;
75+
76+
use Cake\ORM\Entity;
77+
use Muffin\Model\Entity\StiAwareTrait;
78+
79+
class AssistantChef extends Entity
80+
{
81+
use StiAwareTrait;
82+
}
83+
```
84+
6685
Optionally, you can create classes for your tables that extend the parent table to encapsulate business logic:
6786

6887
```php

src/Model/Behavior/StiBehavior.php

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ class StiBehavior extends Behavior
2424
public function initialize(array $config)
2525
{
2626
$this->verifyConfig();
27+
28+
$defaultEntityClass = $this->_table()->entityClass();
29+
if ($defaultEntityClass === '\Cake\ORM\Entity') {
30+
$defaultEntityClass = current(Hash::extract($this->_typeMap, '{s}.entityClass'));
31+
$this->_table()->entityClass($defaultEntityClass);
32+
}
33+
34+
if (!method_exists($defaultEntityClass, 'forCopy')) {
35+
throw new \Exception();
36+
}
2737
}
2838

2939
public function verifyConfig()
@@ -84,37 +94,17 @@ public function beforeFind(Event $event, Query $query, ArrayObject $options, $pr
8494
return;
8595
}
8696

87-
$assocs = $this->_table()->associations()->keys();
88-
89-
$mapper = function ($row) use ($assocs) {
90-
foreach ($assocs as $k => $assoc) {
91-
unset($assocs[$k]);
92-
if ($row->has($assoc)) {
93-
$assocs[$assoc] = $row->$assoc;
94-
$row->unsetProperty($assoc);
95-
}
96-
}
97-
98-
$type = $row[$this->config('typeField')];
99-
$entityClass = $this->_typeMap[$type]['entityClass'];
100-
$entity = new $entityClass($row->toArray(), [
101-
'markNew' => $row->isNew(),
102-
'markClean' => true,
103-
'guard' => false,
104-
'source' => $this->_typeMap[$type]['alias'],
105-
]);
106-
$entity->virtualProperties($row->virtualProperties());
107-
108-
foreach ($assocs as $assoc => $val) {
109-
$entity->set($assoc, $val);
110-
$entity->dirty($assoc, false);
111-
}
112-
113-
return $entity;
114-
};
115-
116-
$query->formatResults(function ($results) use ($mapper) {
117-
return $results->map($mapper);
97+
$query->formatResults(function ($results) {
98+
return $results->map(function ($row) {
99+
$type = $row[$this->config('typeField')];
100+
$entityClass = $this->_typeMap[$type]['entityClass'];
101+
return new $entityClass($row->forCopy(), [
102+
'markNew' => $row->isNew(),
103+
'markClean' => true,
104+
'guard' => false,
105+
'source' => $this->_typeMap[$type]['alias'],
106+
]);
107+
});
118108
});
119109
}
120110

src/Model/Entity/StiAwareTrait.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Muffin\Sti\Model\Entity;
3+
4+
trait StiAwareTrait
5+
{
6+
public function forCopy()
7+
{
8+
return $this->_properties;
9+
}
10+
}

tests/test_app/Model/Entity/Chef.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
use Cake\ORM\Entity;
55
use Cake\Utility\Inflector;
6+
use Muffin\Sti\Model\Entity\StiAwareTrait;
67

78
class Chef extends Entity
89
{
10+
use StiAwareTrait;
911
protected $_hidden = [
1012
'age',
1113
];

tests/test_app/Model/Entity/Spoon.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
namespace Muffin\Sti\TestApp\Model\Entity;
33

44
use Cake\ORM\Entity;
5+
use Muffin\Sti\Model\Entity\StiAwareTrait;
56

67
class Spoon extends Entity
78
{
8-
9+
use StiAwareTrait;
910
}

tests/test_app/Model/Table/UtensilsTable.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public function initialize(array $config)
1515
'electronic' => 'Muffin\Sti\TestApp\Model\Entity\Electronic',
1616
]
1717
]);
18+
$this->entityClass('Muffin\Sti\TestApp\Model\Entity\Spoon');
1819
}
1920
}

0 commit comments

Comments
 (0)