orbitale/doctrine-tools

该软件包已被废弃,不再维护。作者建议使用 orbitale/array-fixture 软件包。

Doctrine ORM 的工具包

v0.7.5 2020-03-08 16:50 UTC

README

Orbitale Doctrine 工具

这个库由多个与 Doctrine ORM 一起使用的工具组成。

文档

安装

只需使用 Composer 安装库

    composer require orbitale/doctrine-tools:~0.1

使用

实体存储库

有 3 种使用 EntityRepositoryHelperTrait 的方式

  1. 在你的自己的存储库中,只需扩展 Orbitale 的存储库
<?php

namespace AppBundle\Repository;

use Orbitale\Component\DoctrineTools\EntityRepositoryHelperTrait;

class PostRepository
{
    use EntityRepositoryHelperTrait;

    // Your custom logic here ...

}
  1. 如果你使用 Symfony,你可以在配置中覆盖默认的实体管理器
# app/config.yml
doctrine:
    orm:
        default_repository_class: Orbitale\Component\DoctrineTools\EntityRepositoryHelperTrait
  1. 如果你“原生”使用 Doctrine,你可以在 Doctrine 配置类中覆盖默认的实体存储库类
$configuration = new Doctrine\ORM\Configuration();
$configuration->setDefaultRepositoryClassName('Orbitale\Component\DoctrineTools\EntityRepositoryHelperTrait');

// Build the EntityManager with its configuration...

这种方式,你可以像以前一样使用你的实体存储库,它只是增加了新的酷炫方法!

只需查看 EntityRepositoryHelperTrait 类,看看它增加了哪些酷炫功能!

Doctrine 固定值

这个类主要用于在需要创建 Doctrine 固定值时,并且当你想要以最简单的方式完成它时。

要使用它,你必须安装 doctrine/data-fixtures(如果你使用 Symfony,还必须安装 doctrine/doctrine-fixtures-bundle)。

以下是一个新的固定值类的简单示例

<?php

namespace AppBundle\DataFixtures\ORM;

use App\Entity\Post;
use Orbitale\Component\DoctrineTools\AbstractFixture;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;

class PostFixtures extends AbstractFixture implements ORMFixtureInterface
{
    public function getEntityClass(): string
    {
        return Post::class;
    }

    public function getObjects(): array
    {
        return [
            ['title' => 'First post', 'description' => 'Lorem ipsum'],
            ['title' => 'Second post', 'description' => 'muspi meroL'],
        ];
    }
}

使用可调用来获取引用

当你有自引用关系时,你可能需要一个可能已经持久化的对象的引用。

为此,首先,你应该将 flushEveryXIterations 选项设置为 1(见下文)以允许在每次迭代时刷新。

然后,你可以将一个 callable 元素作为对象的值,这样你就可以手动与注入的对象(作为第一个参数)以及 AbstractFixture 对象(作为第二个参数)交互。

如果需要执行一些特定请求或通过另一个表进行查询,EntityManagerInterface 也作为第三个参数注入。

示例在这里

<?php

namespace App\DataFixtures\ORM;

use App\Entity\Post;
use Orbitale\Component\DoctrineTools\AbstractFixture;
use Doctrine\Bundle\FixturesBundle\ORMFixtureInterface;
use Doctrine\ORM\EntityManagerInterface;

class PostFixtures extends AbstractFixture implements ORMFixtureInterface
{
    public function getEntityClass(): string
    {
        return Post::class;
    }

    /**
     * With this, we can retrieve a Post reference with this method:
     * $this->getReference('posts-1');
     * where '1' is the post id.
     * Only works with same object if it's flushed on every iteration.
     */
    public function getReferencePrefix(): ?string
    {
        return 'posts-';
    }

    /**
     * Set this to 1 so the first post is always persisted before the next one.
     * This is mandatory as we are referencing the same object. 
     * If we had to use a reference to another object, only "getOrder()" would have to be overriden. 
     */
    public function flushEveryXIterations(): int 
    {
        return 1;
    }

    public function getObjects(): array
    {
        return [
            ['id' => 'c5022243-343b-40c3-8c88-09c1a76faf78', 'title' => 'First post', 'parent' => null],
            [
                'title' => 'Second post',
                'parent' => function(Post $object, AbstractFixture $fixture, EntityManagerInterface $manager) {
                    return $fixture->getReference('posts-c5022243-343b-40c3-8c88-09c1a76faf78');
                },
            ],
        ];
    }
}

这允许在处理自引用关系时实现完美的同步。

可以覆盖的 AbstractFixture 类方法

  • getOrder()(默认 0)用于更改固定值加载的顺序。
  • getReferencePrefix()(默认 null)用于在Fixtures批处理中添加引用,以便以后使用。引用存储为{referencePrefix}-{id|__toString()}
  • getMethodNameForReference()(默认 getId)用于指定对象上哪个方法用于指定引用。默认为getId,并且始终回退到__toString()(如果存在)。
  • flushEveryXIterations()(默认 0)用于批量刷新,而不是在所有fixture持久化结束后仅刷新一次。
  • disableLogger()用于禁用SQL查询日志,这在运行时节省内存很有用。

这样,2个对象将自动持久化到数据库中,并且它们都用它们的ID进行标识。另外,如果你使用symfony app/console doctrine:fixtures:load命令并使用--append选项,数据库中将检测到ID,并且不会重复插入,也不会出错,这样你就可以真正地使用fixture作为参考数据!

查看AbstractFixture类以了解您可以覆盖的其他方法!