fernet/doctrine

此包已被 废弃 并不再维护。未建议替代包。

Fernet 框架的 Doctrine 插件

v0.3.3 2021-04-26 04:05 UTC

This package is auto-updated.

Last update: 2024-03-26 14:32:04 UTC


README

Fernet 框架的 Doctrine 插件

配置

将配置添加到您的 .env 文件中

DB_DRIVER="pdo_sql"
DB_USER="root"
DB_PASSWORD="your-password"
DB_NAME="my-database"

如果您手动设置插件,别忘了将 "fernet/doctrine" 添加到 plugins.json 文件中。

使用方法

src\Entity 文件夹中添加您的实体类,使用 App\Entity 命名空间。

<?php
namespace App\Entity;

/** @Entity */
class Post
{
    /** @Id @Column(type="integer") @GeneratedValue */
    public int $id;
    /** @Column(type="string") */
    public string $title;
    /** @Column(type="text") */
    public string $content;
}

然后运行 Doctrine 命令 vendor/bin/doctrine orm:validate-schema。要创建数据库,可以运行 orm:schema-tool:create,要更新数据库模式,可以运行 orm:schema-tool:update

在您的组件中使用 Doctrine\ORM\EntityManager 对象,例如

<?php
namespace App\Component;

use App\Entity\Post;
use Doctrine\ORM\EntityManager;

class ShowPost
{
    public $id;

    private EntityManager $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function __toString(): string
    {
        $post = $this->entityManager->find(Post::class, $this->id);
        return "<h1>{$post->title}</h1><section>{$post->content}</section>";
    }
}

更多信息请访问 Doctrine 文档

仓库

要使用 Fernet 自动装配,可以将您的 Repository 类扩展为 DoctrineFernet\EntityRepository 并实现返回实体类名称的 getEntity 方法。

<?php
namespace App\Repository;

use App\Entity\Post;
use DoctrineFernet\EntityRepository;

class PostRepository extends EntityRepository
{
    public function getEntity(): string
    {
        return Post::class;
    }
}