Skip to content

Commit

Permalink
Revert "WIP/DRAFT fix Model::getFields"
Browse files Browse the repository at this point in the history
This reverts commit 6c98cf4.
  • Loading branch information
mvorisek committed Nov 7, 2021
1 parent 6c98cf4 commit 5da6d19
Show file tree
Hide file tree
Showing 17 changed files with 77 additions and 75 deletions.
6 changes: 3 additions & 3 deletions src/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public function getTypeObject(): Type
return Type::getType($this->type ?? 'string');
}

protected function onHookToOwner(string $spot, \Closure $fx, array $args = [], int $priority = 5): int
protected function onHookShortToOwner(string $spot, \Closure $fx, array $args = [], int $priority = 5): int
{
$name = $this->short_name; // use static function to allow this object to be GCed

return $this->getOwner()->onHookDynamic(
return $this->getOwner()->onHookDynamicShort(
$spot,
static function (Model $owner) use ($name) {
return $owner->getModel()->getField($name);
return $owner->getField($name);
},
$fx,
$args,
Expand Down
6 changes: 4 additions & 2 deletions src/Field/Callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ protected function init(): void

$this->ui['table']['sortable'] = false;

$this->onHookToOwner(Model::HOOK_AFTER_LOAD, function (Model $entity) {
$entity->getDataRef()[$this->short_name] = ($this->expr)($entity);
$this->onHookShortToOwner(Model::HOOK_AFTER_LOAD, function () {
$model = $this->getOwner();

$model->getDataRef()[$this->short_name] = ($this->expr)($model);
});
}
}
4 changes: 2 additions & 2 deletions src/FieldSqlExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ protected function init(): void
}

if ($this->concat) {
$this->onHookToOwner(Model::HOOK_AFTER_SAVE, \Closure::fromCallable([$this, 'afterSave']));
$this->onHookShortToOwner(Model::HOOK_AFTER_SAVE, \Closure::fromCallable([$this, 'afterSave']));
}
}

/**
* Possibly that user will attempt to insert values here. If that is the case, then
* we would need to inject it into related hasMany relationship.
*/
public function afterSave(Model $entity): void
public function afterSave(): void
{
}

Expand Down
44 changes: 27 additions & 17 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -653,13 +653,13 @@ public function hasField(string $name): bool

public function getField(string $name): Field
{
// if ($this->isEntity()) { // TODO dev only
// $entityField = clone $this->getModel()->getField($name);
// $entityField->unsetOwner();
// $entityField->_setOwner($this);
//
// return $entityField;
// }
if ($this->isEntity()) { // TODO dev only
$entityField = clone $this->getModel()->getField($name);
$entityField->unsetOwner();
$entityField->_setOwner($this);

return $entityField;
}

$this->assertIsModel();

Expand Down Expand Up @@ -780,7 +780,7 @@ public function set(string $field, $value)
{
$this->getModel()->checkOnlyFieldsField($field);

$f = $this->getModel()->getField($field);
$f = $this->getField($field);

try {
$value = $f->normalize($value);
Expand Down Expand Up @@ -825,13 +825,13 @@ public function set(string $field, $value)
public function setNull(string $field)
{
// set temporary hook to disable any normalization (null validation)
$hookIndex = $this->getModel()->onHookShort(self::HOOK_NORMALIZE, static function () {
$hookIndex = $this->onHookShort(self::HOOK_NORMALIZE, static function () {
throw new \Atk4\Core\HookBreaker(false);
}, [], \PHP_INT_MIN);
try {
return $this->set($field, null);
} finally {
$this->getModel()->removeHook(self::HOOK_NORMALIZE, $hookIndex, true);
$this->removeHook(self::HOOK_NORMALIZE, $hookIndex, true);
}
}

Expand Down Expand Up @@ -876,7 +876,7 @@ public function get(string $field = null)
return $dataRef[$field];
}

return $this->getModel()->getField($field)->default;
return $this->getField($field)->default;
}

private function assertHasIdField(): void
Expand Down Expand Up @@ -972,7 +972,7 @@ public function getTitles(): array
*/
public function compare(string $name, $value): bool
{
return $this->getModel()->getField($name)->compare($this->get($name), $value);
return $this->getField($name)->compare($this->get($name), $value);
}

/**
Expand Down Expand Up @@ -1350,7 +1350,7 @@ public function reload()
*/
public function duplicate()
{
// deprecated, TODO remove in v3.2
// deprecated, TODO remove in v3.1
if (func_num_args() > 0) {
throw new Exception('Duplicating using existing ID is no longer supported');
}
Expand Down Expand Up @@ -1515,6 +1515,11 @@ public function checkPersistence(string $method = null): void
*/
public function save(array $data = [])
{
// deprecated, TODO remove in v3.1
if (func_num_args() > 1) {
throw new Exception('Model::save() with 2nd param $to_persistence is no longer supported');
}

$this->checkPersistence();

if ($this->read_only) {
Expand All @@ -1538,7 +1543,7 @@ public function save(array $data = [])
$data = [];
$dirty_join = false;
foreach ($dirtyRef as $name => $ignore) {
$field = $this->getModel()->getField($name);
$field = $this->getField($name);
if ($field->read_only || $field->never_persist || $field->never_save) {
continue;
}
Expand All @@ -1549,7 +1554,7 @@ public function save(array $data = [])
if ($field->hasJoin()) {
$dirty_join = true;
// storing into a different table join
$field->getJoin($this)->set($name, $value);
$field->getJoin()->set($name, $value);
} else {
$data[$name] = $value;
}
Expand All @@ -1570,14 +1575,14 @@ public function save(array $data = [])
} else {
$data = [];
foreach ($this->get() as $name => $value) {
$field = $this->getModel()->getField($name);
$field = $this->getField($name);
if ($field->read_only || $field->never_persist || $field->never_save) {
continue;
}

if ($field->hasJoin()) {
// storing into a different table join
$field->getJoin($this)->set($name, $value);
$field->getJoin()->set($name, $value);
} else {
$data[$name] = $value;
}
Expand Down Expand Up @@ -1897,6 +1902,11 @@ public function delete($id = null)
*/
public function atomic(\Closure $fx)
{
// deprecated, TODO remove in v3.1
if (func_num_args() > 1) {
throw new Exception('Model::atomic() with 2nd param $persistence is no longer supported');
}

try {
return $this->persistence->atomic($fx);
} catch (\Exception $e) {
Expand Down
17 changes: 2 additions & 15 deletions src/Model/JoinLinkTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

namespace Atk4\Data\Model;

use Atk4\Data\Model;

trait JoinLinkTrait
{
/**
Expand All @@ -15,20 +13,9 @@ trait JoinLinkTrait
*/
protected $joinName;

private function assertIsOwnerEntity(Model $entity): void
public function getJoin(): Join
{
$entity->assertIsEntity(/* TODO $this->getOwner() valid once not rebound to insatnce in Model */);
}

public function getJoin(Model $entity = null): Join
{
$model = $this->getOwner();
if ($entity !== null) { // TODO non-null default?
$this->assertIsOwnerEntity($entity);
$model = $entity;
}

return $model->getElement($this->joinName);
return $this->getOwner()->getElement($this->joinName);
}

public function hasJoin(): bool
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function typecastLoadRow(Model $model, array $row): array
{
$result = [];
foreach ($row as $fieldName => $value) {
$field = $model->getModel(true)->getField($fieldName);
$field = $model->getField($fieldName);

$result[$fieldName] = $this->typecastLoadField($field, $value);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ private function assertNoIdMismatch($idFromRow, $id): void
private function saveRow(Model $model, array $rowData, $id): void
{
if ($model->id_field) {
$idField = $model->getModel(true)->getField($model->id_field);
$idField = $model->getField($model->id_field);
$idColumnName = $idField->getPersistenceName();
if (array_key_exists($idColumnName, $rowData)) {
$this->assertNoIdMismatch($rowData[$idColumnName], $id);
Expand Down Expand Up @@ -238,7 +238,7 @@ public function insert(Model $model, array $data)
if ($model->id_field && ($data[$model->id_field] ?? null) === null) {
unset($data[$model->id_field]);
}
$data = $this->typecastSaveRow($model->getModel(true), $data);
$data = $this->typecastSaveRow($model, $data);

$id = $data[$model->id_field] ?? $this->generateNewId($model);

Expand All @@ -256,7 +256,7 @@ public function update(Model $model, $id, array $data): void
{
$table = $this->seedDataAndGetTable($model);

$data = $this->typecastSaveRow($model->getModel(true), $data);
$data = $this->typecastSaveRow($model, $data);

$this->saveRow($model, array_merge($this->filterRowDataOnlyModelFields($model, $table->getRowById($model, $id)->getData()), $data), $id);
}
Expand All @@ -282,7 +282,7 @@ public function generateNewId(Model $model)
{
$this->seedData($model);

$type = $model->id_field ? $model->getModel(true)->getField($model->id_field)->type : 'integer';
$type = $model->id_field ? $model->getField($model->id_field)->type : 'integer';

switch ($type) {
case 'integer':
Expand Down
2 changes: 1 addition & 1 deletion src/Persistence/Csv.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public function insert(Model $model, array $data)
throw new Exception('Currently reading records, so writing is not possible.');
}

$data = $this->typecastSaveRow($model->getModel(), $data);
$data = $this->typecastSaveRow($model, $data);

if (!$this->handle) {
$this->saveHeader($model);
Expand Down
18 changes: 9 additions & 9 deletions src/Persistence/Sql.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,14 @@ public function initQueryConditions(Model $model, Query $query): void

// add entity ID to scope to allow easy traversal
if ($model->isEntity() && $model->id_field && $model->getId() !== null) {
$query->group($model->getModel(true)->getField($model->id_field));
$query->group($model->getField($model->id_field));
if ($this->getDatabasePlatform() instanceof SQLServer2012Platform
|| $this->getDatabasePlatform() instanceof OraclePlatform) {
foreach ($query->args['field'] as $alias => $field) {
$query->group(is_int($alias) ? $field : $alias);
}
}
$query->having($model->getModel(true)->getField($model->id_field), $model->getId());
$query->having($model->getField($model->id_field), $model->getId());
}
}

Expand Down Expand Up @@ -448,7 +448,7 @@ public function action(Model $model, string $type, array $args = [])
->addMoreInfo('action', $type);
}

$field = is_string($args[0]) ? $model->getModel(true)->getField($args[0]) : $args[0];
$field = is_string($args[0]) ? $model->getField($args[0]) : $args[0];
$model->hook(self::HOOK_INIT_SELECT_QUERY, [$query, $type]);
if (isset($args['alias'])) {
$query->reset('field')->field($field, $args['alias']);
Expand All @@ -461,7 +461,7 @@ public function action(Model $model, string $type, array $args = [])
$this->setLimitOrder($model, $query);

if ($model->isEntity() && $model->loaded()) {
$query->where($model->getModel(true)->getField($model->id_field), $model->getId());
$query->where($model->getField($model->id_field), $model->getId());
}

return $query;
Expand Down Expand Up @@ -563,7 +563,7 @@ public function insert(Model $model, array $data): string
unset($data[$model->id_field]);
}

$insert->set($this->typecastSaveRow($model->getModel(true), $data));
$insert->set($this->typecastSaveRow($model, $data));

$st = null;
try {
Expand All @@ -580,7 +580,7 @@ public function insert(Model $model, array $data): string
if ($model->id_field && ($data[$model->id_field] ?? null) !== null) {
$id = (string) $data[$model->id_field];
} else {
$id = $this->lastInsertId($model->getModel(true));
$id = $this->lastInsertId($model);
}

$model->hook(self::HOOK_AFTER_INSERT_QUERY, [$insert, $st]);
Expand Down Expand Up @@ -633,11 +633,11 @@ public function update(Model $model, $id, array $data): void
$update = $this->initQuery($model);
$update->mode('update');

$data = $this->typecastSaveRow($model->getModel(true), $data);
$data = $this->typecastSaveRow($model, $data);

// only apply fields that has been modified
$update->set($data);
$update->where($model->getModel()->getField($model->id_field), $id);
$update->where($model->getField($model->id_field), $id);

$st = null;
try {
Expand Down Expand Up @@ -683,7 +683,7 @@ public function delete(Model $model, $id): void

$delete = $this->initQuery($model);
$delete->mode('delete');
$delete->where($model->getModel(true)->getField($model->id_field), $id);
$delete->where($model->getField($model->id_field), $id);
$model->hook(self::HOOK_BEFORE_DELETE_QUERY, [$delete]);

try {
Expand Down
8 changes: 4 additions & 4 deletions src/Persistence/Sql/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public function initSelectQuery(Query $query): void
$this->getOwner()->expr('{{}}.{} = {}', [
($this->foreign_alias ?: $this->foreign_table),
$this->foreign_field,
$this->getOwner()->getModel(true)->getField($this->master_field),
$this->getOwner()->getField($this->master_field),
]),
$this->kind,
$this->foreign_alias
Expand Down Expand Up @@ -198,11 +198,11 @@ public function afterInsert($id): void
$model = $this->getOwner();

$query = $this->dsql();
$query->set($model->persistence->typecastSaveRow($model->getModel(), $this->save_buffer));
$query->set($model->persistence->typecastSaveRow($model, $this->save_buffer));
$this->save_buffer = [];
$query->set($this->foreign_field, $this->hasJoin() ? $this->getJoin()->id : $id);
$query->insert();
$this->id = $model->persistence->lastInsertId($model->getModel());
$this->id = $model->persistence->lastInsertId($model);
}

/**
Expand All @@ -220,7 +220,7 @@ public function beforeUpdate(array &$data): void

$model = $this->getOwner();
$query = $this->dsql();
$query->set($model->persistence->typecastSaveRow($model->getModel(), $this->save_buffer));
$query->set($model->persistence->typecastSaveRow($model, $this->save_buffer));
$this->save_buffer = [];

$id = $this->reverse ? $model->getId() : $model->get($this->master_field);
Expand Down
2 changes: 1 addition & 1 deletion src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ protected function getOurFieldName(): string

final protected function getOurField(): Field
{
return $this->getOurModel()->getModel(true)->getField($this->getOurFieldName());
return $this->getOurModel()->getField($this->getOurFieldName());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/BusinessModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function testDefaultInit(): void
$m = new Model($p);
$m = $m->createEntity();

$this->assertNotNull($m->getModel()->getField('id'));
$this->assertNotNull($m->getField('id'));

$m->set('id', 20);
$this->assertEquals(20, $m->getId());
Expand Down
Loading

0 comments on commit 5da6d19

Please sign in to comment.