Skip to content

Commit 6bac093

Browse files
committed
[#2006] allow integer type for blamable fields
doctrine uses `integer` if the column is of type `int` Signed-off-by: Stefan Gehrig <stefan.gehrig.hn@googlemail.com>
1 parent eef8a13 commit 6bac093

3 files changed

Lines changed: 131 additions & 2 deletions

File tree

src/Blameable/Mapping/Driver/Attribute.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Attribute extends AbstractAnnotationDriver
3636
'one',
3737
'string',
3838
'int',
39+
'integer',
3940
'ulid',
4041
'uuid',
4142
'ascii_string',
@@ -65,12 +66,12 @@ public function readExtendedMetadata($meta, array &$config)
6566

6667
if ($meta->hasField($field)) {
6768
if (!$this->isValidField($meta, $field)) {
68-
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' or a one-to-many relation in class - {$meta->getName()}");
69+
throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string', 'integer' or a one-to-many relation in class - {$meta->getName()}");
6970
}
7071
} else {
7172
// association
7273
if (!$meta->isSingleValuedAssociation($field)) {
73-
throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->getName()}");
74+
throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string or integer field - {$meta->getName()}");
7475
}
7576
}
7677

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Doctrine Behavioral Extensions package.
7+
* (c) Gediminas Morkevicius <gediminas.morkevicius@gmail.com> http://www.gediminasm.org
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Gedmo\Blameable;
13+
14+
use Doctrine\Common\EventManager;
15+
use Gedmo\Tests\Blameable\Fixture\Entity\CompanyInteger;
16+
use Gedmo\Tests\Tool\BaseTestCaseORM;
17+
18+
final class BlameableIntegerTest extends BaseTestCaseORM
19+
{
20+
private int $userId;
21+
22+
protected function setUp(): void
23+
{
24+
parent::setUp();
25+
26+
$this->userId = 42;
27+
28+
$listener = new BlameableListener();
29+
$listener->setUserValue($this->userId);
30+
31+
$evm = new EventManager();
32+
$evm->addEventSubscriber($listener);
33+
34+
$this->getDefaultMockSqliteEntityManager($evm);
35+
}
36+
37+
public function testBlameableInteger(): void
38+
{
39+
$company = new CompanyInteger();
40+
$company->setName('My Name');
41+
42+
$this->em->persist($company);
43+
$this->em->flush();
44+
$this->em->clear();
45+
46+
/**
47+
* @var CompanyInteger $foundCompany
48+
*/
49+
$foundCompany = $this->em->getRepository(CompanyInteger::class)->findOneBy(['name' => 'My Name']);
50+
$creator = $foundCompany->getCreator();
51+
52+
static::assertSame($this->userId, $creator);
53+
}
54+
55+
protected function getUsedEntityFixtures(): array
56+
{
57+
return [
58+
CompanyInteger::class,
59+
];
60+
}
61+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Gedmo\Tests\Blameable\Fixture\Entity;
4+
5+
use Doctrine\DBAL\Types\Types;
6+
use Doctrine\ORM\Mapping as ORM;
7+
use Gedmo\Mapping\Annotation as Gedmo;
8+
9+
/**
10+
* @ORM\Entity
11+
*/
12+
#[ORM\Entity]
13+
class CompanyInteger
14+
{
15+
/**
16+
* @var int|null
17+
*
18+
* @ORM\Id
19+
* @ORM\GeneratedValue
20+
* @ORM\Column(type="integer")
21+
*/
22+
#[ORM\Id]
23+
#[ORM\GeneratedValue]
24+
#[ORM\Column(type: Types::INTEGER)]
25+
private $id;
26+
27+
/**
28+
* @ORM\Column(name="name", type="string", length=128)
29+
*/
30+
#[ORM\Column(name: 'name', type: Types::STRING, length: 128)]
31+
private ?string $name = null;
32+
33+
/**
34+
* @var int|null
35+
*
36+
* @Gedmo\Blameable(on="create")
37+
* @ORM\Column(name="creator", type="integer")
38+
*/
39+
#[ORM\Column(name: 'creator', type: Types::INTEGER)]
40+
#[Gedmo\Blameable(on: 'create')]
41+
private $creator;
42+
43+
public function getId(): ?int
44+
{
45+
return $this->id;
46+
}
47+
48+
public function setName(?string $name): void
49+
{
50+
$this->name = $name;
51+
}
52+
53+
public function getName(): ?string
54+
{
55+
return $this->name;
56+
}
57+
58+
public function getCreator(): ?int
59+
{
60+
return $this->creator;
61+
}
62+
63+
public function setCreator(?int $creator): void
64+
{
65+
$this->creator = $creator;
66+
}
67+
}

0 commit comments

Comments
 (0)