Skip to content

Commit ffe12d8

Browse files
pbretechefranmomuphansys
authored
[Sluggable] Add datetime_immutable as valid type (#2438)
* Add datetime_immutable as valid type Slug fields already allow datetime and datetimez, all variations that instanciate \DateTime and DateTimeImmutable objects should work either. * Add tests for datetime_immutable fields * Remove duplicated tests * Update CHANGELOG.md Co-authored-by: Fran Moreno <[email protected]> Co-authored-by: Javier Spagnoletti <[email protected]>
1 parent 0fa18ea commit ffe12d8

File tree

9 files changed

+704
-1
lines changed

9 files changed

+704
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ a release.
1818
---
1919

2020
## [Unreleased]
21-
2221
#### Added
22+
- Sluggable: Add support for DateTimeImmutable fields
2323
- Tree: [NestedSet] `childrenQueryBuilder()` to allow specifying sort order separately for each field
2424
- Tree: [NestedSet] Added option to reorder only direct children in `reorder()` method
2525

src/Sluggable/Mapping/Driver/Annotation.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,12 @@ class Annotation extends AbstractAnnotationDriver
5252
'text',
5353
'integer',
5454
'int',
55+
'date',
56+
'date_immutable',
5557
'datetime',
58+
'datetime_immutable',
5659
'datetimetz',
60+
'datetimetz_immutable',
5761
'citext',
5862
];
5963

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 <[email protected]> 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\Tests\Sluggable\Fixture\DateTimeTypes;
13+
14+
use Doctrine\DBAL\Types\Types;
15+
use Doctrine\ORM\Mapping as ORM;
16+
use Gedmo\Mapping\Annotation as Gedmo;
17+
use Gedmo\Sluggable\Sluggable;
18+
19+
/**
20+
* @ORM\Entity
21+
*/
22+
#[ORM\Entity]
23+
class ArticleDate implements Sluggable
24+
{
25+
/**
26+
* @var int|null
27+
*
28+
* @ORM\Id
29+
* @ORM\GeneratedValue
30+
* @ORM\Column(type="integer")
31+
*/
32+
#[ORM\Id]
33+
#[ORM\GeneratedValue]
34+
#[ORM\Column(type: Types::INTEGER)]
35+
private $id;
36+
37+
/**
38+
* @var string|null
39+
*
40+
* @ORM\Column(name="title", type="string", length=64)
41+
*/
42+
#[ORM\Column(name: 'title', type: Types::STRING, length: 64)]
43+
private $title;
44+
45+
/**
46+
* @var \DateTime|null
47+
*
48+
* @ORM\Column(name="created_at", type="date")
49+
*/
50+
#[ORM\Column(name: 'created_at', type: Types::DATE_MUTABLE)]
51+
private $createdAt;
52+
53+
/**
54+
* @var string|null
55+
*
56+
* @Gedmo\Slug(separator="-", updatable=true, fields={"title", "createdAt"}, dateFormat="Y-m-d")
57+
* @ORM\Column(name="slug", type="string", length=64, unique=true)
58+
*/
59+
#[Gedmo\Slug(separator: '-', updatable: true, fields: ['title', 'createdAt'], dateFormat: 'Y-m-d')]
60+
#[ORM\Column(name: 'slug', type: Types::STRING, length: 64, unique: true)]
61+
private $slug;
62+
63+
public function getId(): ?int
64+
{
65+
return $this->id;
66+
}
67+
68+
public function setTitle(?string $title): void
69+
{
70+
$this->title = $title;
71+
}
72+
73+
public function getTitle(): ?string
74+
{
75+
return $this->title;
76+
}
77+
78+
public function setCreatedAt(?\DateTime $createdAt): void
79+
{
80+
$this->createdAt = $createdAt;
81+
}
82+
83+
public function getCreatedAt(): ?\DateTime
84+
{
85+
return $this->createdAt;
86+
}
87+
88+
public function setSlug(?string $slug): void
89+
{
90+
$this->slug = $slug;
91+
}
92+
93+
public function getSlug(): ?string
94+
{
95+
return $this->slug;
96+
}
97+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 <[email protected]> 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\Tests\Sluggable\Fixture\DateTimeTypes;
13+
14+
use Doctrine\DBAL\Types\Types;
15+
use Doctrine\ORM\Mapping as ORM;
16+
use Gedmo\Mapping\Annotation as Gedmo;
17+
use Gedmo\Sluggable\Sluggable;
18+
19+
/**
20+
* @ORM\Entity
21+
*/
22+
#[ORM\Entity]
23+
class ArticleDateImmutable implements Sluggable
24+
{
25+
/**
26+
* @var int|null
27+
*
28+
* @ORM\Id
29+
* @ORM\GeneratedValue
30+
* @ORM\Column(type="integer")
31+
*/
32+
#[ORM\Id]
33+
#[ORM\GeneratedValue]
34+
#[ORM\Column(type: Types::INTEGER)]
35+
private $id;
36+
37+
/**
38+
* @var string|null
39+
*
40+
* @ORM\Column(name="title", type="string", length=64)
41+
*/
42+
#[ORM\Column(name: 'title', type: Types::STRING, length: 64)]
43+
private $title;
44+
45+
/**
46+
* @var \DateTimeImmutable|null
47+
*
48+
* @ORM\Column(name="created_at", type="date_immutable")
49+
*/
50+
#[ORM\Column(name: 'created_at', type: Types::DATE_IMMUTABLE)]
51+
private $createdAt;
52+
53+
/**
54+
* @var string|null
55+
*
56+
* @Gedmo\Slug(separator="-", updatable=true, fields={"title", "createdAt"}, dateFormat="Y-m-d")
57+
* @ORM\Column(name="slug", type="string", length=64, unique=true)
58+
*/
59+
#[Gedmo\Slug(separator: '-', updatable: true, fields: ['title', 'createdAt'], dateFormat: 'Y-m-d')]
60+
#[ORM\Column(name: 'slug', type: Types::STRING, length: 64, unique: true)]
61+
private $slug;
62+
63+
public function getId(): ?int
64+
{
65+
return $this->id;
66+
}
67+
68+
public function setTitle(?string $title): void
69+
{
70+
$this->title = $title;
71+
}
72+
73+
public function getTitle(): ?string
74+
{
75+
return $this->title;
76+
}
77+
78+
public function setCreatedAt(?\DateTimeImmutable $createdAt): void
79+
{
80+
$this->createdAt = $createdAt;
81+
}
82+
83+
public function getCreatedAt(): ?\DateTimeImmutable
84+
{
85+
return $this->createdAt;
86+
}
87+
88+
public function setSlug(?string $slug): void
89+
{
90+
$this->slug = $slug;
91+
}
92+
93+
public function getSlug(): ?string
94+
{
95+
return $this->slug;
96+
}
97+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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 <[email protected]> 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\Tests\Sluggable\Fixture\DateTimeTypes;
13+
14+
use Doctrine\DBAL\Types\Types;
15+
use Doctrine\ORM\Mapping as ORM;
16+
use Gedmo\Mapping\Annotation as Gedmo;
17+
use Gedmo\Sluggable\Sluggable;
18+
19+
/**
20+
* @ORM\Entity
21+
*/
22+
#[ORM\Entity]
23+
class ArticleDateTime implements Sluggable
24+
{
25+
/**
26+
* @var int|null
27+
*
28+
* @ORM\Id
29+
* @ORM\GeneratedValue
30+
* @ORM\Column(type="integer")
31+
*/
32+
#[ORM\Id]
33+
#[ORM\GeneratedValue]
34+
#[ORM\Column(type: Types::INTEGER)]
35+
private $id;
36+
37+
/**
38+
* @var string|null
39+
*
40+
* @ORM\Column(name="title", type="string", length=64)
41+
*/
42+
#[ORM\Column(name: 'title', type: Types::STRING, length: 64)]
43+
private $title;
44+
45+
/**
46+
* @var \DateTime|null
47+
*
48+
* @ORM\Column(name="created_at", type="datetime")
49+
*/
50+
#[ORM\Column(name: 'created_at', type: Types::DATETIME_MUTABLE)]
51+
private $createdAt;
52+
53+
/**
54+
* @var string|null
55+
*
56+
* @Gedmo\Slug(separator="-", updatable=true, fields={"title", "createdAt"}, dateFormat="Y-m-d")
57+
* @ORM\Column(name="slug", type="string", length=64, unique=true)
58+
*/
59+
#[Gedmo\Slug(separator: '-', updatable: true, fields: ['title', 'createdAt'], dateFormat: 'Y-m-d')]
60+
#[ORM\Column(name: 'slug', type: Types::STRING, length: 64, unique: true)]
61+
private $slug;
62+
63+
public function getId(): ?int
64+
{
65+
return $this->id;
66+
}
67+
68+
public function setTitle(?string $title): void
69+
{
70+
$this->title = $title;
71+
}
72+
73+
public function getTitle(): ?string
74+
{
75+
return $this->title;
76+
}
77+
78+
public function setCreatedAt(?\DateTime $createdAt): void
79+
{
80+
$this->createdAt = $createdAt;
81+
}
82+
83+
public function getCreatedAt(): ?\DateTime
84+
{
85+
return $this->createdAt;
86+
}
87+
88+
public function setSlug(?string $slug): void
89+
{
90+
$this->slug = $slug;
91+
}
92+
93+
public function getSlug(): ?string
94+
{
95+
return $this->slug;
96+
}
97+
}

0 commit comments

Comments
 (0)