Skip to content

Commit

Permalink
nullable sub/sup entity must not throw an error!
Browse files Browse the repository at this point in the history
  • Loading branch information
Zrnik committed Nov 3, 2021
1 parent 5a3e970 commit 949507b
Show file tree
Hide file tree
Showing 17 changed files with 466 additions and 98 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
"phpstan/phpstan-phpunit": "^0.12",
"nette/neon": "^3",
"brick/date-time": "^0.3",
"zrnik/phpunit-exceptions": "^0.0.3"
"zrnik/phpunit-exceptions": "^0.0.4"
},
"autoload": {
"psr-4": {
Expand Down
28 changes: 14 additions & 14 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/Exceptions/TableCreationFailedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php declare(strict_types=1);

namespace Zrnik\MkSQL\Exceptions;

use Throwable;

class TableCreationFailedException extends MkSQLException
{
public function __construct(
string $tableName, ?string $columnName, string $query, Throwable $previous
)
{

$reason = $previous->getMessage();

$message = $columnName === null
? sprintf(
"Unable to create table '%s'\nreason: %s\nquery: %s",
$tableName, $reason, $query
)
: sprintf(
"Unable to create column '%s::%s'\nreason: %s\nquery: %s",
$tableName, $columnName, $reason, $query
);

parent::__construct($message, (int) $previous->getCode(), $previous);
}
}
48 changes: 27 additions & 21 deletions src/Repository/BaseEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ public static function getReflectionClass(BaseEntity|string $obj): ReflectionCla
if (!array_key_exists($objKey, self::$reflectionClasses)) {
try {
/**
* @throws ReflectionException
* @var ReflectionClass<BaseEntity> $reflectionInstance
* @noinspection PhpRedundantVariableDocTypeInspection
* @throws ReflectionException
*/
$reflectionInstance = new ReflectionClass($objKey);
self::$reflectionClasses[$objKey] = $reflectionInstance;
Expand Down Expand Up @@ -159,12 +159,12 @@ public function toArray(): array
$foreignAttributeType = Reflection::propertyGetAttribute($reflectionProperty, ForeignKey::class);
if ($foreignAttributeType !== null) {

// That means '$propertyValue' is an object extending 'BaseEntity'
if ($propertyValue instanceof self) {
// That means '$propertyValue' is an object extending 'BaseEntity' or null
if ($propertyValue instanceof self || $propertyValue === null) {
/** @var BaseEntity $foreignEntityClassName */
$foreignEntityClassName = Reflection::attributeGetArgument($foreignAttributeType);
$primaryKeyName = $foreignEntityClassName::getPrimaryKeyName();
$propertyValue = $propertyValue->toArray()[$primaryKeyName];
$propertyValue = $propertyValue?->toArray()[$primaryKeyName];

// Also, we need to update the `$rawData` property!

Expand Down Expand Up @@ -222,19 +222,19 @@ public static function create(array $initValues = []): static
$primaryKeyPropertyName = static::getPrimaryKeyPropertyName();

// add 'getDefaultValues' to the created entity
foreach($entity->getDefaults() as $key => $value) {
foreach ($entity->getDefaults() as $key => $value) {

if($primaryKeyPropertyName === $key) {
if ($primaryKeyPropertyName === $key) {
throw new PrimaryKeyProvidedInDefaults();
}

$entity->$key = $value;
}

// Overwrite "initialize" values
foreach($initValues as $key => $value) {
foreach ($initValues as $key => $value) {

if($primaryKeyPropertyName === $key) {
if ($primaryKeyPropertyName === $key) {
throw new PrimaryKeyProvidedInDefaults();
}

Expand Down Expand Up @@ -851,24 +851,30 @@ public static function hydrateUpdater(Updater $updater, array $baseEntitiesWeHav
);
}

$addToForeignKeyReference = true;

if (
!$isFetchArray
&& in_array($referencedEntityName, $baseEntitiesWeHaveAlreadySeen, true)
) {

//dump($referencedEntityName, static::class);

if($referencedEntityName === static::class) {
continue;
if ($referencedEntityName !== static::class) {
throw new CircularReferenceDetectedException(
static::class, $property->getName()
);
}

throw new CircularReferenceDetectedException(
static::class, $property->getName()
);

$addToForeignKeyReference = false;

}

$referencedForeignKeys[$referencedEntityName] = $property->getName();
$hydrateAfter[] = $referencedEntityName;


if ($addToForeignKeyReference) {
$hydrateAfter[] = $referencedEntityName;
}
}

//endregion
Expand Down Expand Up @@ -1030,7 +1036,7 @@ public function fixSubEntityForeignKeys(): void

/**
* Sub entities (fetch array)
* @return BaseEntity[]
* @return array<?BaseEntity>
*/
public function subEntities(): array
{
Expand All @@ -1048,7 +1054,7 @@ public function subEntities(): array

/**
* Superior entities (foreign key)
* @return BaseEntity[]
* @return array<?BaseEntity>
*/
public function supEntities(): array
{
Expand Down Expand Up @@ -1084,16 +1090,16 @@ public function getOriginalData(): ?array
*/
final public function setOriginalData(?array $newOriginalData): void
{
if(!Misc::isPhpUnitTest()) {
if (!Misc::isPhpUnitTest()) {
throw new Exception("Method 'setOriginalData' is internal, and exists only for testing purpose!");
}

$this->originalData = $newOriginalData;
}

public function indicateSave(): void
public function indicateSave(bool $saved = true): void
{
$this->originalData = $this->getRawData();
$this->originalData = $saved ? $this->getRawData() : [];
}

}
4 changes: 3 additions & 1 deletion src/Repository/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ public function getResultsByKeys(
): array
{
$dispenser = new Dispenser(($this->getPdo()));
$entities = $dispenser->getResultsByKeys($baseEntityClassString, $propertyName, $values);
$entities = $dispenser->getResultsByKeys(
$baseEntityClassString, $propertyName, $values
);
$this->executedQueries += $dispenser->getExecutedQueryCount();
return $entities;
}
Expand Down
7 changes: 6 additions & 1 deletion src/Repository/Fetch/EntityStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,12 @@ public function getEntities(): array
string $className, mixed $primaryKeyValue
): ?BaseEntity
{
$index = $this->entitiesIndexPointer[$this->cnpk($className, $primaryKeyValue)];
$index =
$this->entitiesIndexPointer
[$this->cnpk($className, $primaryKeyValue)]
?? 'null'
;

return $this->entities[$index] ?? null;
}

Expand Down
Loading

0 comments on commit 949507b

Please sign in to comment.