mmoreram/simple-doctrine-mapping

v1.2.0 2016-12-04 20:15 UTC

This package is auto-updated.

Last update: 2024-08-29 03:47:18 UTC


README

Build Status

保持简单,记得吗?

此项目提供了一种在Symfony项目中添加实体映射数据的方法,无需参与auto_mapping doctrine过程。

通过在您的bundle中添加简单的编译器通过,确保您的实体正确映射到数据库中,并为bundle用户提供一种覆盖和自定义这些实体的独特方式。

要使用此bundle的更高抽象级别,请使用http://github.com/mmoreram/BaseBundle中的映射部分。此bundle将SimpleDoctrineMapping与您的bundle集成到最佳程度。

跟我重复一遍,保持简单

编译器通过

对于那些还不了解编译器通过的人,试着把它想象成配置容器的最后机会。在这个时候,你可以检索所有参数配置,但不能构建任何服务,这是动态构建和完成服务的地方。

一旦编译完成,这个容器将是只读的。

此编译器通过让每个bundle负责其自身的实体,为每个实体定义映射的类、映射文件的路径以及将管理它的管理器。

你应该创建自己的编译器通过

<?php

/**
 * SimpleDoctrineMapping for Symfony2
 */

namespace TestBundle\CompilerPass;

use Symfony\Component\DependencyInjection\ContainerBuilder;

use Mmoreram\SimpleDoctrineMapping\CompilerPass\Abstracts\AbstractMappingCompilerPass;

/**
 * Class MappingCompilerPass
 */
class MappingCompilerPass extends AbstractMappingCompilerPass
{
    /**
     * You can modify the container here before it is dumped to PHP code.
     *
     * @param ContainerBuilder $container
     *
     * @api
     */
    public function process(ContainerBuilder $container)
    {
        $this
            ->addEntityMapping(
                $container,
                'default',
                'TestBundle\Entity\User',
                '@TestBundle/Mapping/User.orm.yml',
                true
            )
        ;
    }
}

并将其添加到容器中。就像这样

    <?php

    /**
     * SimpleDoctrineMapping for Symfony2
     */

    namespace Mmoreram\SimpleDoctrineMapping\Tests\Functional\TestBundle;

    use Symfony\Component\DependencyInjection\ContainerBuilder;
    use Symfony\Component\HttpKernel\Bundle\Bundle;

    use Mmoreram\SimpleDoctrineMapping\Tests\Functional\TestBundle\CompilerPass\MappingCompilerPass;

    /**
     * Class TestBundle
     */
    class TestBundle extends Bundle
    {
        /**
         * @param ContainerBuilder $container
         */
        public function build(ContainerBuilder $container)
        {
            $container->addCompilerPass(new MappingCompilerPass());
        }
    }

就是这样。在容器编译后,它将添加我们的映射信息。没有魔法。

addEntityMapping()

addEntityMapping()方法没有给我们提供很多选项,但足以在大多数情况下定义实体映射。

    /**
     * Add mapping entity
     *
     * This method adds a new Driver into global MappingDriverChain with single
     * entity mapping information.
     *
     * $entityManagerName must be an existing entityManager. By default doctrine
     * creates just one common EntityManager called default, but many can be
     * defined with different connection information
     *
     * p.e. default
     * p.e. anotherEntityManager
     *
     * $entityNamespace must be an existing namespace of Entity. This value also
     * can be a valid and existing container parameter, with an existing
     * namespace of an Entity as value.
     *
     * p.e. MyBundle\Entity\User
     * p.e. mybundle.entity.user.class
     *
     * $entityMappingFilePath must be a path of an existing yml or xml file with
     * mapping information about $entityNamespace. This bundle uses Short Bundle
     * notation, with "@" symbol. This value also can be a valid and existing
     * container parameter, with a path to an existing yml or xml file as value.
     *
     * p.e. @MyBundle/Resources/config/doctrine/User.orm.yml
     * p.e. @MyBundle/Resources/config/doctrine/User.orm.xml
     * p.e. mybundle.entity.user.mapping_file_path
     *
     * Finally, $enable flag just allow you to add current mapping definition
     * into all Doctrine Map table, or just dismiss it. This is useful when you
     * want to give possibility to final user to enable or disable a mapping
     * class.
     *
     * @param ContainerBuilder $container             Container
     * @param string           $entityManagerName     EntityManager name
     * @param string           $entityNamespace       Entity namespace
     * @param string           $entityMappingFilePath Entity Mapping file path
     * @param boolean          $enable                Entity mapping must be included
     *
     * @return $this self Object
     *
     * @throws EntityManagerNotFound Entity Manager not found
     */
    protected function addEntityMapping(
        ContainerBuilder $container,
        $entityManagerName,
        $entityNamespace,
        $entityMappingFilePath,
        $enable = true
    )

当然,所有值都是必需的,但最后一个不是。

参数

所以,想象一下你在工作在一个公共Bundle中,我的意思是,你的bundle将被其他项目在它们的vendor文件夹中安装,但你希望公开你的实体模型。

你可能认为通过使用此bundle你强迫最终用户在所有情况下使用你的模型实现,但事实并非如此。

如果你想给用户这种能力,如果你想公开可覆盖的实体,你可以使用容器参数定义你的模型。

parameters:

    #
    # Mapping information
    #
    test_bundle.entity.user.class: "TestBundle\Entity\User"
    test_bundle.entity.user.mapping_file_path: "@TestBundle/Mapping/Class.orm.yml"
    test_bundle.entity.user.entity_manager: default
    test_bundle.entity.user.enable: true

在这种情况下,你的bundle将把覆盖所有必要参数的能力留给用户,只需覆盖特定的配置项即可。

你必须最终用你的默认值创建这些参数。

<?php

/**
 * SimpleDoctrineMapping for Symfony2
 */

namespace TestBundle\CompilerPass;

use Symfony\Component\DependencyInjection\ContainerBuilder;

use Mmoreram\SimpleDoctrineMapping\CompilerPass\Abstracts\AbstractMappingCompilerPass;

/**
 * Class MappingCompilerPass
 */
class MappingCompilerPass extends AbstractMappingCompilerPass
{
    /**
     * You can modify the container here before it is dumped to PHP code.
     *
     * @param ContainerBuilder $container
     *
     * @api
     */
    public function process(ContainerBuilder $container)
    {
        $this
            ->addEntityMapping(
                $container,
                'test_bundle.entity.user.entity_manager',
                'test_bundle.entity.user.class',
                'test_bundle.entity.user.mapping_file_path',
                'test_bundle.entity.user.enable'
            )
        ;
    }
}