Skip to content
Closed
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
33 changes: 19 additions & 14 deletions src/Database/Database.php
Original file line numberDiff line numberDiff line change
Expand Up@@ -1622,7 +1622,8 @@ public function createRelationship(
throw new DuplicateException('Attribute already exists');
}

if ($attribute->getAttribute('type') === self::VAR_RELATIONSHIP
if (
$attribute->getAttribute('type') === self::VAR_RELATIONSHIP
&& \strtolower($attribute->getAttribute('options')['twoWayKey']) === \strtolower($twoWayKey)
&& $attribute->getAttribute('options')['relatedCollection'] === $relatedCollection->getId()
) {
Expand DownExpand Up@@ -3050,15 +3051,16 @@ public function updateDocument(string $collection, string $id, Document $documen
$relationType = (string) $relationships[$key]['options']['relationType'];
$side = (string) $relationships[$key]['options']['side'];

switch($relationType) {
switch($relationType) {
case Database::RELATION_ONE_TO_ONE:
$oldValue = $old->getAttribute($key) instanceof Document
? $old->getAttribute($key)->getId()
: $old->getAttribute($key);

if ((\is_null($value) !== \is_null($oldValue))
|| (\is_string($value) && $value !== $oldValue)
|| ($value instanceof Document && $value->getId() !== $oldValue)) {
|| (\is_string($value) && $value !== $oldValue)
|| ($value instanceof Document && $value->getId() !== $oldValue)
) {
$shouldUpdate = true;
}
break;
Expand All@@ -3074,15 +3076,17 @@ public function updateDocument(string $collection, string $id, Document $documen
: $old->getAttribute($key);

if ((\is_null($value) !== \is_null($oldValue))
|| (\is_string($value) && $value !== $oldValue)
|| ($value instanceof Document && $value->getId() !== $oldValue)) {
|| (\is_string($value) && $value !== $oldValue)
|| ($value instanceof Document && $value->getId() !== $oldValue)
) {
$shouldUpdate = true;
}
break;
}

if ((\is_null($old->getAttribute($key)) !== \is_null($value))
|| \count($old->getAttribute($key)) !== \count($value)) {
|| \count($old->getAttribute($key)) !== \count($value)
) {
$shouldUpdate = true;
break;
}
Expand All@@ -3092,7 +3096,8 @@ public function updateDocument(string $collection, string $id, Document $documen
: $old->getAttribute($key)[$index];

if ((\is_string($relation) && $relation !== $oldValue)
|| ($relation instanceof Document && $relation->getId() !== $oldValue)) {
|| ($relation instanceof Document && $relation->getId() !== $oldValue)
) {
$shouldUpdate = true;
break;
}
Expand DownExpand Up@@ -3199,7 +3204,7 @@ private function updateDocumentRelationships(Document $collection, Document $old
if ($oldValue == $value) {
if (
($relationType === Database::RELATION_ONE_TO_ONE ||
($relationType === Database::RELATION_MANY_TO_ONE && $side === Database::RELATION_SIDE_PARENT)) &&
($relationType === Database::RELATION_MANY_TO_ONE && $side === Database::RELATION_SIDE_PARENT)) &&
$value instanceof Document
) {
$document->setAttribute($key, $value->getId());
Expand DownExpand Up@@ -3796,8 +3801,8 @@ private function deleteDocumentRelationships(Document $collection, Document $doc
// collection as an existing relationship, but a different two-way key (the third condition),
// or the same two-way key as an existing relationship, but a different key (the fourth condition).
$transitive = (($existingKey === $twoWayKey
&& $existingCollection === $relatedCollection->getId()
&& $existingSide !== $side)
&& $existingCollection === $relatedCollection->getId()
&& $existingSide !== $side)
|| ($existingTwoWayKey === $key
&& $existingRelatedCollection === $collection->getId()
&& $existingSide !== $side)
Expand DownExpand Up@@ -4141,7 +4146,7 @@ public function find(string $collection, array $queries = []): array
$collection = $this->silent(fn () => $this->getCollection($collection));

if ($collection->isEmpty()) {
throw new DatabaseException('Collection "'. $originalName .'" not found');
throw new DatabaseException('Collection "'. $originalName .'" not found');
}

$attributes = $collection->getAttribute('attributes', []);
Expand DownExpand Up@@ -4279,10 +4284,10 @@ public function find(string $collection, array $queries = []): array
/**
* @param string $collection
* @param array<Query> $queries
* @return bool|Document
* @return false|Document
* @throws DatabaseException
*/
public function findOne(string $collection, array $queries = []): bool|Document
public function findOne(string $collection, array $queries = []): false|Document
{
$results = $this->silent(fn () => $this->find($collection, \array_merge([
Query::limit(1)
Expand Down