From fd908058e2856c58cca0d1eba582bee020145a3c Mon Sep 17 00:00:00 2001 From: Ambroise Maupate Date: Tue, 5 Mar 2024 15:30:40 +0100 Subject: [PATCH] perf: Made all foreign key columns not nullable for index performance --- composer.json | 1 + .../migrations/Version20240305133641.php | 31 +++++++ .../migrations/Version20240305134734.php | 37 +++++++++ .../migrations/Version20240305142243.php | 62 ++++++++++++++ .../NodesSourcesBreadcrumbsFactory.php | 1 - .../src/Entity/AttributeDocuments.php | 44 ++-------- .../src/Entity/AttributeValue.php | 24 ++---- .../src/Entity/CustomForm.php | 1 - .../src/Entity/CustomFormAnswer.php | 4 +- .../src/Entity/CustomFormField.php | 23 ++---- .../src/Entity/CustomFormFieldAttribute.php | 33 ++------ .../src/Entity/DocumentTranslation.php | 18 ++-- .../src/Entity/FolderTranslation.php | 14 ++-- lib/RoadizCoreBundle/src/Entity/Node.php | 18 +--- .../src/Entity/NodeTypeField.php | 68 +++------------ .../src/Entity/NodesSources.php | 27 ++---- .../src/Entity/TagTranslation.php | 82 ++++--------------- .../src/Entity/TagTranslationDocuments.php | 40 +++------ .../src/Entity/Translation.php | 2 +- lib/RoadizCoreBundle/src/Entity/UrlAlias.php | 25 +----- .../EntityHandler/CustomFormFieldHandler.php | 8 +- .../Model/AttributeGroupTranslationTrait.php | 20 ++--- .../src/Model/AttributeTranslationTrait.php | 15 ++-- .../src/Model/AttributeValueTrait.php | 7 +- .../Model/AttributeValueTranslationTrait.php | 32 +++----- lib/RoadizCoreBundle/src/Node/NodeFactory.php | 19 ++--- .../src/Node/NodeTranstyper.php | 3 +- .../src/Repository/DocumentRepository.php | 25 ++++++ .../DefaultNodesSourcesIndexingSubscriber.php | 5 +- .../NodeTypeObjectConstructor.php | 1 - .../AjaxAttributeValuesController.php | 6 +- lib/Rozier/src/Forms/NodeType.php | 2 +- lib/Rozier/src/Models/NodeModel.php | 4 +- lib/Rozier/src/Models/NodeSourceModel.php | 2 +- 34 files changed, 296 insertions(+), 408 deletions(-) create mode 100644 lib/RoadizCoreBundle/migrations/Version20240305133641.php create mode 100644 lib/RoadizCoreBundle/migrations/Version20240305134734.php create mode 100644 lib/RoadizCoreBundle/migrations/Version20240305142243.php diff --git a/composer.json b/composer.json index e13e5a3c..bc212eb4 100644 --- a/composer.json +++ b/composer.json @@ -111,6 +111,7 @@ "symfony/property-info": "6.4.*", "symfony/proxy-manager-bridge": "6.4.*", "symfony/rate-limiter": "6.4.*", + "symfony/redis-messenger": "6.4.*", "symfony/requirements-checker": "^2.0", "symfony/routing": "6.4.*", "symfony/runtime": "6.4.*", diff --git a/lib/RoadizCoreBundle/migrations/Version20240305133641.php b/lib/RoadizCoreBundle/migrations/Version20240305133641.php new file mode 100644 index 00000000..ba74548a --- /dev/null +++ b/lib/RoadizCoreBundle/migrations/Version20240305133641.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE nodes_sources CHANGE node_id node_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE nodes_sources CHANGE node_id node_id INT DEFAULT NULL, CHANGE translation_id translation_id INT DEFAULT NULL'); + } +} diff --git a/lib/RoadizCoreBundle/migrations/Version20240305134734.php b/lib/RoadizCoreBundle/migrations/Version20240305134734.php new file mode 100644 index 00000000..0bb9c334 --- /dev/null +++ b/lib/RoadizCoreBundle/migrations/Version20240305134734.php @@ -0,0 +1,37 @@ +addSql('ALTER TABLE documents_translations CHANGE translation_id translation_id INT NOT NULL, CHANGE document_id document_id INT NOT NULL'); + $this->addSql('ALTER TABLE folders_translations CHANGE folder_id folder_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL'); + $this->addSql('ALTER TABLE tags_translations CHANGE tag_id tag_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL'); + $this->addSql('ALTER TABLE tags_translations_documents CHANGE tag_translation_id tag_translation_id INT NOT NULL, CHANGE document_id document_id INT NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE documents_translations CHANGE translation_id translation_id INT DEFAULT NULL, CHANGE document_id document_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE folders_translations CHANGE folder_id folder_id INT DEFAULT NULL, CHANGE translation_id translation_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE tags_translations CHANGE tag_id tag_id INT DEFAULT NULL, CHANGE translation_id translation_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE tags_translations_documents CHANGE tag_translation_id tag_translation_id INT DEFAULT NULL, CHANGE document_id document_id INT DEFAULT NULL'); + } +} diff --git a/lib/RoadizCoreBundle/migrations/Version20240305142243.php b/lib/RoadizCoreBundle/migrations/Version20240305142243.php new file mode 100644 index 00000000..9a8f36a5 --- /dev/null +++ b/lib/RoadizCoreBundle/migrations/Version20240305142243.php @@ -0,0 +1,62 @@ +addSql('ALTER TABLE attribute_group_translations CHANGE attribute_group_id attribute_group_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL'); + $this->addSql('ALTER TABLE attribute_translations CHANGE attribute_id attribute_id INT NOT NULL, CHANGE translation_id translation_id INT NOT NULL'); + $this->addSql('ALTER TABLE attribute_value_translations CHANGE translation_id translation_id INT NOT NULL, CHANGE attribute_value attribute_value INT NOT NULL'); + $this->addSql('ALTER TABLE attribute_values CHANGE attribute_id attribute_id INT NOT NULL, CHANGE node_id node_id INT NOT NULL'); + $this->addSql('ALTER TABLE attributes_documents CHANGE attribute_id attribute_id INT NOT NULL, CHANGE document_id document_id INT NOT NULL'); + $this->addSql('ALTER TABLE custom_form_answers CHANGE custom_form_id custom_form_id INT NOT NULL'); + $this->addSql('ALTER TABLE custom_form_field_attributes CHANGE custom_form_answer_id custom_form_answer_id INT NOT NULL, CHANGE custom_form_field_id custom_form_field_id INT NOT NULL'); + $this->addSql('ALTER TABLE custom_form_fields CHANGE custom_form_id custom_form_id INT NOT NULL'); + + // Remove all node_type_fields where node_type_id is null before changing it to not-nullable + $this->addSql('DELETE FROM node_type_fields WHERE node_type_id IS NULL'); + $this->addSql('ALTER TABLE node_type_fields CHANGE node_type_id node_type_id INT NOT NULL'); + + // Remove all nodes where nodeType_id is null before changing it to not-nullable + $this->addSql('DELETE FROM nodes WHERE nodeType_id IS NULL'); + $this->addSql('ALTER TABLE nodes CHANGE nodeType_id nodeType_id INT NOT NULL'); + + // Remove all url_aliases where ns_id is null before changing it to not-nullable + $this->addSql('DELETE FROM url_aliases WHERE ns_id IS NULL'); + $this->addSql('ALTER TABLE url_aliases DROP FOREIGN KEY FK_E261ED65AA2D61'); + $this->addSql('ALTER TABLE url_aliases CHANGE ns_id ns_id INT NOT NULL'); + $this->addSql('ALTER TABLE url_aliases ADD CONSTRAINT FK_E261ED65AA2D61 FOREIGN KEY (ns_id) REFERENCES nodes_sources (id) ON DELETE CASCADE'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE attribute_group_translations CHANGE translation_id translation_id INT DEFAULT NULL, CHANGE attribute_group_id attribute_group_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE attribute_translations CHANGE translation_id translation_id INT DEFAULT NULL, CHANGE attribute_id attribute_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE attribute_value_translations CHANGE translation_id translation_id INT DEFAULT NULL, CHANGE attribute_value attribute_value INT DEFAULT NULL'); + $this->addSql('ALTER TABLE attribute_values CHANGE node_id node_id INT DEFAULT NULL, CHANGE attribute_id attribute_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE attributes_documents CHANGE attribute_id attribute_id INT DEFAULT NULL, CHANGE document_id document_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE custom_form_answers CHANGE custom_form_id custom_form_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE custom_form_field_attributes CHANGE custom_form_answer_id custom_form_answer_id INT DEFAULT NULL, CHANGE custom_form_field_id custom_form_field_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE custom_form_fields CHANGE custom_form_id custom_form_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE node_type_fields CHANGE node_type_id node_type_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE nodes CHANGE nodeType_id nodeType_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE url_aliases DROP FOREIGN KEY FK_E261ED65AA2D61'); + $this->addSql('ALTER TABLE url_aliases CHANGE ns_id ns_id INT DEFAULT NULL'); + $this->addSql('ALTER TABLE url_aliases ADD CONSTRAINT FK_E261ED65AA2D61 FOREIGN KEY (ns_id) REFERENCES nodes_sources (id) ON UPDATE NO ACTION ON DELETE NO ACTION'); + } +} diff --git a/lib/RoadizCoreBundle/src/Api/Breadcrumbs/NodesSourcesBreadcrumbsFactory.php b/lib/RoadizCoreBundle/src/Api/Breadcrumbs/NodesSourcesBreadcrumbsFactory.php index 07fcf193..6f02c639 100644 --- a/lib/RoadizCoreBundle/src/Api/Breadcrumbs/NodesSourcesBreadcrumbsFactory.php +++ b/lib/RoadizCoreBundle/src/Api/Breadcrumbs/NodesSourcesBreadcrumbsFactory.php @@ -20,7 +20,6 @@ public function create(?PersistableInterface $entity): ?BreadcrumbsInterface } if ( - null === $entity->getNode()->getNodeType() || !$entity->isReachable() ) { return null; diff --git a/lib/RoadizCoreBundle/src/Entity/AttributeDocuments.php b/lib/RoadizCoreBundle/src/Entity/AttributeDocuments.php index fed01b4b..596a3947 100644 --- a/lib/RoadizCoreBundle/src/Entity/AttributeDocuments.php +++ b/lib/RoadizCoreBundle/src/Entity/AttributeDocuments.php @@ -32,12 +32,13 @@ class AttributeDocuments extends AbstractPositioned ORM\JoinColumn( name: "attribute_id", referencedColumnName: "id", + nullable: false, onDelete: "CASCADE" ), Serializer\Exclude(), SymfonySerializer\Ignore() ] - protected ?Attribute $attribute = null; + protected Attribute $attribute; #[ ORM\ManyToOne( @@ -49,72 +50,45 @@ class AttributeDocuments extends AbstractPositioned ORM\JoinColumn( name: "document_id", referencedColumnName: "id", + nullable: false, onDelete: "CASCADE" ), Serializer\Groups(["attribute"]), SymfonySerializer\Groups(["attribute"]), Serializer\Type(Document::class) ] - protected ?Document $document = null; + protected Document $document; - /** - * @param Attribute|null $attribute - * @param Document|null $document - */ - public function __construct(Attribute $attribute = null, Document $document = null) + public function __construct(Attribute $attribute, Document $document) { $this->document = $document; $this->attribute = $attribute; } - /** - * - */ public function __clone() { if ($this->id) { $this->id = null; - $this->attribute = null; } } - /** - * Gets the value of document. - * - * @return Document|null - */ - public function getDocument(): ?Document + public function getDocument(): Document { return $this->document; } - /** - * Sets the value of document. - * - * @param Document|null $document the document - * - * @return AttributeDocuments - */ - public function setDocument(?Document $document): AttributeDocuments + public function setDocument(Document $document): AttributeDocuments { $this->document = $document; - return $this; } - /** - * @return Attribute|null - */ - public function getAttribute(): ?Attribute + public function getAttribute(): Attribute { return $this->attribute; } - /** - * @param Attribute|null $attribute - * @return AttributeDocuments - */ - public function setAttribute(?Attribute $attribute): AttributeDocuments + public function setAttribute(Attribute $attribute): AttributeDocuments { $this->attribute = $attribute; return $this; diff --git a/lib/RoadizCoreBundle/src/Entity/AttributeValue.php b/lib/RoadizCoreBundle/src/Entity/AttributeValue.php index 641fd2fa..f5df8d02 100644 --- a/lib/RoadizCoreBundle/src/Entity/AttributeValue.php +++ b/lib/RoadizCoreBundle/src/Entity/AttributeValue.php @@ -35,12 +35,9 @@ class AttributeValue extends AbstractPositioned implements AttributeValueInterfa { use AttributeValueTrait; - /** - * @var Node|null - */ #[ ORM\ManyToOne(targetEntity: Node::class, inversedBy: "attributeValues"), - ORM\JoinColumn(name: "node_id", onDelete: "CASCADE"), + ORM\JoinColumn(name: "node_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Groups(["attribute_node"]), SymfonySerializer\Groups(["attribute_node"]), SymfonySerializer\MaxDepth(1), @@ -55,7 +52,7 @@ class AttributeValue extends AbstractPositioned implements AttributeValueInterfa "node.visible" ]) ] - protected ?Node $node = null; + protected Node $node; #[ORM\ManyToOne(targetEntity: Realm::class)] #[ORM\JoinColumn( @@ -85,10 +82,7 @@ public function getPosition(): float return $this->position; } - /** - * @inheritDoc - */ - public function getAttributable(): ?AttributableInterface + public function getAttributable(): Node { return $this->node; } @@ -105,20 +99,12 @@ public function setAttributable(?AttributableInterface $attributable) throw new \InvalidArgumentException('Attributable have to be an instance of Node.'); } - /** - * @return Node|null - */ - public function getNode(): ?Node + public function getNode(): Node { return $this->node; } - /** - * @param Node|null $node - * - * @return AttributeValue - */ - public function setNode(?Node $node): AttributeValue + public function setNode(Node $node): AttributeValue { $this->node = $node; diff --git a/lib/RoadizCoreBundle/src/Entity/CustomForm.php b/lib/RoadizCoreBundle/src/Entity/CustomForm.php index 61befe69..d6b900aa 100644 --- a/lib/RoadizCoreBundle/src/Entity/CustomForm.php +++ b/lib/RoadizCoreBundle/src/Entity/CustomForm.php @@ -339,7 +339,6 @@ public function removeField(CustomFormField $field): CustomForm { if ($this->getFields()->contains($field)) { $this->getFields()->removeElement($field); - $field->setCustomForm(null); } return $this; diff --git a/lib/RoadizCoreBundle/src/Entity/CustomFormAnswer.php b/lib/RoadizCoreBundle/src/Entity/CustomFormAnswer.php index 40121188..e43de922 100644 --- a/lib/RoadizCoreBundle/src/Entity/CustomFormAnswer.php +++ b/lib/RoadizCoreBundle/src/Entity/CustomFormAnswer.php @@ -56,11 +56,11 @@ class CustomFormAnswer extends AbstractEntity targetEntity: CustomForm::class, inversedBy: "customFormAnswers" ), - ORM\JoinColumn(name: "custom_form_id", referencedColumnName: "id", onDelete: "CASCADE"), + ORM\JoinColumn(name: "custom_form_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Exclude, SymfonySerializer\Ignore ] - private ?CustomForm $customForm = null; + private CustomForm $customForm; public function __construct() { diff --git a/lib/RoadizCoreBundle/src/Entity/CustomFormField.php b/lib/RoadizCoreBundle/src/Entity/CustomFormField.php index f7b90f28..d1c6ee20 100644 --- a/lib/RoadizCoreBundle/src/Entity/CustomFormField.php +++ b/lib/RoadizCoreBundle/src/Entity/CustomFormField.php @@ -50,11 +50,11 @@ class CustomFormField extends AbstractField #[ ORM\ManyToOne(targetEntity: CustomForm::class, inversedBy: "fields"), - ORM\JoinColumn(name: "custom_form_id", referencedColumnName: "id", onDelete: "CASCADE"), + ORM\JoinColumn(name: "custom_form_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Exclude, SymfonySerializer\Ignore ] - private ?CustomForm $customForm = null; + private CustomForm $customForm; /** * @var Collection @@ -84,7 +84,7 @@ public function __construct() * * @return $this */ - public function setLabel($label) + public function setLabel($label): CustomFormField { parent::setLabel($label); $this->setName($label); @@ -92,25 +92,15 @@ public function setLabel($label) return $this; } - /** - * @return CustomForm|null - */ - public function getCustomForm(): ?CustomForm + public function getCustomForm(): CustomForm { return $this->customForm; } - /** - * @param CustomForm|null $customForm - * - * @return $this - */ - public function setCustomForm(CustomForm $customForm = null): CustomFormField + public function setCustomForm(CustomForm $customForm): CustomFormField { $this->customForm = $customForm; - if (null !== $customForm) { - $this->customForm->addField($this); - } + $this->customForm->addField($this); return $this; } @@ -162,7 +152,6 @@ public function __clone() { if ($this->id) { $this->id = null; - $this->customForm = null; $this->customFormFieldAttributes = new ArrayCollection(); } } diff --git a/lib/RoadizCoreBundle/src/Entity/CustomFormFieldAttribute.php b/lib/RoadizCoreBundle/src/Entity/CustomFormFieldAttribute.php index 63c441d1..65ec6285 100644 --- a/lib/RoadizCoreBundle/src/Entity/CustomFormFieldAttribute.php +++ b/lib/RoadizCoreBundle/src/Entity/CustomFormFieldAttribute.php @@ -24,15 +24,15 @@ class CustomFormFieldAttribute extends AbstractEntity { #[ ORM\ManyToOne(targetEntity: CustomFormAnswer::class, inversedBy: "answerFields"), - ORM\JoinColumn(name: "custom_form_answer_id", referencedColumnName: "id", onDelete: "CASCADE") + ORM\JoinColumn(name: "custom_form_answer_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE") ] - protected ?CustomFormAnswer $customFormAnswer = null; + protected CustomFormAnswer $customFormAnswer; #[ ORM\ManyToOne(targetEntity: CustomFormField::class, inversedBy: "customFormFieldAttributes"), - ORM\JoinColumn(name: "custom_form_field_id", referencedColumnName: "id", onDelete: "CASCADE") + ORM\JoinColumn(name: "custom_form_field_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE") ] - protected ?CustomFormField $customFormField = null; + protected CustomFormField $customFormField; /** * @var Collection @@ -83,23 +83,11 @@ public function setValue(?string $value): CustomFormFieldAttribute return $this; } - /** - * Gets the value of customFormAnswer. - * - * @return CustomFormAnswer|null - */ - public function getCustomFormAnswer(): ?CustomFormAnswer + public function getCustomFormAnswer(): CustomFormAnswer { return $this->customFormAnswer; } - /** - * Sets the value of customFormAnswer. - * - * @param CustomFormAnswer $customFormAnswer the custom form answer - * - * @return self - */ public function setCustomFormAnswer(CustomFormAnswer $customFormAnswer): CustomFormFieldAttribute { $this->customFormAnswer = $customFormAnswer; @@ -116,20 +104,11 @@ public function __toString(): string return $this->getValue() ?? ''; } - /** - * @return CustomFormField|null - */ - public function getCustomFormField(): ?CustomFormField + public function getCustomFormField(): CustomFormField { return $this->customFormField; } - /** - * Sets the value of customFormField. - * - * @param CustomFormField $customFormField the custom form field - * @return self - */ public function setCustomFormField(CustomFormField $customFormField): CustomFormFieldAttribute { $this->customFormField = $customFormField; diff --git a/lib/RoadizCoreBundle/src/Entity/DocumentTranslation.php b/lib/RoadizCoreBundle/src/Entity/DocumentTranslation.php index 858d529a..e7ac1609 100644 --- a/lib/RoadizCoreBundle/src/Entity/DocumentTranslation.php +++ b/lib/RoadizCoreBundle/src/Entity/DocumentTranslation.php @@ -43,16 +43,16 @@ class DocumentTranslation extends AbstractEntity implements Loggable protected ?string $externalUrl = null; #[ORM\ManyToOne(targetEntity: Translation::class, fetch: 'EXTRA_LAZY', inversedBy: 'documentTranslations')] - #[ORM\JoinColumn(name: 'translation_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'translation_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Groups(['document', 'nodes_sources', 'tag', 'attribute'])] #[Serializer\Groups(['document', 'nodes_sources', 'tag', 'attribute'])] - protected ?TranslationInterface $translation = null; + protected TranslationInterface $translation; #[ORM\ManyToOne(targetEntity: Document::class, fetch: 'EXTRA_LAZY', inversedBy: 'documentTranslations')] - #[ORM\JoinColumn(name: 'document_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'document_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Ignore] #[Serializer\Exclude] - protected ?DocumentInterface $document; + protected DocumentInterface $document; #[ORM\Column(type: 'text', nullable: true)] #[SymfonySerializer\Groups(['document', 'nodes_sources', 'tag', 'attribute'])] @@ -79,19 +79,11 @@ public function setName(?string $name): DocumentTranslation return $this; } - /** - * @return string - */ public function getDescription(): ?string { return $this->description; } - /** - * @param string|null $description - * - * @return $this - */ public function setDescription(?string $description): DocumentTranslation { $this->description = $description; @@ -167,7 +159,7 @@ public function getDocument(): DocumentInterface * @param DocumentInterface $document * @return $this */ - public function setDocument(DocumentInterface $document) + public function setDocument(DocumentInterface $document): DocumentTranslation { $this->document = $document; return $this; diff --git a/lib/RoadizCoreBundle/src/Entity/FolderTranslation.php b/lib/RoadizCoreBundle/src/Entity/FolderTranslation.php index b827d27e..76a78b84 100644 --- a/lib/RoadizCoreBundle/src/Entity/FolderTranslation.php +++ b/lib/RoadizCoreBundle/src/Entity/FolderTranslation.php @@ -33,21 +33,17 @@ class FolderTranslation extends AbstractEntity protected string $name = ''; #[ORM\ManyToOne(targetEntity: Folder::class, inversedBy: 'translatedFolders')] - #[ORM\JoinColumn(name: 'folder_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'folder_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Ignore] #[Serializer\Exclude] - protected ?Folder $folder = null; + protected Folder $folder; #[ORM\ManyToOne(targetEntity: Translation::class, fetch: 'EXTRA_LAZY', inversedBy: 'folderTranslations')] - #[ORM\JoinColumn(name: 'translation_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'translation_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Groups(['folder', 'document'])] #[Serializer\Groups(['folder', 'document'])] - protected ?TranslationInterface $translation = null; + protected TranslationInterface $translation; - /** - * @param Folder $original - * @param TranslationInterface $translation - */ public function __construct(Folder $original, TranslationInterface $translation) { $this->setFolder($original); @@ -67,7 +63,7 @@ public function getName(): string * @param string $name * @return $this */ - public function setName(string $name) + public function setName(string $name): FolderTranslation { $this->name = $name; return $this; diff --git a/lib/RoadizCoreBundle/src/Entity/Node.php b/lib/RoadizCoreBundle/src/Entity/Node.php index a9016794..13282531 100644 --- a/lib/RoadizCoreBundle/src/Entity/Node.php +++ b/lib/RoadizCoreBundle/src/Entity/Node.php @@ -198,15 +198,12 @@ class Node extends AbstractDateTimedPositioned implements LeafInterface, Attribu )] private string $childrenOrderDirection = 'ASC'; - /** - * @var NodeTypeInterface|null - */ #[ORM\ManyToOne(targetEntity: NodeTypeInterface::class)] - #[ORM\JoinColumn(name: 'nodeType_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'nodeType_id', referencedColumnName: 'id', nullable:false, onDelete: 'CASCADE')] #[SymfonySerializer\Groups(['node'])] #[Serializer\Groups(['node'])] #[SymfonySerializer\Ignore] - private ?NodeTypeInterface $nodeType = null; + private NodeTypeInterface $nodeType; /** * @var Node|null @@ -905,19 +902,12 @@ public function setNodeName(string $nodeName): static return $this; } - /** - * @return NodeTypeInterface|null - */ - public function getNodeType(): ?NodeTypeInterface + public function getNodeType(): NodeTypeInterface { return $this->nodeType; } - /** - * @param NodeTypeInterface|null $nodeType - * @return $this - */ - public function setNodeType(?NodeTypeInterface $nodeType = null): static + public function setNodeType(NodeTypeInterface $nodeType): Node { $this->nodeType = $nodeType; return $this; diff --git a/lib/RoadizCoreBundle/src/Entity/NodeTypeField.php b/lib/RoadizCoreBundle/src/Entity/NodeTypeField.php index 34d6fed3..3fb48538 100644 --- a/lib/RoadizCoreBundle/src/Entity/NodeTypeField.php +++ b/lib/RoadizCoreBundle/src/Entity/NodeTypeField.php @@ -74,11 +74,11 @@ class NodeTypeField extends AbstractField implements NodeTypeFieldInterface, Ser #[ ORM\ManyToOne(targetEntity: NodeType::class, inversedBy: "fields"), - ORM\JoinColumn(name: "node_type_id", onDelete: "CASCADE"), + ORM\JoinColumn(name: "node_type_id", nullable: false, onDelete: "CASCADE"), Serializer\Exclude(), SymfonySerializer\Ignore ] - private ?NodeTypeInterface $nodeType = null; + private NodeTypeInterface $nodeType; #[ Serializer\Groups(["node_type"]), @@ -152,46 +152,28 @@ class NodeTypeField extends AbstractField implements NodeTypeFieldInterface, Ser ] public function getNodeTypeName(): string { - return $this->getNodeType() ? $this->getNodeType()->getName() : ''; + return $this->getNodeType()->getName(); } - /** - * @return NodeTypeInterface|null - */ - public function getNodeType(): ?NodeTypeInterface + public function getNodeType(): NodeTypeInterface { return $this->nodeType; } - /** - * @param NodeTypeInterface|null $nodeType - * - * @return $this - */ - public function setNodeType(?NodeTypeInterface $nodeType) + public function setNodeType(NodeTypeInterface $nodeType): NodeTypeField { $this->nodeType = $nodeType; - return $this; } - /** - * @return int|null - */ public function getMinLength(): ?int { return $this->minLength; } - /** - * @param int|null $minLength - * - * @return $this - */ - public function setMinLength(?int $minLength) + public function setMinLength(?int $minLength): NodeTypeField { $this->minLength = $minLength; - return $this; } @@ -203,15 +185,9 @@ public function getMaxLength(): ?int return $this->maxLength; } - /** - * @param int|null $maxLength - * - * @return $this - */ - public function setMaxLength(?int $maxLength) + public function setMaxLength(?int $maxLength): NodeTypeField { $this->maxLength = $maxLength; - return $this; } @@ -229,7 +205,7 @@ public function isSearchable(): bool * @return string */ #[SymfonySerializer\Ignore] - public function getOneLineSummary() + public function getOneLineSummary(): string { return $this->getId() . " — " . $this->getLabel() . ' [' . $this->getName() . ']' . ' - ' . $this->getTypeName() . @@ -246,11 +222,7 @@ public function isIndexed(): bool return $this->indexed && $this->getDoctrineType() !== 'json'; } - /** - * @param bool $indexed - * @return $this - */ - public function setIndexed(bool $indexed) + public function setIndexed(bool $indexed): NodeTypeField { $this->indexed = $indexed; return $this; @@ -264,14 +236,9 @@ public function isVisible(): bool return $this->visible; } - /** - * @param bool $visible - * @return $this - */ - public function setVisible(bool $visible) + public function setVisible(bool $visible): NodeTypeField { $this->visible = $visible; - return $this; } @@ -296,7 +263,7 @@ public function getUniversal(): bool * @param bool $universal * @return NodeTypeField */ - public function setUniversal(bool $universal) + public function setUniversal(bool $universal): NodeTypeField { $this->universal = $universal; return $this; @@ -318,29 +285,18 @@ public function getExcludeFromSearch(): bool return $this->excludeFromSearch; } - /** - * @return bool - */ public function isExcludeFromSearch(): bool { return $this->getExcludeFromSearch(); } - /** - * @param bool $excludeFromSearch - * - * @return NodeTypeField - */ - public function setExcludeFromSearch(bool $excludeFromSearch) + public function setExcludeFromSearch(bool $excludeFromSearch): NodeTypeField { $this->excludeFromSearch = $excludeFromSearch; return $this; } - /** - * @return string|null - */ public function getSerializationExclusionExpression(): ?string { return $this->serializationExclusionExpression; diff --git a/lib/RoadizCoreBundle/src/Entity/NodesSources.php b/lib/RoadizCoreBundle/src/Entity/NodesSources.php index e07d5c81..29bccd26 100644 --- a/lib/RoadizCoreBundle/src/Entity/NodesSources.php +++ b/lib/RoadizCoreBundle/src/Entity/NodesSources.php @@ -5,9 +5,9 @@ namespace RZ\Roadiz\CoreBundle\Entity; use ApiPlatform\Doctrine\Orm\Filter as BaseFilter; +use ApiPlatform\Metadata\ApiFilter; use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Serializer\Filter\PropertyFilter; -use ApiPlatform\Metadata\ApiFilter; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\Criteria; @@ -16,7 +16,6 @@ use Gedmo\Loggable\Loggable; use Gedmo\Mapping\Annotation as Gedmo; use JMS\Serializer\Annotation as Serializer; -use RuntimeException; use RZ\Roadiz\Core\AbstractEntities\AbstractEntity; use RZ\Roadiz\Core\AbstractEntities\TranslationInterface; use RZ\Roadiz\CoreBundle\Api\Filter as RoadizFilter; @@ -168,21 +167,23 @@ class NodesSources extends AbstractEntity implements Loggable "node.nodesTags.tag.tagName", ])] #[ORM\ManyToOne(targetEntity: Node::class, cascade: ['persist'], fetch: 'EAGER', inversedBy: 'nodeSources')] - #[ORM\JoinColumn(name: 'node_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'node_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Groups(['nodes_sources', 'nodes_sources_base', 'log_sources'])] #[Serializer\Groups(['nodes_sources', 'nodes_sources_base', 'log_sources'])] #[Assert\Valid] - private ?Node $node = null; + #[Assert\NotNull] + private Node $node; #[ApiFilter(BaseFilter\SearchFilter::class, properties: [ "translation.id" => "exact", "translation.locale" => "exact", ])] #[ORM\ManyToOne(targetEntity: Translation::class, inversedBy: 'nodeSources')] - #[ORM\JoinColumn(name: 'translation_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'translation_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Groups(['translation_base'])] #[Serializer\Groups(['translation_base'])] - private ?TranslationInterface $translation = null; + #[Assert\NotNull] + private TranslationInterface $translation; /** * @var Collection @@ -236,22 +237,11 @@ public function preUpdate(): void $this->getNode()->setUpdatedAt(new \DateTime("now")); } - /** - * @return Node - */ public function getNode(): Node { - if (null === $this->node) { - throw new \BadMethodCallException('NodeSource node should never be null.'); - } return $this->node; } - /** - * @param Node $node - * - * @return $this - */ public function setNode(Node $node): NodesSources { $this->node = $node; @@ -575,9 +565,6 @@ public function setTitle(?string $title): NodesSources */ public function getTranslation(): TranslationInterface { - if (null === $this->translation) { - throw new RuntimeException('Node source translation cannot be null.'); - } return $this->translation; } diff --git a/lib/RoadizCoreBundle/src/Entity/TagTranslation.php b/lib/RoadizCoreBundle/src/Entity/TagTranslation.php index b8aa78f1..36017fe1 100644 --- a/lib/RoadizCoreBundle/src/Entity/TagTranslation.php +++ b/lib/RoadizCoreBundle/src/Entity/TagTranslation.php @@ -45,16 +45,16 @@ class TagTranslation extends AbstractEntity protected ?string $description = null; #[ORM\ManyToOne(targetEntity: Tag::class, inversedBy: 'translatedTags')] - #[ORM\JoinColumn(name: 'tag_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'tag_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Ignore] #[Serializer\Exclude] - protected ?Tag $tag = null; + protected Tag $tag; #[ORM\ManyToOne(targetEntity: Translation::class, fetch: 'EXTRA_LAZY', inversedBy: 'tagTranslations')] - #[ORM\JoinColumn(name: 'translation_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'translation_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Groups(['tag', 'node', 'nodes_sources'])] #[Serializer\Groups(['tag', 'node', 'nodes_sources'])] - protected ?TranslationInterface $translation = null; + protected TranslationInterface $translation; /** * @var Collection @@ -73,33 +73,22 @@ class TagTranslation extends AbstractEntity /** * Create a new TagTranslation with its origin Tag and Translation. * - * @param Tag|null $original - * @param TranslationInterface|null $translation + * @param Tag $original + * @param TranslationInterface $translation */ - public function __construct(Tag $original = null, TranslationInterface $translation = null) + public function __construct(Tag $original, TranslationInterface $translation) { $this->setTag($original); $this->setTranslation($translation); $this->tagTranslationDocuments = new ArrayCollection(); - - if (null !== $original) { - $this->name = $original->getDirtyTagName() != '' ? $original->getDirtyTagName() : $original->getTagName(); - } + $this->name = $original->getDirtyTagName() != '' ? $original->getDirtyTagName() : $original->getTagName(); } - /** - * @return string - */ public function getName(): string { return $this->name; } - /** - * @param string|null $name - * - * @return $this - */ public function setName(?string $name): TagTranslation { $this->name = $name ?? ''; @@ -107,19 +96,11 @@ public function setName(?string $name): TagTranslation return $this; } - /** - * @return string - */ public function getDescription(): ?string { return $this->description; } - /** - * @param string|null $description - * - * @return $this - */ public function setDescription(?string $description): TagTranslation { $this->description = $description; @@ -127,48 +108,23 @@ public function setDescription(?string $description): TagTranslation return $this; } - /** - * Gets the value of tag. - * - * @return Tag - */ - public function getTag(): ?Tag + public function getTag(): Tag { return $this->tag; } - /** - * Sets the value of tag. - * - * @param Tag|null $tag the tag - * - * @return self - */ - public function setTag(?Tag $tag): TagTranslation + public function setTag(Tag $tag): TagTranslation { $this->tag = $tag; - return $this; } - /** - * Gets the value of translation. - * - * @return TranslationInterface|null - */ - public function getTranslation(): ?TranslationInterface + public function getTranslation(): TranslationInterface { return $this->translation; } - /** - * Sets the value of translation. - * - * @param TranslationInterface|null $translation the translation - * - * @return self - */ - public function setTranslation(?TranslationInterface $translation): TagTranslation + public function setTranslation(TranslationInterface $translation): TagTranslation { $this->translation = $translation; @@ -186,14 +142,12 @@ public function __clone() if ($this->id) { $this->id = null; $documents = $this->getDocuments(); - if ($documents !== null) { - $this->tagTranslationDocuments = new ArrayCollection(); - /** @var TagTranslationDocuments $document */ - foreach ($documents as $document) { - $cloneDocument = clone $document; - $this->tagTranslationDocuments->add($cloneDocument); - $cloneDocument->setTagTranslation($this); - } + $this->tagTranslationDocuments = new ArrayCollection(); + /** @var TagTranslationDocuments $document */ + foreach ($documents as $document) { + $cloneDocument = clone $document; + $this->tagTranslationDocuments->add($cloneDocument); + $cloneDocument->setTagTranslation($this); } } } diff --git a/lib/RoadizCoreBundle/src/Entity/TagTranslationDocuments.php b/lib/RoadizCoreBundle/src/Entity/TagTranslationDocuments.php index ca765fb0..d06683da 100644 --- a/lib/RoadizCoreBundle/src/Entity/TagTranslationDocuments.php +++ b/lib/RoadizCoreBundle/src/Entity/TagTranslationDocuments.php @@ -28,10 +28,10 @@ class TagTranslationDocuments extends AbstractPositioned fetch: 'EAGER', inversedBy: 'tagTranslationDocuments' )] - #[ORM\JoinColumn(name: 'tag_translation_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'tag_translation_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Ignore] #[Serializer\Exclude] - protected ?TagTranslation $tagTranslation = null; + protected TagTranslation $tagTranslation; #[ORM\ManyToOne( targetEntity: Document::class, @@ -39,18 +39,18 @@ class TagTranslationDocuments extends AbstractPositioned fetch: 'EAGER', inversedBy: 'tagTranslations' )] - #[ORM\JoinColumn(name: 'document_id', referencedColumnName: 'id', onDelete: 'CASCADE')] + #[ORM\JoinColumn(name: 'document_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Groups(['tag'])] #[Serializer\Groups(['tag'])] - protected ?Document $document = null; + protected Document $document; /** * Create a new relation between NodeSource, a Document and a NodeTypeField. * - * @param TagTranslation|null $tagTranslation - * @param Document|null $document + * @param TagTranslation $tagTranslation + * @param Document $document */ - public function __construct(TagTranslation $tagTranslation = null, Document $document = null) + public function __construct(TagTranslation $tagTranslation, Document $document) { $this->document = $document; $this->tagTranslation = $tagTranslation; @@ -60,44 +60,26 @@ public function __clone() { if ($this->id) { $this->id = null; - $this->tagTranslation = null; } } - /** - * Gets the value of document. - * - * @return Document|null - */ - public function getDocument(): ?Document + public function getDocument(): Document { return $this->document; } - /** - * Sets the value of document. - * - * @param Document|null $document the document - * - * @return self - */ - public function setDocument(?Document $document): TagTranslationDocuments + public function setDocument(Document $document): TagTranslationDocuments { $this->document = $document; - return $this; } - public function getTagTranslation(): ?TagTranslation + public function getTagTranslation(): TagTranslation { return $this->tagTranslation; } - /** - * @param TagTranslation|null $tagTranslation - * @return TagTranslationDocuments - */ - public function setTagTranslation(?TagTranslation $tagTranslation): TagTranslationDocuments + public function setTagTranslation(TagTranslation $tagTranslation): TagTranslationDocuments { $this->tagTranslation = $tagTranslation; return $this; diff --git a/lib/RoadizCoreBundle/src/Entity/Translation.php b/lib/RoadizCoreBundle/src/Entity/Translation.php index 1fba916d..549360e8 100644 --- a/lib/RoadizCoreBundle/src/Entity/Translation.php +++ b/lib/RoadizCoreBundle/src/Entity/Translation.php @@ -553,7 +553,7 @@ class Translation extends AbstractDateTimed implements TranslationInterface * @Serializer\Groups({"translation", "document", "nodes_sources", "tag", "attribute", "folder", "log_sources"}) * @Serializer\Type("string") */ - #[ORM\Column(type: 'string', length: 10, unique: true)] + #[ORM\Column(type: 'string', length: 10, unique: true, nullable: false)] #[SymfonySerializer\Ignore] #[Assert\NotBlank] #[Assert\NotNull] diff --git a/lib/RoadizCoreBundle/src/Entity/UrlAlias.php b/lib/RoadizCoreBundle/src/Entity/UrlAlias.php index 8298c4de..9f713fef 100644 --- a/lib/RoadizCoreBundle/src/Entity/UrlAlias.php +++ b/lib/RoadizCoreBundle/src/Entity/UrlAlias.php @@ -32,20 +32,10 @@ class UrlAlias extends AbstractEntity private string $alias = ''; #[ORM\ManyToOne(targetEntity: NodesSources::class, inversedBy: 'urlAliases')] - #[ORM\JoinColumn(name: 'ns_id', referencedColumnName: 'id')] + #[ORM\JoinColumn(name: 'ns_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')] #[SymfonySerializer\Ignore] #[Serializer\Exclude] - private ?NodesSources $nodeSource = null; - - /** - * Create a new UrlAlias linked to a NodeSource. - * - * @param NodesSources|null $nodeSource - */ - public function __construct(?NodesSources $nodeSource = null) - { - $this->setNodeSource($nodeSource); - } + private NodesSources $nodeSource; /** * @return string @@ -66,19 +56,12 @@ public function setAlias(string $alias): UrlAlias return $this; } - /** - * @return NodesSources|null - */ - public function getNodeSource(): ?NodesSources + public function getNodeSource(): NodesSources { return $this->nodeSource; } - /** - * @param NodesSources|null $nodeSource - * @return $this - */ - public function setNodeSource(?NodesSources $nodeSource): UrlAlias + public function setNodeSource(NodesSources $nodeSource): UrlAlias { $this->nodeSource = $nodeSource; return $this; diff --git a/lib/RoadizCoreBundle/src/EntityHandler/CustomFormFieldHandler.php b/lib/RoadizCoreBundle/src/EntityHandler/CustomFormFieldHandler.php index 90576601..5af55d5f 100644 --- a/lib/RoadizCoreBundle/src/EntityHandler/CustomFormFieldHandler.php +++ b/lib/RoadizCoreBundle/src/EntityHandler/CustomFormFieldHandler.php @@ -52,11 +52,7 @@ public function cleanPositions(bool $setPositions = true): float throw new \BadMethodCallException('CustomForm is null'); } - if ($this->customFormField->getCustomForm() !== null) { - $this->customFormHandler->setCustomForm($this->customFormField->getCustomForm()); - return $this->customFormHandler->cleanFieldsPositions($setPositions); - } - - return 1; + $this->customFormHandler->setCustomForm($this->customFormField->getCustomForm()); + return $this->customFormHandler->cleanFieldsPositions($setPositions); } } diff --git a/lib/RoadizCoreBundle/src/Model/AttributeGroupTranslationTrait.php b/lib/RoadizCoreBundle/src/Model/AttributeGroupTranslationTrait.php index f89961f2..34ec8324 100644 --- a/lib/RoadizCoreBundle/src/Model/AttributeGroupTranslationTrait.php +++ b/lib/RoadizCoreBundle/src/Model/AttributeGroupTranslationTrait.php @@ -13,12 +13,12 @@ trait AttributeGroupTranslationTrait { #[ ORM\ManyToOne(targetEntity: "RZ\Roadiz\Core\AbstractEntities\TranslationInterface"), - ORM\JoinColumn(name: "translation_id", onDelete: "CASCADE"), + ORM\JoinColumn(name: "translation_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Groups(["attribute_group", "attribute", "node", "nodes_sources"]), Serializer\Type("RZ\Roadiz\Core\AbstractEntities\TranslationInterface"), Serializer\Accessor(getter: "getTranslation", setter: "setTranslation") ] - protected ?TranslationInterface $translation = null; + protected TranslationInterface $translation; #[ ORM\Column(type: "string", length: 255, unique: false, nullable: false), @@ -30,10 +30,10 @@ trait AttributeGroupTranslationTrait #[ ORM\ManyToOne(targetEntity: AttributeGroupInterface::class, cascade: ["persist"], inversedBy: "attributeGroupTranslations"), - ORM\JoinColumn(name: "attribute_group_id", referencedColumnName: "id", nullable: true, onDelete: "CASCADE"), + ORM\JoinColumn(name: "attribute_group_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Exclude ] - protected ?AttributeGroupInterface $attributeGroup = null; + protected AttributeGroupInterface $attributeGroup; /** * @return string @@ -45,7 +45,6 @@ public function getName(): string /** * @param string $value - * * @return self */ public function setName(string $value) @@ -56,8 +55,7 @@ public function setName(string $value) /** * @param TranslationInterface $translation - * - * @return mixed + * @return self */ public function setTranslation(TranslationInterface $translation) { @@ -65,10 +63,7 @@ public function setTranslation(TranslationInterface $translation) return $this; } - /** - * @return TranslationInterface|null - */ - public function getTranslation(): ?TranslationInterface + public function getTranslation(): TranslationInterface { return $this->translation; } @@ -83,8 +78,7 @@ public function getAttributeGroup(): AttributeGroupInterface /** * @param AttributeGroupInterface $attributeGroup - * - * @return mixed + * @return self */ public function setAttributeGroup(AttributeGroupInterface $attributeGroup) { diff --git a/lib/RoadizCoreBundle/src/Model/AttributeTranslationTrait.php b/lib/RoadizCoreBundle/src/Model/AttributeTranslationTrait.php index b415ee86..9926436f 100644 --- a/lib/RoadizCoreBundle/src/Model/AttributeTranslationTrait.php +++ b/lib/RoadizCoreBundle/src/Model/AttributeTranslationTrait.php @@ -13,12 +13,12 @@ trait AttributeTranslationTrait { #[ ORM\ManyToOne(targetEntity: TranslationInterface::class), - ORM\JoinColumn(onDelete: "CASCADE"), + ORM\JoinColumn(name: "translation_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Groups(["attribute", "node", "nodes_sources"]), Serializer\Type("RZ\Roadiz\Core\AbstractEntities\TranslationInterface"), Serializer\Accessor(getter: "getTranslation", setter: "setTranslation") ] - protected ?TranslationInterface $translation = null; + protected TranslationInterface $translation; #[ ORM\Column(type: "string", length: 250, unique: false, nullable: false), @@ -40,10 +40,10 @@ trait AttributeTranslationTrait #[ ORM\ManyToOne(targetEntity: AttributeInterface::class, cascade: ["persist"], inversedBy: "attributeTranslations"), - ORM\JoinColumn(referencedColumnName: "id", onDelete: "CASCADE"), + ORM\JoinColumn(name: "attribute_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Exclude ] - protected ?AttributeInterface $attribute = null; + protected AttributeInterface $attribute; /** * @return string|null @@ -66,7 +66,6 @@ public function setLabel(?string $label) /** * @param TranslationInterface $translation - * * @return $this */ public function setTranslation(TranslationInterface $translation) @@ -75,10 +74,7 @@ public function setTranslation(TranslationInterface $translation) return $this; } - /** - * @return TranslationInterface|null - */ - public function getTranslation(): ?TranslationInterface + public function getTranslation(): TranslationInterface { return $this->translation; } @@ -93,7 +89,6 @@ public function getAttribute(): AttributeInterface /** * @param AttributeInterface $attribute - * * @return $this */ public function setAttribute(AttributeInterface $attribute) diff --git a/lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php b/lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php index bf1dff3c..887475cd 100644 --- a/lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php +++ b/lib/RoadizCoreBundle/src/Model/AttributeValueTrait.php @@ -15,7 +15,7 @@ trait AttributeValueTrait { #[ ORM\ManyToOne(targetEntity: AttributeInterface::class, fetch: "EAGER", inversedBy: "attributeValues"), - ORM\JoinColumn(name: "attribute_id", referencedColumnName: "id", onDelete: "CASCADE"), + ORM\JoinColumn(name: "attribute_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Groups(["attribute", "node", "nodes_sources"]), Serializer\Type("RZ\Roadiz\CoreBundle\Entity\Attribute"), ApiFilter(BaseFilter\SearchFilter::class, properties: [ @@ -30,7 +30,7 @@ trait AttributeValueTrait "attribute.searchable" ]) ] - protected ?AttributeInterface $attribute = null; + protected AttributeInterface $attribute; /** * @var Collection @@ -68,8 +68,7 @@ public function getAttribute(): ?AttributeInterface /** * @param AttributeInterface $attribute - * - * @return static + * @return self */ public function setAttribute(AttributeInterface $attribute) { diff --git a/lib/RoadizCoreBundle/src/Model/AttributeValueTranslationTrait.php b/lib/RoadizCoreBundle/src/Model/AttributeValueTranslationTrait.php index 0e038563..5cb95d26 100644 --- a/lib/RoadizCoreBundle/src/Model/AttributeValueTranslationTrait.php +++ b/lib/RoadizCoreBundle/src/Model/AttributeValueTranslationTrait.php @@ -13,12 +13,12 @@ trait AttributeValueTranslationTrait { #[ ORM\ManyToOne(targetEntity: TranslationInterface::class), - ORM\JoinColumn(name: "translation_id", referencedColumnName: "id", onDelete: "CASCADE"), + ORM\JoinColumn(name: "translation_id", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Groups(["attribute", "node", "nodes_sources"]), Serializer\Type("RZ\Roadiz\Core\AbstractEntities\TranslationInterface"), Serializer\Accessor(getter: "getTranslation", setter: "setTranslation") ] - protected ?TranslationInterface $translation = null; + protected TranslationInterface $translation; #[ ORM\Column(type: "string", length: 255, unique: false, nullable: true), @@ -30,16 +30,16 @@ trait AttributeValueTranslationTrait #[ ORM\ManyToOne(targetEntity: AttributeValueInterface::class, cascade: ["persist"], inversedBy: "attributeValueTranslations"), - ORM\JoinColumn(name: "attribute_value", referencedColumnName: "id", onDelete: "CASCADE"), + ORM\JoinColumn(name: "attribute_value", referencedColumnName: "id", nullable: false, onDelete: "CASCADE"), Serializer\Exclude ] - protected ?AttributeValueInterface $attributeValue = null; + protected AttributeValueInterface $attributeValue; /** * @return bool|\DateTime|float|int|string|null * @throws \Exception */ - public function getValue() + public function getValue(): bool|\DateTime|float|int|string|null { if (null === $this->value) { return null; @@ -56,9 +56,9 @@ public function getValue() /** * @param mixed|null $value * - * @return static + * @return self */ - public function setValue($value) + public function setValue(mixed $value) { if (null === $value) { $this->value = null; @@ -86,8 +86,7 @@ public function setValue($value) /** * @param TranslationInterface $translation - * - * @return static + * @return self */ public function setTranslation(TranslationInterface $translation) { @@ -95,10 +94,7 @@ public function setTranslation(TranslationInterface $translation) return $this; } - /** - * @return TranslationInterface|null - */ - public function getTranslation(): ?TranslationInterface + public function getTranslation(): TranslationInterface { return $this->translation; } @@ -113,19 +109,15 @@ public function getAttributeValue(): AttributeValueInterface /** * @param AttributeValueInterface $attributeValue - * - * @return static + * @return self */ public function setAttributeValue(AttributeValueInterface $attributeValue) { $this->attributeValue = $attributeValue; return $this; } - - /** - * @return AttributeInterface|null - */ - public function getAttribute(): ?AttributeInterface + + public function getAttribute(): AttributeInterface { return $this->getAttributeValue()->getAttribute(); } diff --git a/lib/RoadizCoreBundle/src/Node/NodeFactory.php b/lib/RoadizCoreBundle/src/Node/NodeFactory.php index 49aeff5c..4776bbb0 100644 --- a/lib/RoadizCoreBundle/src/Node/NodeFactory.php +++ b/lib/RoadizCoreBundle/src/Node/NodeFactory.php @@ -17,19 +17,10 @@ final class NodeFactory { - private ManagerRegistry $managerRegistry; - private NodeNamePolicyInterface $nodeNamePolicy; - - /** - * @param ManagerRegistry $managerRegistry - * @param NodeNamePolicyInterface $nodeNamePolicy - */ public function __construct( - ManagerRegistry $managerRegistry, - NodeNamePolicyInterface $nodeNamePolicy + private readonly ManagerRegistry $managerRegistry, + private readonly NodeNamePolicyInterface $nodeNamePolicy ) { - $this->nodeNamePolicy = $nodeNamePolicy; - $this->managerRegistry = $managerRegistry; } public function create( @@ -100,10 +91,12 @@ public function createWithUrlAlias( ?Node $parent = null ): Node { $node = $this->create($title, $type, $translation, $node, $parent); + $nodeSource = $node->getNodeSources()->first(); /** @var UrlAliasRepository $repository */ $repository = $this->managerRegistry->getRepository(UrlAlias::class); - if (false === $repository->exists($urlAlias)) { - $alias = new UrlAlias($node->getNodeSources()->first() ?: null); + if (false !== $nodeSource && false === $repository->exists($urlAlias)) { + $alias = new UrlAlias(); + $alias->setNodeSource($nodeSource); $alias->setAlias($urlAlias); $this->managerRegistry->getManagerForClass(UrlAlias::class)->persist($alias); } diff --git a/lib/RoadizCoreBundle/src/Node/NodeTranstyper.php b/lib/RoadizCoreBundle/src/Node/NodeTranstyper.php index f5a150e1..29060aa1 100644 --- a/lib/RoadizCoreBundle/src/Node/NodeTranstyper.php +++ b/lib/RoadizCoreBundle/src/Node/NodeTranstyper.php @@ -214,7 +214,8 @@ protected function doTranstypeSingleSource( */ /** @var UrlAlias $urlAlias */ foreach ($existingSource->getUrlAliases() as $urlAlias) { - $newUrlAlias = new UrlAlias($source); + $newUrlAlias = new UrlAlias(); + $newUrlAlias->setNodeSource($source); $this->getManager()->persist($newUrlAlias); $newUrlAlias->setAlias($urlAlias->getAlias()); $source->addUrlAlias($newUrlAlias); diff --git a/lib/RoadizCoreBundle/src/Repository/DocumentRepository.php b/lib/RoadizCoreBundle/src/Repository/DocumentRepository.php index a135e1e6..e92ba864 100644 --- a/lib/RoadizCoreBundle/src/Repository/DocumentRepository.php +++ b/lib/RoadizCoreBundle/src/Repository/DocumentRepository.php @@ -550,6 +550,31 @@ public function findByNodeSourceAndField( return $qb->getQuery()->getResult(); } + /** + * @param NodesSources $nodeSource + * @param int $fieldId + * @return array + */ + public function findByNodeSourceAndFieldId( + NodesSources $nodeSource, + int $fieldId + ): array { + $qb = $this->createQueryBuilder('d'); + $qb->addSelect('dt') + ->leftJoin('d.documentTranslations', 'dt', 'WITH', 'dt.translation = :translation') + ->innerJoin('d.nodesSourcesByFields', 'nsf', 'WITH', 'nsf.nodeSource = :nodeSource') + ->andWhere($qb->expr()->eq('nsf.field', ':field')) + ->andWhere($qb->expr()->eq('d.raw', ':raw')) + ->addOrderBy('nsf.position', 'ASC') + ->setParameter('field', $fieldId) + ->setParameter('nodeSource', $nodeSource) + ->setParameter('translation', $nodeSource->getTranslation()) + ->setParameter('raw', false) + ->setCacheable(true); + + return $qb->getQuery()->getResult(); + } + /** * Find documents used as Settings. * diff --git a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php index 70a7ae00..f86bb8e1 100644 --- a/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php +++ b/lib/RoadizCoreBundle/src/SearchEngine/Subscriber/DefaultNodesSourcesIndexingSubscriber.php @@ -235,9 +235,6 @@ protected function canIndexTitleInCollection(NodesSources $source): bool return ((bool) $source->getShowTitle()); } - if (null !== $source->getNode() && $source->getNode()->getNodeType()) { - return $source->getNode()->getNodeType()->isSearchable(); - } - return true; + return $source->getNode()->getNodeType()->isSearchable(); } } diff --git a/lib/RoadizCoreBundle/src/Serializer/ObjectConstructor/NodeTypeObjectConstructor.php b/lib/RoadizCoreBundle/src/Serializer/ObjectConstructor/NodeTypeObjectConstructor.php index a3a53084..b2e9d5cf 100644 --- a/lib/RoadizCoreBundle/src/Serializer/ObjectConstructor/NodeTypeObjectConstructor.php +++ b/lib/RoadizCoreBundle/src/Serializer/ObjectConstructor/NodeTypeObjectConstructor.php @@ -45,7 +45,6 @@ protected function removeExtraFields(NodeType $nodeType, array $data): NodeType foreach ($nodeType->getFields() as $field) { if (!\in_array($field->getName(), $fieldsName)) { $nodeType->getFields()->removeElement($field); - $field->setNodeType(null); } } diff --git a/lib/Rozier/src/AjaxControllers/AjaxAttributeValuesController.php b/lib/Rozier/src/AjaxControllers/AjaxAttributeValuesController.php index a10af158..07af53f6 100644 --- a/lib/Rozier/src/AjaxControllers/AjaxAttributeValuesController.php +++ b/lib/Rozier/src/AjaxControllers/AjaxAttributeValuesController.php @@ -64,17 +64,17 @@ public function editAction(Request $request, int $attributeValueId): Response } /** - * @param array $parameters + * @param array $parameters * @param AttributeValue $attributeValue * * @return array */ - protected function updatePosition($parameters, AttributeValue $attributeValue): array + protected function updatePosition(array $parameters, AttributeValue $attributeValue): array { $attributable = $attributeValue->getAttributable(); $details = [ '%name%' => $attributeValue->getAttribute()->getLabelOrCode(), - '%nodeName%' => $attributable instanceof Node ? $attributable->getNodeName() : '', + '%nodeName%' => $attributable->getNodeName(), ]; /* * First, we set the new parent diff --git a/lib/Rozier/src/Forms/NodeType.php b/lib/Rozier/src/Forms/NodeType.php index c85a0b4f..9cf491af 100644 --- a/lib/Rozier/src/Forms/NodeType.php +++ b/lib/Rozier/src/Forms/NodeType.php @@ -31,7 +31,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void /** @var Node|null $node */ $node = $builder->getData(); - $isReachable = null !== $node && $node->getNodeType()?->isReachable(); + $isReachable = null !== $node && $node->getNodeType()->isReachable(); if ($isReachable) { $builder->add('home', CheckboxType::class, [ 'label' => 'node.isHome', diff --git a/lib/Rozier/src/Models/NodeModel.php b/lib/Rozier/src/Models/NodeModel.php index f98f4bb2..1ff17d4d 100644 --- a/lib/Rozier/src/Models/NodeModel.php +++ b/lib/Rozier/src/Models/NodeModel.php @@ -37,7 +37,7 @@ public function toArray(): array 'nodeName' => $this->node->getNodeName(), 'isPublished' => $this->node->isPublished(), 'nodeType' => [ - 'color' => $this->node->getNodeType()?->getColor() ?? '#000000', + 'color' => $this->node->getNodeType()->getColor() ?? '#000000', ] ]; if ($this->security->isGranted(NodeVoter::EDIT_SETTING, $this->node)) { @@ -60,7 +60,7 @@ public function toArray(): array 'nodeName' => $this->node->getNodeName(), 'isPublished' => $this->node->isPublished(), 'nodeType' => [ - 'color' => $this->node->getNodeType()?->getColor() ?? '#000000', + 'color' => $this->node->getNodeType()->getColor() ?? '#000000', ] ]; diff --git a/lib/Rozier/src/Models/NodeSourceModel.php b/lib/Rozier/src/Models/NodeSourceModel.php index b8531110..c8208a4c 100644 --- a/lib/Rozier/src/Models/NodeSourceModel.php +++ b/lib/Rozier/src/Models/NodeSourceModel.php @@ -40,7 +40,7 @@ public function toArray(): array 'thumbnail' => $thumbnail ? $thumbnail->getDocument() : null, 'isPublished' => $node->isPublished(), 'nodeType' => [ - 'color' => $node->getNodeType()?->getColor() ?? '#000000', + 'color' => $node->getNodeType()->getColor() ?? '#000000', ] ];