Skip to content

Commit ce04b1b

Browse files
committed
fix article entity
1 parent 02c3c4d commit ce04b1b

8 files changed

Lines changed: 60 additions & 84 deletions

File tree

src/App/src/ConfigProvider.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
1111
use Dot\Cache\Adapter\ArrayAdapter;
1212
use Dot\Cache\Adapter\FilesystemAdapter;
13+
use Light\App\Factory\EntityListenerResolverFactory;
1314
use Light\App\Factory\GetIndexViewHandlerFactory;
1415
use Light\App\Handler\GetIndexViewHandler;
16+
use Light\App\Resolver\EntityListenerResolver;
1517
use Mezzio\Application;
1618
use Ramsey\Uuid\Doctrine\UuidType;
1719
use Roave\PsrContainerDoctrine\EntityManagerFactory;
@@ -23,6 +25,7 @@
2325
* @phpstan-type ConfigType array{
2426
* dependencies: DependenciesType,
2527
* doctrine: DoctrineConfigType,
28+
* resultCacheLifetime: int,
2629
* }
2730
* @phpstan-type DoctrineConfigType array{
2831
* cache: array{
@@ -79,6 +82,7 @@ public function __invoke(): array
7982
'dependencies' => $this->getDependencies(),
8083
'doctrine' => $this->getDoctrineConfig(),
8184
'templates' => $this->getTemplates(),
85+
'resultCacheLifetime' => 3600,
8286
];
8387
}
8488

@@ -95,6 +99,7 @@ public function getDependencies(): array
9599
],
96100
'factories' => [
97101
'doctrine.entity_manager.orm_default' => EntityManagerFactory::class,
102+
EntityListenerResolver::class => EntityListenerResolverFactory::class,
98103
GetIndexViewHandler::class => GetIndexViewHandlerFactory::class,
99104
],
100105
'aliases' => [
@@ -144,6 +149,7 @@ private function getDoctrineConfig(): array
144149
],
145150
'configuration' => [
146151
'orm_default' => [
152+
'entity_listener_resolver' => EntityListenerResolver::class,
147153
'result_cache' => 'filesystem',
148154
'metadata_cache' => 'filesystem',
149155
'query_cache' => 'filesystem',
@@ -159,8 +165,6 @@ private function getDoctrineConfig(): array
159165
],
160166
],
161167
'driver' => [
162-
// The default metadata driver aggregates all other drivers into a single one.
163-
// Override `orm_default` only if you know what you're doing.
164168
'orm_default' => [
165169
'class' => MappingDriverChain::class,
166170
],
@@ -175,9 +179,9 @@ private function getDoctrineConfig(): array
175179
'execution_time_column_name' => 'execution_time',
176180
],
177181
'migrations_paths' => [
178-
'Migration' => 'src/App/src/Migration',
182+
'Migration' => 'src/App/src/Migration',
179183
],
180-
'all_or_nothing' => true,
184+
'all_or_nothing' => false,
181185
'check_database_platform' => true,
182186
],
183187
'types' => [
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Light\App\Factory;
6+
7+
use Light\App\Resolver\EntityListenerResolver;
8+
use Psr\Container\ContainerInterface;
9+
10+
class EntityListenerResolverFactory
11+
{
12+
public function __invoke(ContainerInterface $container): EntityListenerResolver
13+
{
14+
return new EntityListenerResolver($container);
15+
}
16+
}

src/App/src/Migration/Version20260602194657.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/App/src/Migration/Version20260609135654.php

Lines changed: 0 additions & 31 deletions
This file was deleted.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Light\App\Resolver;
6+
7+
use Doctrine\ORM\Mapping\DefaultEntityListenerResolver;
8+
use Psr\Container\ContainerExceptionInterface;
9+
use Psr\Container\ContainerInterface;
10+
use Psr\Container\NotFoundExceptionInterface;
11+
12+
class EntityListenerResolver extends DefaultEntityListenerResolver
13+
{
14+
public function __construct(
15+
protected ContainerInterface $container,
16+
) {
17+
}
18+
19+
/**
20+
* @throws ContainerExceptionInterface
21+
* @throws NotFoundExceptionInterface
22+
*/
23+
public function resolve(string $className): object
24+
{
25+
return $this->container->get($className);
26+
}
27+
}

src/Blog/src/DBAL/Types/ArticleStatusEnumType.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99

1010
class ArticleStatusEnumType extends AbstractEnumType
1111
{
12-
public const NAME = 'status';
13-
12+
public const NAME = 'article_status_enum';
1413
public function getEnumClass(): string
1514
{
1615
return ArticleStatusEnum::class;

src/Blog/src/Entity/Article.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ class Article extends AbstractEntity
2121
#[ORM\Column(name: 'slug', type: 'string', length: 500, unique: true)]
2222
private string $slug;
2323

24-
#[ORM\Column(name: 'post_date', type: 'datetime_immutable', nullable: true)]
25-
private string $postDate;
24+
#[ORM\Column(name: 'post_date', type: 'datetime_immutable')]
25+
private DateTimeImmutable $postDate;
2626

2727
#[ORM\Column(
2828
name: 'status',
2929
type: 'article_status_enum',
3030
enumType: ArticleStatusEnum::class,
31-
options: ['default' => ArticleStatusEnum::Published]
31+
options: ['default' => ArticleStatusEnum::Draft]
3232
)]
33-
private ArticleStatusEnum $status;
33+
private ArticleStatusEnum $status = ArticleStatusEnum::Draft;
3434

3535
#[ORM\ManyToOne(targetEntity: Category::class)]
3636
#[ORM\JoinColumn(name: 'category_id', referencedColumnName: 'id', nullable: false)]
@@ -60,12 +60,12 @@ public function setSlug(string $slug): void
6060
$this->slug = $slug;
6161
}
6262

63-
public function getPostDate(): string
63+
public function getPostDate(): DateTimeImmutable
6464
{
6565
return $this->postDate;
6666
}
6767

68-
public function setPostDate(string $postDate): void
68+
public function setPostDate(DateTimeImmutable $postDate): void
6969
{
7070
$this->postDate = $postDate;
7171
}
@@ -118,7 +118,7 @@ public function getArrayCopy(): array
118118
'title' => $this->title,
119119
'slug' => $this->slug,
120120
'status' => $this->status->value,
121-
'postDate' => $this->postDate,
121+
'postDate' => $this->postDate->format('Y-m-d H:i:s'),
122122
'category' => $this->category->getArrayCopy(),
123123
'author' => $this->author->getArrayCopy(),
124124
];

src/Blog/src/Repository/CategoryRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class CategoryRepository extends AbstractRepository
1616
public function getCategories(): array
1717
{
1818
$qb = $this->getQueryBuilder()
19-
->select('categories.name, categories.slug')
19+
->select('categories.name, categories.slug, categories.created')
2020
->from(Category::class, 'categories');
2121

2222
return $qb->getQuery()->getResult();

0 commit comments

Comments
 (0)