Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Model for Join write operations #960

Merged
merged 19 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
}
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"phpstan/extension-installer": true
},
"sort-packages": true
}
}
4 changes: 2 additions & 2 deletions docs/persistence/sql/queries.rst
Original file line number Diff line number Diff line change
Expand Up @@ -473,11 +473,11 @@ and overwrite this simple method to support expressions like this, for example:
Joining with other tables
-------------------------

.. php:method:: join($foreign_table, $master_field, $join_kind)
.. php:method:: join($foreignTable, $master_field, $join_kind)

Join results with additional table using "JOIN" statement in your query.

:param string|array $foreign_table: table to join (may include field and alias)
:param string|array $foreignTable: table to join (may include field and alias)
:param mixed $master_field: main field (and table) to join on or Expression
:param string $join_kind: 'left' (default), 'inner', 'right' etc - which join type to use
:returns: $this
Expand Down
3 changes: 0 additions & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,6 @@ parameters:
# for src/Persistence/Sql.php
- '~^Call to an undefined method Atk4\\Data\\Persistence::expr\(\)\.$~'
- '~^Call to an undefined method Atk4\\Data\\Persistence::exprNow\(\)\.$~'
# for src/Persistence/Sql/Join.php
- '~^Call to an undefined method Atk4\\Data\\Persistence::initQuery\(\)\.$~'
- '~^Call to an undefined method Atk4\\Data\\Persistence::lastInsertId\(\)\.$~'
# for src/Reference/HasMany.php
- '~^Call to an undefined method Atk4\\Data\\Model::dsql\(\)\.$~'
# for tests/FieldTest.php
Expand Down
6 changes: 3 additions & 3 deletions src/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Model implements \IteratorAggregate
/** @var array Array of set order by. */
public $order = [];

/** @var array Array of WITH cursors set. */
/** @var array<string, array{'model': Model, 'mapping': array<int|string, string>, 'recursive': bool}> */
public $with = [];

