infinite-networks/specifications

Doctrine ORM 规范库

1.1.0 2018-10-12 02:58 UTC

This package is auto-updated.

Last update: 2024-08-24 17:39:14 UTC


README

这个库是一组遵循规范模式的对象基础集,基于一篇关于驯服 Doctrine 的 EntityRepository 类的博客文章。

http://www.whitewashing.de/2013/03/04/doctrine_repositories.html

如何使用这个库

这个库向基础 EntityRepository 类添加了 2 个方法,matchmatchOne,这两个方法都接受一个将构建查询构建器的规范。开发者需要实现满足他们领域需求的规范,每个规范都必须实现 Infinite\Specification\ORM\Specification 接口。

使用这个库的一个示例

    use Infinite\Specification\ORM as Spec;

    $repository = $managerRegistry->getRepository('Entity\User');

    // Retrieve any enabled users, ordered by username
    $enabledUsersSpec = new Spec\Sort(array('username' => 'ASC'), new Spec\AndX(array(
        new Spec\Equals('enabled', 1)
    ));
    $enabledUsers = $repository->match($enabledUsersSpec);

    // Count enabled users
    $countEnabledUsersSpec = new Spec\SingleScalar(new Spec\Count(new Spec\Equals('enabled', 1)));
    $enabledUsersCount = $repository->matchOne($countEnabledUsersSpec);

Doctrine 必须配置为使用不同的基础 EntityRepository 类,并且你应用程序中实现的任何 EntityRepositories 都必须使用这个库中定义的 EntityRepository 类作为其基类。

要在 Symfony2 中定义不同的 EntityRepository 基础类,请添加以下配置

doctrine:
    orm:
        default_repository_class: Infinite\Specification\ORM\EntityRepository