Skip to content
Open
Show file tree
Hide file tree
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
10 changes: 9 additions & 1 deletion src/Persisters/Entity/AbstractEntityInheritancePersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,21 @@ protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r'
{
$tableAlias = $alias === 'r' ? '' : $alias;
$fieldMapping = $class->fieldMappings[$field];
$columnAlias = $this->getSQLColumnAlias($fieldMapping['columnName']);
$sql = sprintf(
'%s.%s',
$this->getSQLTableAlias($class->name, $tableAlias),
$this->quoteStrategy->getColumnName($field, $class, $this->platform)
);

$columnAlias = null;
if ($this->currentPersisterContext->rsm->hasColumnAliasByField($alias, $field)) {
$columnAlias = $this->currentPersisterContext->rsm->getColumnAliasByField($alias, $field);
}

if ($columnAlias === null) {
$columnAlias = $this->getSQLColumnAlias($fieldMapping['columnName']);
}

$this->currentPersisterContext->rsm->addFieldResult($alias, $columnAlias, $field, $class->name);

if (isset($fieldMapping['requireSQLConversion'])) {
Expand Down
4 changes: 4 additions & 0 deletions src/Query/ResultSetMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,10 @@ public function addFieldResult($alias, $columnName, $fieldName, $declaringClass

public function hasColumnAliasByField(string $alias, string $fieldName): bool
{
if (! isset($this->aliasMap[$alias])) {
return false;
}

$declaringClass = $this->aliasMap[$alias];

return isset($this->columnAliasMappings[$declaringClass][$alias][$fieldName]);
Expand Down
11 changes: 2 additions & 9 deletions tests/Tests/DbalTypes/Rot13Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ class Rot13Type extends Type
/**
* {@inheritDoc}
*
* @param string|null $value
* @param AbstractPlatform $platform
* @param string|null $value
*
* @return string|null
*/
Expand All @@ -35,8 +34,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
/**
* {@inheritDoc}
*
* @param string|null $value
* @param AbstractPlatform $platform
* @param string|null $value
*
* @return string|null
*/
Expand All @@ -52,9 +50,6 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
/**
* {@inheritDoc}
*
* @param array $fieldDeclaration
* @param AbstractPlatform $platform
*
* @return string
*/
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
Expand All @@ -69,8 +64,6 @@ public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $pla
/**
* {@inheritDoc}
*
* @param AbstractPlatform $platform
*
* @return int|null
*/
public function getDefaultLength(AbstractPlatform $platform)
Expand Down
123 changes: 123 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH12225/AbstractDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH12225;

use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Index;
use Doctrine\ORM\Mapping\InheritanceType;
use Doctrine\ORM\Mapping\ManyToOne;
use Doctrine\ORM\Mapping\OneToMany;
use Doctrine\ORM\Mapping\Table;

/**
* @Entity
* @Table(name="gh_12225_directory", indexes={@Index(columns={"dir_key"})})
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="type", type="string")
* @DiscriminatorMap({"main" = ConcreteDirectory::class})
*/
#[Entity]
#[Table(name: 'gh_12225_directory')]
#[Index(columns: ['dir_key'])]
#[InheritanceType('SINGLE_TABLE')]
#[DiscriminatorColumn(name: 'type', type: 'string')]
#[DiscriminatorMap(['main' => ConcreteDirectory::class])]
class AbstractDirectory
{
/**
* @var int
* @Id
* @GeneratedValue
* @Column(name="id", type="integer")
*/
#[Id]
#[GeneratedValue]
#[Column(name: 'id', type: 'integer')]
private $id;

/**
* @var string
* @Column(name="dir_key", type="string")
*/
#[Column(name: 'dir_key', type: 'string')]
private $dirKey;

/**
* @var DateTimeImmutable|null
* @Column(name="deleted_at", type="datetime_immutable", nullable=true)
*/
#[Column(name: 'deleted_at', type: 'datetime_immutable', nullable: true)]
private $deletedAt = null;

/**
* @var AbstractDirectory|null
* @ManyToOne(targetEntity=AbstractDirectory::class, fetch="LAZY", inversedBy="directories")
*/
#[ManyToOne(targetEntity: self::class, fetch: 'LAZY', inversedBy: 'directories')]
private $parent = null;

/**
* @var Collection<string, self>
* @OneToMany(mappedBy="parent", targetEntity=AbstractDirectory::class, fetch="EXTRA_LAZY", indexBy="dirKey")
*/
#[OneToMany(mappedBy: 'parent', targetEntity: self::class, fetch: 'EXTRA_LAZY', indexBy: 'dirKey')]
private $children;

public function __construct(string $dirKey)
{
$this->dirKey = $dirKey;
$this->children = new ArrayCollection();
}

public function getId(): int
{
return $this->id;
}

public function getDirKey(): string
{
return $this->dirKey;
}

public function getDeletedAt(): ?DateTimeImmutable
{
return $this->deletedAt;
}

public function setDeletedAt(?DateTimeImmutable $deletedAt): AbstractDirectory
{
$this->deletedAt = $deletedAt;

return $this;
}

public function getParent(): ?AbstractDirectory
{
return $this->parent;
}

public function setParent(?AbstractDirectory $parent): AbstractDirectory
{
$this->parent = $parent;

return $this;
}

/**
* @return Collection<string, self>
*/
public function getChildren(): Collection
{
return $this->children;
}
}
15 changes: 15 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH12225/ConcreteDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH12225;

use Doctrine\ORM\Mapping\Entity;

/**
* @Entity
*/
#[Entity]
class ConcreteDirectory extends AbstractDirectory
{
}
49 changes: 49 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH12225/GH12225Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH12225;

use Doctrine\Tests\OrmFunctionalTestCase;

class GH12225Test extends OrmFunctionalTestCase
{
protected function setUp(): void
{
parent::setUp();

$this->setUpEntitySchema([
AbstractDirectory::class,
ConcreteDirectory::class,
]);
}

public function testHydrateWithIndexByFilterAndInheritanceMapping(): void
{
// Enable the filter
$this->_em->getConfiguration()->addFilter('my_filter', MyFilter::class);
$this->_em->getFilters()->enable('my_filter');

// Load entities into database
$parent = new ConcreteDirectory('parent');
$child = (new ConcreteDirectory('child'))->setParent($parent);
$this->_em->persist($parent);
$this->_em->persist($child);
$this->_em->flush();
$this->_em->clear();

$repository = $this->_em->getRepository(AbstractDirectory::class);

// Fetch entities from database while changing filters
$this->_em->getFilters()->suspend('my_filter');
$directories = $repository->findBy(['parent' => null]);
$this->_em->getFilters()->restore('my_filter');

// Ensure we got the parent directory
self::assertCount(1, $directories);
self::assertEquals('parent', $directories[0]->getDirKey());

// Try to hydrate all children of the parent directory (toArray is important here to initialize the collection)
self::assertCount(1, $directories[0]->getChildren()->toArray());
}
}
16 changes: 16 additions & 0 deletions tests/Tests/ORM/Functional/Ticket/GH12225/MyFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\ORM\Functional\Ticket\GH12225;

use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query\Filter\SQLFilter;

class MyFilter extends SQLFilter
{
public function addFilterConstraint(ClassMetadata $targetEntity, $targetTableAlias): string
{
return $targetTableAlias . '.deleted_at IS NULL';
}
}