Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
117 changes: 75 additions & 42 deletions src/Libraries/Database/Adapters/Idiorm/Statements/Join.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@
* @author Arman Ag. <arman.ag@softberg.org>
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org)
* @link http://quantum.softberg.org/
* @since 2.9.8
* @since 2.9.9
*/

namespace Quantum\Libraries\Database\Adapters\Idiorm\Statements;

use Quantum\Libraries\Database\Exceptions\DatabaseException;
use Quantum\Libraries\Database\Adapters\Idiorm\IdiormPatch;
use Quantum\Libraries\Database\Contracts\DbalInterface;
use Quantum\Libraries\Database\Enums\Relation;
use Quantum\Model\Exceptions\ModelException;
use Quantum\App\Exceptions\BaseException;
use Quantum\Model\QtModel;

/**
Expand All @@ -29,7 +30,7 @@ trait Join

/**
* @inheritDoc
* @throws DatabaseException
* @throws BaseException
*/
public function join(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
{
Expand All @@ -39,7 +40,7 @@ public function join(string $table, array $constraint, ?string $tableAlias = nul

/**
* @inheritDoc
* @throws DatabaseException
* @throws BaseException
*/
public function innerJoin(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
{
Expand All @@ -49,7 +50,7 @@ public function innerJoin(string $table, array $constraint, ?string $tableAlias

/**
* @inheritDoc
* @throws DatabaseException
* @throws BaseException
*/
public function leftJoin(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
{
Expand All @@ -59,7 +60,7 @@ public function leftJoin(string $table, array $constraint, ?string $tableAlias =

/**
* @inheritDoc
* @throws DatabaseException
* @throws BaseException
*/
public function rightJoin(string $table, array $constraint, ?string $tableAlias = null): DbalInterface
{
Expand All @@ -69,65 +70,97 @@ public function rightJoin(string $table, array $constraint, ?string $tableAlias

/**
* @inheritDoc
* @throws DatabaseException
* @throws BaseException
* @throws ModelException
*/
public function joinTo(QtModel $modelToJoin, bool $switch = true): DbalInterface
public function joinTo(QtModel $relatedModel, bool $switch = true): DbalInterface
{
$foreignKeys = $modelToJoin->relations();
$relatedModelName = $this->getModelName();
$relation = $this->getValidatedRelation($relatedModel);

if (!isset($foreignKeys[$relatedModelName])) {
throw ModelException::wrongRelation(get_class($modelToJoin), $relatedModelName);
switch ($relation['type']) {
case Relation::HAS_ONE:
case Relation::HAS_MANY:
$this->applyHasRelation($relatedModel, $relation);
break;

case Relation::BELONGS_TO:
$this->applyBelongsTo($relatedModel, $relation);
break;

default:
throw ModelException::unsupportedRelationType($relation['type']);
}

if ($switch) {
$this->modelName = get_class($relatedModel);
$this->table = $relatedModel->table;
$this->idColumn = $relatedModel->idColumn;
$this->foreignKeys = $relatedModel->relations();
}

$this->getOrmModel()->join($modelToJoin->table,
return $this;
}

/**
* @param QtModel $relatedModel
* @param array $relation
* @return void
* @throws BaseException
*/
protected function applyHasRelation(QtModel $relatedModel, array $relation): void
{
$this->getOrmModel()->join(
$relatedModel->table,
[
$modelToJoin->table . '.' . $foreignKeys[$relatedModelName]['foreign_key'],
$relatedModel->table . '.' . $relation['foreign_key'],
'=',
$this->table . '.' . $foreignKeys[$relatedModelName]['local_key']
$this->table . '.' . $relation['local_key']
]
);
}

if ($switch) {
$this->modelName = get_class($modelToJoin);
$this->table = $modelToJoin->table;
$this->idColumn = $modelToJoin->idColumn;
$this->foreignKeys = $foreignKeys;
}

return $this;
/**
* @param QtModel $relatedModel
* @param array $relation
* @return void
* @throws BaseException
*/
protected function applyBelongsTo(QtModel $relatedModel, array $relation): void
{
$this->getOrmModel()->join(
$relatedModel->table,
[
$relatedModel->table . '.' . $relation['local_key'],
'=',
$this->table . '.' . $relation['foreign_key']
]
);
}

/**
* @inheritDoc
* @throws DatabaseException
* @param QtModel $modelToJoin
* @return array
* @throws ModelException
*/
public function joinThrough(QtModel $modelToJoin, bool $switch = true): DbalInterface
private function getValidatedRelation(QtModel $modelToJoin): array
{
$foreignKeys = $this->getForeignKeys();
$relations = $this->getForeignKeys();
$relatedModelName = get_class($modelToJoin);

if (!isset($foreignKeys[$relatedModelName])) {
throw ModelException::wrongRelation($relatedModelName, $this->getModelName());
if (!isset($relations[$relatedModelName])) {
throw ModelException::wrongRelation($this->getModelName(), $relatedModelName);
}

$this->getOrmModel()->join($modelToJoin->table,
[
$modelToJoin->table . '.' . $foreignKeys[$relatedModelName]['local_key'],
'=',
$this->table . '.' . $foreignKeys[$relatedModelName]['foreign_key'],
]
);
$relation = $relations[$relatedModelName];

if ($switch) {
$this->modelName = get_class($modelToJoin);
$this->table = $modelToJoin->table;
$this->idColumn = $modelToJoin->idColumn;
$this->foreignKeys = $modelToJoin->relations();
if (empty($relation['type'])) {
throw ModelException::relationTypeMissing($this->getModelName(), $relatedModelName);
}

return $this;
if (empty($relation['foreign_key']) || empty($relation['local_key'])) {
throw ModelException::missingRelationKeys($this->getModelName(), $relatedModelName);
}

return $relation;
}
}
4 changes: 3 additions & 1 deletion src/Libraries/Database/Adapters/Sleekdb/SleekDbal.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ public function updateOrmModel(?array $modelData)

/**
* Deletes the table and the data
* @throws BaseException
* @throws DatabaseException
* @throws IOException
* @throws InvalidArgumentException
Expand All @@ -304,11 +305,12 @@ public function deleteTable()
/**
* Gets the query builder object
* @return QueryBuilder
* @throws BaseException
* @throws DatabaseException
* @throws ModelException
* @throws IOException
* @throws InvalidArgumentException
* @throws InvalidConfigurationException
* @throws ModelException
*/
public function getBuilder(): QueryBuilder
{
Expand Down
Loading