Skip to content

Commit

Permalink
Partial DBAL 4.x support
Browse files Browse the repository at this point in the history
  • Loading branch information
mbabker committed Aug 25, 2024
1 parent ee91783 commit a681cbd
Show file tree
Hide file tree
Showing 23 changed files with 184 additions and 68 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"require-dev": {
"doctrine/annotations": "^1.13 || ^2.0",
"doctrine/cache": "^1.11 || ^2.0",
"doctrine/dbal": "^3.2",
"doctrine/dbal": "^3.7 || ^4.0",
"doctrine/doctrine-bundle": "^2.3",
"doctrine/mongodb-odm": "^2.3",
"doctrine/orm": "^2.14.0 || ^3.0",
Expand All @@ -73,7 +73,7 @@
},
"conflict": {
"doctrine/annotations": "<1.13 || >=3.0",
"doctrine/dbal": "<3.2 || >=4.0",
"doctrine/dbal": "<3.7 || >=5.0",
"doctrine/mongodb-odm": "<2.3 || >=3.0",
"doctrine/orm": "<2.14.0 || 2.16.0 || 2.16.1 || >=4.0"
},
Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ parameters:
count: 3
path: src/AbstractTrackingListener.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getConnection\\(\\)\\.$#"
count: 1
path: src/AbstractTrackingListener.php