/**
Expand Down Expand Up @@ -1074,8 +1074,8 @@ public function withId($id)
*/
public function addWith(self $model, string $alias, array $mapping = [], bool $recursive = false)
{
if (isset($this->with[$alias])) {
throw (new Exception('With cursor already set with this alias'))
if ($alias === $this->table || $alias === $this->table_alias || isset($this->with[$alias])) {
throw (new Exception('With cursor already set with given alias'))
->addMoreInfo('alias', $alias);
}

Expand Down
170 changes: 156 additions & 14 deletions src/Model/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
*
* @method Model getOwner()
*/
class Join
abstract class Join
{
use DiContainerTrait;
use InitializerTrait {
Expand All @@ -31,8 +31,7 @@ class Join
}

/**
* Name of the table (or collection) that can be used to retrieve data from.
* For SQL, This can also be an expression or sub-select.
* Foreign model or WITH/CTE alias when used with SQL persistence.
*
* @var string
mvorisek marked this conversation as resolved.
Show resolved Hide resolved
*/
Expand Down Expand Up @@ -120,9 +119,9 @@ class Join
/** @var array<int, array<string, mixed>> Data indexed by spl_object_id(entity) which is populated here as the save/insert progresses. */
private $saveBufferByOid = [];

public function __construct(string $foreign_table = null)
public function __construct(string $foreignTable = null)
{
$this->foreign_table = $foreign_table;
$this->foreign_table = $foreignTable;

// handle foreign table containing a dot - that will be reverse join
if (strpos($this->foreign_table, '.') !== false) {
Expand All @@ -132,6 +131,42 @@ public function __construct(string $foreign_table = null)
}
}

/**
* Create fake foreign model, in the future, this method should be removed
* in favor of always requiring an object model.
*/
protected function createFakeForeignModel(): Model
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI if foreign model hooks needs to be executed, override this method to provide non-faked Model for $this->foreign_table table

{
$fakeModel = new Model($this->getOwner()->persistence, [
'table' => $this->foreign_table,
]);
foreach ($this->getOwner()->getFields() as $ownerField) {
if ($ownerField->hasJoin() && $ownerField->getJoin()->short_name === $this->short_name
&& $ownerField->short_name !== $fakeModel->id_field
&& $ownerField->short_name !== $this->foreign_field) {
$fakeModel->addField($ownerField->short_name, [
'actual' => $ownerField->actual,
'type' => $ownerField->type,
]);
}
}
if ($fakeModel->id_field !== $this->foreign_field && $this->foreign_field !== null) {
$fakeModel->addField($this->foreign_field, ['type' => 'integer']);
}

return $fakeModel;
}

public function getForeignModel(): Model
{
// TODO this should be removed in the future
if (!isset($this->getOwner()->with[$this->foreign_table])) {
return $this->createFakeForeignModel();
}

return $this->getOwner()->with[$this->foreign_table]['model'];
}

/**
* @param Model $owner
*
Expand Down Expand Up @@ -203,6 +238,8 @@ protected function init(): void
{
$this->_init();

$this->getForeignModel(); // assert valid foreign_table

// owner model should have id_field set
$id_field = $this->getOwner()->id_field;
if (!$id_field) {
Expand Down Expand Up @@ -236,12 +273,29 @@ protected function init(): void
}
}

$this->onHookToOwnerEntity(Model::HOOK_AFTER_UNLOAD, \Closure::fromCallable([$this, 'afterUnload']));

// if kind is not specified, figure out join type
if (!$this->kind) {
$this->kind = $this->weak ? 'left' : 'inner';
}

$this->initJoinHooks();
}

protected function initJoinHooks(): void
{
$this->onHookToOwnerEntity(Model::HOOK_AFTER_UNLOAD, \Closure::fromCallable([$this, 'afterUnload']));

if ($this->reverse) {
$this->onHookToOwnerEntity(Model::HOOK_AFTER_INSERT, \Closure::fromCallable([$this, 'afterInsert']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_DELETE, \Closure::fromCallable([$this, 'doDelete']), [], -5);
} else {
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_INSERT, \Closure::fromCallable([$this, 'beforeInsert']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_BEFORE_UPDATE, \Closure::fromCallable([$this, 'beforeUpdate']), [], -5);
$this->onHookToOwnerEntity(Model::HOOK_AFTER_DELETE, \Closure::fromCallable([$this, 'doDelete']));

$this->onHookToOwnerEntity(Model::HOOK_AFTER_LOAD, \Closure::fromCallable([$this, 'afterLoad']));
}
}

/**
Expand Down Expand Up @@ -280,23 +334,23 @@ public function addFields(array $fields = [], array $defaults = [])
*
* @param array<string, mixed> $defaults
*/
public function join(string $foreign_table, array $defaults = []): self
public function join(string $foreignTable, array $defaults = []): self
{
$defaults['joinName'] = $this->short_name;

return $this->getOwner()->join($foreign_table, $defaults);
return $this->getOwner()->join($foreignTable, $defaults);
}

/**
* Another leftJoin will be attached to a current join.
*
* @param array<string, mixed> $defaults
*/
public function leftJoin(string $foreign_table, array $defaults = []): self
public function leftJoin(string $foreignTable, array $defaults = []): self
{
$defaults['joinName'] = $this->short_name;

return $this->getOwner()->leftJoin($foreign_table, $defaults);
return $this->getOwner()->leftJoin($foreignTable, $defaults);
}

/**
Expand Down Expand Up @@ -452,12 +506,100 @@ public function setSaveBufferValue(Model $entity, string $fieldName, $value): vo
$this->saveBufferByOid[spl_object_id($entity)][$fieldName] = $value;
}

/**
* Clears id and save buffer.
*/
protected function afterUnload(Model $entity): void
{
$this->unsetId($entity);
$this->unsetSaveBuffer($entity);
}

abstract public function afterLoad(Model $entity): void;

public function beforeInsert(Model $entity, array &$data): void
{
if ($this->weak) {
return;
}

$model = $this->getOwner();

// the value for the master_field is set, so we are going to use existing record anyway
if ($model->hasField($this->master_field) && $entity->get($this->master_field) !== null) {
return;
}

$foreignModel = $this->getForeignModel();
$foreignEntity = $foreignModel->createEntity()
->setMulti($this->getAndUnsetSaveBuffer($entity))
/*->set($this->foreign_field, null)*/;
$foreignEntity->save();

$this->setId($entity, $foreignEntity->getId());

if ($this->hasJoin()) {
$this->getJoin()->setSaveBufferValue($entity, $this->master_field, $this->getId($entity));
} else {
$data[$this->master_field] = $this->getId($entity);
}

// $entity->set($this->master_field, $this->getId($entity));
}

public function afterInsert(Model $entity): void
{
if ($this->weak) {
return;
}

$this->setSaveBufferValue($entity, $this->foreign_field, $this->hasJoin() ? $this->getJoin()->getId($entity) : $entity->getId()); // from array persistence...

$foreignModel = $this->getForeignModel();
$foreignEntity = $foreignModel->createEntity()
->setMulti($this->getAndUnsetSaveBuffer($entity))
->set($this->foreign_field, $this->hasJoin() ? $this->getJoin()->getId($entity) : $entity->getId());
$foreignEntity->save();

$this->setId($entity, $entity->getId()); // TODO why is this here? it seems to be not needed
}

public function beforeUpdate(Model $entity, array &$data): void
{
if ($this->weak) {
return;
}

if (!$this->issetSaveBuffer($entity)) {
return;
}

$foreignModel = $this->getForeignModel();
$foreignId = $this->reverse ? $entity->getId() : $entity->get($this->master_field);
$saveBuffer = $this->getAndUnsetSaveBuffer($entity);
$foreignModel->atomic(function () use ($foreignModel, $foreignId, $saveBuffer) {
$foreignModel = (clone $foreignModel)->addCondition($this->foreign_field, $foreignId);
foreach ($foreignModel as $foreignEntity) {
$foreignEntity->setMulti($saveBuffer);
$foreignEntity->save();
}
});

// $this->setId($entity, ??); // TODO needed? from array persistence
}

public function doDelete(Model $entity): void
{
if ($this->weak) {
return;
}

$foreignModel = $this->getForeignModel();
$foreignId = $this->reverse ? $entity->getId() : $entity->get($this->master_field);
$foreignModel->atomic(function () use ($foreignModel, $foreignId) {
$foreignModel = (clone $foreignModel)->addCondition($this->foreign_field, $foreignId);
foreach ($foreignModel as $foreignEntity) {
$foreignEntity->delete();
}
});

$this->unsetId($entity); // TODO needed? from array persistence
}
}
14 changes: 7 additions & 7 deletions src/Persistence/Array_.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,20 +214,21 @@ public function tryLoad(Model $model, $id): ?array
{
if ($id === self::ID_LOAD_ONE || $id === self::ID_LOAD_ANY) {
$action = $this->action($model, 'select');
$action->generator->rewind(); // TODO needed for some reasons!

$selectRow = $action->getRow();
if ($selectRow === null) {
$action->limit($id === self::ID_LOAD_ANY ? 1 : 2);

$rowsRaw = $action->getRows();
if (count($rowsRaw) === 0) {
return null;
} elseif ($id === self::ID_LOAD_ONE && $action->getRow() !== null) {
} elseif (count($rowsRaw) !== 1) {
throw (new Exception('Ambiguous conditions, more than one record can be loaded.'))
->addMoreInfo('model', $model)
->addMoreInfo('id', null);
}

$id = $selectRow[$model->id_field];
$idRaw = reset($rowsRaw)[$model->id_field];

$row = $this->tryLoad($model, $id);
$row = $this->tryLoad($model, $idRaw);

return $row;
}
Expand All @@ -238,7 +239,6 @@ public function tryLoad(Model $model, $id): ?array
$condition->key = $model->getField($model->id_field);
$condition->setOwner($model->createEntity()); // TODO needed for typecasting to apply
$action->filter($condition);
$action->generator->rewind(); // TODO needed for some reasons!

$rowData = $action->getRow();
if ($rowData === null) {
Expand Down
Loading