Skip to content

Commit

Permalink
feat(Documents): Made NodesSourcesDocuments.php relation columns not …
Browse files Browse the repository at this point in the history
…nullable for index performance
  • Loading branch information
ambroisemaupate committed Mar 5, 2024
1 parent 153d53c commit 167b2dd
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
31 changes: 31 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20240305125653.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240305125653 extends AbstractMigration
{
public function getDescription(): string
{
return 'Set nodes_sources_documents relation columns to not-nullable';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE nodes_sources_documents CHANGE ns_id ns_id INT NOT NULL, CHANGE document_id document_id INT NOT NULL, CHANGE node_type_field_id node_type_field_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_documents CHANGE ns_id ns_id INT DEFAULT NULL, CHANGE document_id document_id INT DEFAULT NULL, CHANGE node_type_field_id node_type_field_id INT DEFAULT NULL');
}
}
4 changes: 0 additions & 4 deletions lib/RoadizCoreBundle/src/Entity/NodesSources.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,6 @@ function (NodesSourcesDocuments $element) use ($nodeTypeField) {
/** @var NodesSourcesDocuments $toRemove */
foreach ($toRemoveCollection as $toRemove) {
$this->getDocumentsByFields()->removeElement($toRemove);
$toRemove->setNodeSource(null);
}

return $this;
Expand Down Expand Up @@ -322,9 +321,6 @@ public function getOneDisplayableDocument(): ?DocumentInterface
*/
public function setDocumentsByFields(Collection $documentsByFields): NodesSources
{
foreach ($this->documentsByFields as $documentsByField) {
$documentsByField->setNodeSource(null);
}
$this->documentsByFields->clear();
foreach ($documentsByFields as $documentsByField) {
if (!$this->hasNodesSourcesDocuments($documentsByField)) {
Expand Down
51 changes: 27 additions & 24 deletions lib/RoadizCoreBundle/src/Entity/NodesSourcesDocuments.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use RZ\Roadiz\Contracts\NodeType\NodeTypeFieldInterface;
use RZ\Roadiz\Core\AbstractEntities\AbstractPositioned;
use RZ\Roadiz\CoreBundle\Repository\NodesSourcesDocumentsRepository;
use Symfony\Component\Validator\Constraints as Assert;

/**
* Describes a complex ManyToMany relation
Expand All @@ -23,42 +24,45 @@
class NodesSourcesDocuments extends AbstractPositioned
{
/**
* @var NodesSources|null
* @var NodesSources
*/
#[ORM\ManyToOne(
targetEntity: NodesSources::class,
cascade: ['persist'],
fetch: 'EAGER',
inversedBy: 'documentsByFields'
)]
#[ORM\JoinColumn(name: 'ns_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
protected ?NodesSources $nodeSource;
#[Assert\NotNull]
#[ORM\JoinColumn(name: 'ns_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
protected NodesSources $nodeSource;

/**
* @var Document|null
* @var Document
*/
#[ORM\ManyToOne(
targetEntity: Document::class,
cascade: ['persist'],
fetch: 'EAGER',
inversedBy: 'nodesSourcesByFields'
)]
#[ORM\JoinColumn(name: 'document_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
protected ?Document $document;
#[Assert\NotNull]
#[ORM\JoinColumn(name: 'document_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
protected Document $document;

/**
* @var NodeTypeField|null
* @var NodeTypeField
*/
#[ORM\ManyToOne(targetEntity: NodeTypeField::class)]
#[ORM\JoinColumn(name: 'node_type_field_id', referencedColumnName: 'id', onDelete: 'CASCADE')]
protected ?NodeTypeField $field;
#[Assert\NotNull]
#[ORM\JoinColumn(name: 'node_type_field_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
protected NodeTypeField $field;

/**
* Create a new relation between NodeSource, a Document and a NodeTypeField.
*
* @param NodesSources $nodeSource NodesSources and inherited types
* @param Document $document Document to link
* @param NodeTypeFieldInterface $field NodeTypeField
* @param Document $document Document to link
* @param NodeTypeFieldInterface $field NodeTypeField
*/
public function __construct(NodesSources $nodeSource, Document $document, NodeTypeFieldInterface $field)
{
Expand All @@ -74,28 +78,27 @@ public function __clone()
{
if ($this->id) {
$this->id = null;
$this->nodeSource = null;
}
}

/**
* Gets the value of nodeSource.
*
* @return NodesSources|null
* @return NodesSources
*/
public function getNodeSource(): ?NodesSources
public function getNodeSource(): NodesSources
{
return $this->nodeSource;
}

/**
* Sets the value of nodeSource.
*
* @param NodesSources|null $nodeSource the node source
* @param NodesSources $nodeSource the node source
*
* @return self
*/
public function setNodeSource(?NodesSources $nodeSource): NodesSourcesDocuments
public function setNodeSource(NodesSources $nodeSource): NodesSourcesDocuments
{
$this->nodeSource = $nodeSource;

Expand All @@ -105,21 +108,21 @@ public function setNodeSource(?NodesSources $nodeSource): NodesSourcesDocuments
/**
* Gets the value of document.
*
* @return Document|null
* @return Document
*/
public function getDocument(): ?Document
public function getDocument(): Document
{
return $this->document;
}

/**
* Sets the value of document.
*
* @param Document|null $document the document
* @param Document $document the document
*
* @return self
*/
public function setDocument(?Document $document): NodesSourcesDocuments
public function setDocument(Document $document): NodesSourcesDocuments
{
$this->document = $document;

Expand All @@ -129,21 +132,21 @@ public function setDocument(?Document $document): NodesSourcesDocuments
/**
* Gets the value of field.
*
* @return NodeTypeField|null
* @return NodeTypeField
*/
public function getField(): ?NodeTypeField
public function getField(): NodeTypeField
{
return $this->field;
}

/**
* Sets the value of field.
*
* @param NodeTypeField|null $field the field
* @param NodeTypeField $field the field
*
* @return self
*/
public function setField(?NodeTypeField $field): NodesSourcesDocuments
public function setField(NodeTypeField $field): NodesSourcesDocuments
{
$this->field = $field;

Expand Down

0 comments on commit 167b2dd

Please sign in to comment.