-
message: "#^Call to an undefined method Doctrine\\\\Persistence\\\\ObjectManager\\:\\:getUnitOfWork\\(\\)\\.$#"
count: 3
Expand Down
6 changes: 4 additions & 2 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ parameters:
- '#^Result of static method Gedmo\\Uploadable\\Mapping\\Validator::validateConfiguration\(\) \(void\) is used\.$#'
- '#^Result of method Gedmo\\Mapping\\Driver::readExtendedMetadata\(\) \(void\) is used\.$#'
excludePaths:
# Deprecated and unused class, interface does not exist as of 4.0
- src/Tool/Logging/DBAL/QueryAnalyzer.php
# Generates non-ignorable errors like " Parameter #1 $method (string) of method Gedmo\Tree\Entity\Repository\NestedTreeRepository::__call() is not contravariant with parameter #1 $method (mixed) of method Doctrine\ORM\EntityRepository::__call()."
- src/Tool/ORM/Repository/EntityRepositoryCompat.php
# Compat file for ORM 2, causes analysis errors with ORM 3
- src/Tool/ORM/Walker/orm-2.php
# Uses a tracking policy that was removed in ORM 3, PHPStan crashes on this file
- tests/Gedmo/Sortable/Fixture/NotifyNode.php
# Generates non-ignorable errors regarding covariance due to the internal compat layer
- tests/Gedmo/Translatable/Fixture/Type/Custom.php
11 changes: 8 additions & 3 deletions src/AbstractTrackingListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Doctrine\DBAL\Types\Type;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Types\Type as TypeODM;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\UnitOfWork;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
Expand Down Expand Up @@ -267,9 +268,13 @@ private function getPhpValues($values, ?string $type, ObjectManager $om): ?array
} else {
$values[$i] = $value;
}
} elseif (Type::hasType($type)) {
$values[$i] = Type::getType($type)
->convertToPHPValue($value, $om->getConnection()->getDatabasePlatform());
} elseif ($om instanceof EntityManagerInterface) {
if (Type::hasType($type)) {
$values[$i] = $om->getConnection()
->convertToPHPValue($value, $type);
} else {
$values[$i] = $value;
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Loggable/Entity/MappedSuperclass/AbstractLogEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ abstract class AbstractLogEntry implements LogEntryInterface
* @var array<string, mixed>|null
*
* @ORM\Column(type="array", nullable=true)
*
* @note The attribute uses the "array" name directly instead of the constant since it was removed in DBAL 4.0.
*/
#[ORM\Column(type: Types::ARRAY, nullable: true)]
#[ORM\Column(type: 'array', nullable: true)]
protected $data;

/**
Expand Down
8 changes: 4 additions & 4 deletions src/SoftDeleteable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Gedmo\SoftDeleteable\Mapping\Event\Adapter;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\FieldMapping;
Expand Down Expand Up @@ -42,10 +41,11 @@ public function setClock(ClockInterface $clock): void
public function getDateValue($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
$converter = Type::getType($mapping['type'] ?? Types::DATETIME_MUTABLE);
$platform = $this->getObjectManager()->getConnection()->getDriver()->getDatabasePlatform();

return $converter->convertToPHPValue($this->getRawDateValue($mapping), $platform);
return $this->getObjectManager()->getConnection()->convertToPHPValue(
$this->getRawDateValue($mapping),
$mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? Types::DATETIME_MUTABLE)
);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Timestampable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ public function setClock(ClockInterface $clock): void
public function getDateValue($meta, $field)
{
$mapping = $meta->getFieldMapping($field);
$converter = Type::getType($mapping['type'] ?? Types::DATETIME_MUTABLE);
$platform = $this->getObjectManager()->getConnection()->getDriver()->getDatabasePlatform();

return $converter->convertToPHPValue($this->getRawDateValue($mapping), $platform);
return $this->getObjectManager()->getConnection()->convertToPHPValue(
$this->getRawDateValue($mapping),
$mapping instanceof FieldMapping ? $mapping->type : ($mapping['type'] ?? Types::DATETIME_MUTABLE)
);
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/Translatable/Entity/Repository/TranslationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,7 @@ public function translate($entity, $field, $locale, $value)
$listener->setTranslationInDefaultLocale(spl_object_id($entity), $field, $trans);
$needsPersist = $listener->getPersistDefaultLocaleTranslation();
}
$type = Type::getType($meta->getTypeOfField($field));
$transformed = $type->convertToDatabaseValue($value, $this->getEntityManager()->getConnection()->getDatabasePlatform());
$transformed = $this->getEntityManager()->getConnection()->convertToDatabaseValue($value, $meta->getTypeOfField($field));
$transMeta->getReflectionProperty('content')->setValue($trans, $transformed);
if ($needsPersist) {
if ($this->getEntityManager()->getUnitOfWork()->isInIdentityMap($entity)) {
Expand Down
12 changes: 6 additions & 6 deletions src/Translatable/Mapping/Event/Adapter/ORM.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,26 +211,25 @@ public function getTranslationValue($object, $field, $value = false)
$em = $this->getObjectManager();
$wrapped = AbstractWrapper::wrap($object, $em);
$meta = $wrapped->getMetadata();
$type = Type::getType($meta->getTypeOfField($field));

if (false === $value) {
$value = $wrapped->getPropertyValue($field);
}

return $type->convertToDatabaseValue($value, $em->getConnection()->getDatabasePlatform());
return $em->getConnection()->convertToDatabaseValue($value, $meta->getTypeOfField($field));
}

public function setTranslationValue($object, $field, $value)
{
$em = $this->getObjectManager();
$wrapped = AbstractWrapper::wrap($object, $em);
$meta = $wrapped->getMetadata();
$type = Type::getType($meta->getTypeOfField($field));
$value = $type->convertToPHPValue($value, $em->getConnection()->getDatabasePlatform());
$value = $em->getConnection()->convertToPHPValue($value, $meta->getTypeOfField($field));
$wrapped->setPropertyValue($field, $value);
}

/**
* Transforms foreing key of translation to appropriate PHP value
* Transforms foreign key of translation to appropriate PHP value
* to prevent database level cast
*
* @param mixed $key foreign key value
Expand All @@ -245,7 +244,8 @@ private function foreignKey($key, string $className)
$em = $this->getObjectManager();
$meta = $em->getClassMetadata($className);
$type = Type::getType($meta->getTypeOfField('foreignKey'));
switch ($type->getName()) {

switch (Type::lookupName($type)) {
case Types::BIGINT:
case Types::INTEGER:
case Types::SMALLINT:
Expand Down
3 changes: 2 additions & 1 deletion src/Tree/Strategy/ORM/Closure.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

namespace Gedmo\Tree\Strategy\ORM;

use Doctrine\DBAL\ArrayParameterType;
use Doctrine\DBAL\Connection;
use Doctrine\Deprecations\Deprecation;
use Doctrine\ORM\EntityManagerInterface;
Expand Down Expand Up @@ -524,7 +525,7 @@ protected function setLevelFieldOnPendingNodes(ObjectManager $em)
}

// Avoid type conversion performance penalty
$type = 'integer' === $mapping['type'] ? Connection::PARAM_INT_ARRAY : Connection::PARAM_STR_ARRAY;
$type = 'integer' === $mapping['type'] ? ArrayParameterType::INTEGER : ArrayParameterType::STRING;

// We calculate levels for all nodes
$sql = 'SELECT c.descendant, MAX(c.depth) + 1 AS levelNum ';
Expand Down
6 changes: 2 additions & 4 deletions src/Uploadable/UploadableListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
namespace Gedmo\Uploadable;

use Doctrine\Common\EventArgs;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\Persistence\Event\LoadClassMetadataEventArgs;
use Doctrine\Persistence\Event\ManagerEventArgs;
Expand Down Expand Up @@ -338,10 +337,9 @@ public function processFile(AdapterInterface $ea, $object, $action)
}

if ($config['fileSizeField']) {
$typeOfSizeField = Type::getType($meta->getTypeOfField($config['fileSizeField']));
$value = $typeOfSizeField->convertToPHPValue(
$value = $om->getConnection()->convertToPHPValue(
$info['fileSize'],
$om->getConnection()->getDatabasePlatform()
$meta->getTypeOfField($config['fileSizeField'])
);
$this->updateField($object, $uow, $ea, $meta, $config['fileSizeField'], $value);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Gedmo/Loggable/LoggableEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Gedmo\Tests\Loggable;

use Doctrine\DBAL\Types\ArrayType;
use Gedmo\Loggable\Entity\LogEntry;
use Gedmo\Loggable\Entity\Repository\LogEntryRepository;
use Gedmo\Tests\Loggable\Fixture\Entity\Address;
Expand All @@ -37,6 +38,13 @@ abstract class LoggableEntityTest extends BaseTestCaseORM
private const RELATED_ARTICLE = RelatedArticle::class;
private const COMMENT_LOG = Fixture\Entity\Log\Comment::class;

public static function setUpBeforeClass(): void
{
if (!class_exists(ArrayType::class)) {
static::markTestSkipped('The loggable extension is not compatible with doctrine/dbal:>=4.0');
}
}

public function testShouldHandleClonedEntity(): void
{
$art0 = new Article();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<field name="mimeType" column="mime" type="string">
<gedmo:uploadable-file-mime-type/>
</field>
<field name="size" column="size" type="decimal">
<field name="size" column="size" type="decimal" precision="10" scale="2">
<gedmo:uploadable-file-size/>
</field>
<field name="path" column="path" type="string">
Expand Down
4 changes: 2 additions & 2 deletions tests/Gedmo/Mapping/Fixture/Uploadable.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class Uploadable
/**
* @var float
*
* @ORM\Column(name="size", type="decimal")
* @ORM\Column(name="size", type="decimal", precision=10, scale=2)
*
* @Gedmo\UploadableFileSize
*/
#[ORM\Column(name: 'size', type: Types::DECIMAL)]
#[ORM\Column(name: 'size', type: Types::DECIMAL, precision: 10, scale: 2)]
#[Gedmo\UploadableFileSize]
private $size;

Expand Down
Loading

0 comments on commit a681cbd

Please sign in to comment.