From 0acca3e3ad1204d99627fe15a244fc923e4b1b02 Mon Sep 17 00:00:00 2001 From: Bob den Otter Date: Tue, 11 May 2021 17:05:42 +0200 Subject: [PATCH 1/2] Fix relationships showing in backend, allow bidirectional deleting --- .../Backend/ContentEditController.php | 2 +- src/Repository/RelationRepository.php | 37 +++++-------------- src/Twig/RelatedExtension.php | 31 ++-------------- templates/content/_relationships.html.twig | 3 +- tests/spec/Bolt/Twig/RelatedExtensionSpec.php | 12 +++--- 5 files changed, 21 insertions(+), 64 deletions(-) diff --git a/src/Controller/Backend/ContentEditController.php b/src/Controller/Backend/ContentEditController.php index eceb55798..7eb191464 100644 --- a/src/Controller/Backend/ContentEditController.php +++ b/src/Controller/Backend/ContentEditController.php @@ -584,7 +584,7 @@ private function updateTaxonomy(Content $content, string $key, $taxonomy): void private function updateRelation(Content $content, $newRelations): array { $newRelations = (new Collection(Json::findArray($newRelations)))->filter(); - $currentRelations = $this->relationRepository->findRelations($content, null, true, null, false); + $currentRelations = $this->relationRepository->findRelations($content, null, null, false); $relationsResult = []; // Remove old ones diff --git a/src/Repository/RelationRepository.php b/src/Repository/RelationRepository.php index bf5cc6796..06e947448 100644 --- a/src/Repository/RelationRepository.php +++ b/src/Repository/RelationRepository.php @@ -24,46 +24,32 @@ public function __construct(ManagerRegistry $registry) parent::__construct($registry, Relation::class); } - public function findRelations(Content $from, ?string $name, bool $biDirectional = false, ?int $limit = null, bool $publishedOnly = true): array + public function findRelations(Content $from, ?string $name, ?int $limit = null, bool $publishedOnly = true): array { // Only get existing Relations from content that was persisted before if ($from->getId() === null) { return []; } - $result = $this->buildRelationQuery($from, $name, false, $publishedOnly) + $result = $this->buildRelationQuery($from, $name, $publishedOnly) ->setMaxResults($limit) ->getQuery() ->getResult(); - if (empty($result) === true && $biDirectional === true) { - $result = $this->buildRelationQuery($from, $name, true, $publishedOnly) - ->setMaxResults($limit) - ->getQuery() - ->getResult(); - } - return $result; } - public function findFirstRelation(Content $from, ?string $name, bool $biDirectional = false, bool $publishedOnly = true): ?Relation + public function findFirstRelation(Content $from, ?string $name, bool $publishedOnly = true): ?Relation { - $result = $this->buildRelationQuery($from, $name, false, $publishedOnly) + $result = $this->buildRelationQuery($from, $name, $publishedOnly) ->setMaxResults(1) ->getQuery() ->getOneOrNullResult(); - if ($result === null && $biDirectional === true) { - $result = $this->buildRelationQuery($from, $name, true, $publishedOnly) - ->setMaxResults(1) - ->getQuery() - ->getOneOrNullResult(); - } - return $result; } - private function buildRelationQuery(Content $from, ?string $name, bool $reversed = false, bool $publishedOnly = true): QueryBuilder + private function buildRelationQuery(Content $from, ?string $name, bool $publishedOnly = true): QueryBuilder { $qb = $this->createQueryBuilder('r') ->select('r, cfrom, cto') @@ -71,21 +57,16 @@ private function buildRelationQuery(Content $from, ?string $name, bool $reversed ->join('r.toContent', 'cto') ->orderBy('r.position', 'DESC'); - if ($reversed === false) { - $qb->andWhere('r.fromContent = :from'); - $cto = 'cto'; - } else { - $qb->andWhere('r.toContent = :from'); - $cto = 'cfrom'; - } + $qb->andWhere('r.fromContent = :from OR r.toContent = :from'); if ($publishedOnly === true) { - $qb->andWhere($cto . '.status = :status') + $qb->andWhere('cto.status = :status') + ->andWhere('cfrom.status = :status') ->setParameter('status', Statuses::PUBLISHED, \PDO::PARAM_STR); } if ($name !== null) { - $qb->andWhere($cto . '.contentType = :name') + $qb->andWhere('cto.contentType = :name OR cfrom.contentType = :name') ->setParameter('name', $name, \PDO::PARAM_STR); } diff --git a/src/Twig/RelatedExtension.php b/src/Twig/RelatedExtension.php index f5a432ee2..b97948db3 100644 --- a/src/Twig/RelatedExtension.php +++ b/src/Twig/RelatedExtension.php @@ -80,7 +80,7 @@ public function getRelatedContentByType(Content $content, bool $bidirectional = return []; } - $relations = $this->relationRepository->findRelations($content, null, $bidirectional, $limit, $publishedOnly); + $relations = $this->relationRepository->findRelations($content, null, $limit, $publishedOnly); return (new Collection($relations)) ->reduce(function (array $result, Relation $relation) use ($content): array { @@ -105,7 +105,7 @@ public function getRelatedContent($content, ?string $name = null, bool $bidirect return []; } - $relations = $this->relationRepository->findRelations($content, $name, $bidirectional, $limit, $publishedOnly); + $relations = $this->relationRepository->findRelations($content, $name, $limit, $publishedOnly); return (new Collection($relations)) ->map(function (Relation $relation) use ($content) { @@ -115,13 +115,13 @@ public function getRelatedContent($content, ?string $name = null, bool $bidirect ->toArray(); } - public function getFirstRelatedContent($content, ?string $name = null, bool $bidirectional = true, bool $publishedOnly = true): ?Content + public function getFirstRelatedContent($content, ?string $name = null, bool $publishedOnly = true): ?Content { if (! $this->checkforContent($content, 'related_first')) { return null; } - $relation = $this->relationRepository->findFirstRelation($content, $name, $bidirectional, $publishedOnly); + $relation = $this->relationRepository->findFirstRelation($content, $name, $publishedOnly); if ($relation === null) { return null; @@ -202,29 +202,6 @@ public function getRelatedValues(Content $source, string $contentType): Collecti return new Collection($values); } - /** - * Gets relations from this content via the content variable, and not via a doctrine query - */ - public function getRelatedValuesFromContent(Content $source, string $contentType): Collection - { - if (! $this->checkforContent($source, 'related_values')) { - return new Collection([]); - } - - if ($source->getId() === null) { - return new Collection([]); - } - - return new Collection($source->getRelationsFromThisContent() - ->filter(function (Relation $relation) use ($contentType) { - return $relation->getToContent()->getContentType() === $contentType; - })->map(function (Relation $relation) { - $toContent = $relation->getToContent(); - - return $toContent->getId(); - })->getValues()); - } - private function checkforContent($content, string $keyword): bool { if (! $content instanceof Content) { diff --git a/templates/content/_relationships.html.twig b/templates/content/_relationships.html.twig index cd3aa8cc1..43f9e308e 100644 --- a/templates/content/_relationships.html.twig +++ b/templates/content/_relationships.html.twig @@ -2,8 +2,7 @@ {% for contentType, relation in record.definition.relations %} {% set options = related_options(contentType, relation.order|default(), relation.format|default(), relation.required) %} -{# {% set value = record|related_values(contentType) %}#} - {% set value = record|related_values_from_content(contentType) %} + {% set value = record|related_values(contentType) %}
diff --git a/tests/spec/Bolt/Twig/RelatedExtensionSpec.php b/tests/spec/Bolt/Twig/RelatedExtensionSpec.php index 3effac5ee..80ce087c2 100644 --- a/tests/spec/Bolt/Twig/RelatedExtensionSpec.php +++ b/tests/spec/Bolt/Twig/RelatedExtensionSpec.php @@ -24,7 +24,7 @@ public function let(RelationRepository $relationRepository, ContentRepository $c public function it_gets_all_related_content(Content $content, RelationRepository $relationRepository, Relation $relation, Content $related): void { - $relationRepository->findRelations($content, null, true, null, true) + $relationRepository->findRelations($content, null, null, true) ->shouldBeCalledOnce() ->willReturn([$relation, $relation]); @@ -44,7 +44,7 @@ public function it_gets_all_related_content(Content $content, RelationRepository public function it_gets_related_content(Content $content, RelationRepository $relationRepository, Relation $relation, Content $related): void { - $relationRepository->findRelations($content, self::TEST_CT_SLUG, true, null, true) + $relationRepository->findRelations($content, self::TEST_CT_SLUG, null, true) ->shouldBeCalledOnce() ->willReturn([$relation]); @@ -60,7 +60,7 @@ public function it_gets_related_content(Content $content, RelationRepository $re public function it_gets_related_content_unidirectional_with_limit(Content $content, RelationRepository $relationRepository, Relation $relation, Content $related): void { - $relationRepository->findRelations($content, self::TEST_CT_SLUG, false, 3, true) + $relationRepository->findRelations($content, self::TEST_CT_SLUG, 3, true) ->shouldBeCalledOnce() ->willReturn([$relation, $relation, $relation]); @@ -77,7 +77,7 @@ public function it_gets_related_content_unidirectional_with_limit(Content $conte public function it_gets_first_related_content(Content $content, RelationRepository $relationRepository, Relation $relation, Content $related): void { - $relationRepository->findFirstRelation($content, self::TEST_CT_SLUG, true, true) + $relationRepository->findFirstRelation($content, self::TEST_CT_SLUG, true) ->shouldBeCalledOnce() ->willReturn($relation); @@ -92,7 +92,7 @@ public function it_gets_first_related_content(Content $content, RelationReposito public function it_couldnt_find_related_content(Content $content, RelationRepository $relationRepository): void { - $relationRepository->findRelations($content, null, true, null, true)->willReturn([]); + $relationRepository->findRelations($content, null, null, true)->willReturn([]); $result = $this->getRelatedContent($content); $result->shouldBeArray(); $result->shouldHaveCount(0); @@ -100,7 +100,7 @@ public function it_couldnt_find_related_content(Content $content, RelationReposi public function it_couldnt_find_first_related_content(Content $content, RelationRepository $relationRepository): void { - $relationRepository->findFirstRelation($content, null, true, true)->willReturn(null); + $relationRepository->findFirstRelation($content, null, true)->willReturn(null); $result = $this->getFirstRelatedContent($content); $result->shouldBeNull(); } From c8dc8e19b381ebe128a20877bb494b1889e7fd68 Mon Sep 17 00:00:00 2001 From: Bob den Otter Date: Tue, 11 May 2021 17:19:04 +0200 Subject: [PATCH 2/2] CSfix --- src/Repository/RelationRepository.php | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/Repository/RelationRepository.php b/src/Repository/RelationRepository.php index 06e947448..6649a26ca 100644 --- a/src/Repository/RelationRepository.php +++ b/src/Repository/RelationRepository.php @@ -31,22 +31,18 @@ public function findRelations(Content $from, ?string $name, ?int $limit = null, return []; } - $result = $this->buildRelationQuery($from, $name, $publishedOnly) + return $this->buildRelationQuery($from, $name, $publishedOnly) ->setMaxResults($limit) ->getQuery() ->getResult(); - - return $result; } public function findFirstRelation(Content $from, ?string $name, bool $publishedOnly = true): ?Relation { - $result = $this->buildRelationQuery($from, $name, $publishedOnly) + return $this->buildRelationQuery($from, $name, $publishedOnly) ->setMaxResults(1) ->getQuery() ->getOneOrNullResult(); - - return $result; } private function buildRelationQuery(Content $from, ?string $name, bool $publishedOnly = true): QueryBuilder