可重用捆绑软件的Symfony2/Doctrine2模型类映射 [英] Symfony2/Doctrine2 model classes mappings for reusable bundle

查看:90
本文介绍了可重用捆绑软件的Symfony2/Doctrine2模型类映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试使用模型类与Symfony2创建可重用的包,但是我无法注册它们的映射,因此Doctrine可以识别它们.

I am currently trying to create a reusable bundle with Symfony2 using model classes but I am not able to register their mappings so Doctrine recognize them.

我读到使用编译器传递可能是解决方案,因此我遵循了Symfony Cookbook( http://symfony.com/doc/current/cookbook/doctrine/mapping_model_classes.html ),我还查看了FOSUserBundle中的源代码以获取一些启发.

I read that using compiler pass could be the solution so I followed the guide in the Symfony Cookbook (http://symfony.com/doc/current/cookbook/doctrine/mapping_model_classes.html) and I also looked at the source code in the FOSUserBundle for some inspiration.

这是我到目前为止所做的:

And here is what I've done so far :

class GPGoodsBundle extends Bundle{
    public function build(ContainerBuilder $container)
    {
        parent::build($container);
        $this->addRegisterMappingsPass($container);
    }

    /**
     * @param ContainerBuilder $container
     */
    private function addRegisterMappingsPass(ContainerBuilder $container)
    {
        $modelDir = realpath(__DIR__.'/Resources/config/doctrine/model');
        $mappings = array(
            $modelDir => 'GP\Bundle\GoodsBundle\Model',
        );
        $ormCompilerClass = 'Doctrine\Bundle\DoctrineBundle\DependencyInjection\Compiler\DoctrineOrmMappingsPass';
        if (class_exists($ormCompilerClass)) {
            $container->addCompilerPass(
                DoctrineOrmMappingsPass::createXmlMappingDriver(
                    $mappings,
                array('gp_goods.model_manager_name'),
                'gp_goods.backend_type_orm'
                )
            );
        }
    }
}

但是,当尝试迁移我的实体(只是为了查看它是否正常工作)时,结果如下:

But when trying to migrate my entity (just to see if it's working) here is the result :

$php app/console doctrine:migrations:diff
No mapping information to process.

我的实体存储在"GP \ Bundle \ GoodsBundle \ Model"下,其映射存储在"GP \ Bundle \ GoodsBundle \ Resources \ config \ doctrine \ model"下

My entities are stored under "GP\Bundle\GoodsBundle\Model" and their mappings under "GP\Bundle\GoodsBundle\Resources\config\doctrine\model"

所以我的问题是:创建可重复使用的包的好方法是什么?如何注册模型类的映射?

So my question is : what is the good way to create a reusable bundle and how to register mappings of model classes?

如果您需要任何其他信息,请随时询问!

If you need any additional information, do not hesitate to ask!

谢谢您的帮助!

这是我的模型班之一:

class Good implements GoodInterface{
    /**
     * @var integer
     */
    private $id;

    /**
     * @var string
     */
    protected $serial;

    /**
     * @var \DateTime
     */
    protected $manufacturedAt;

    /**
     * @var \DateTime
     */
    protected $deliveredAt;

    /**
     * @var \DateTime
     */
    protected $expiredAt;

    /**
     * @var string
     */
    protected $checkInterval;

    /**
     * @var string
     */
    protected $status;

    /**
     * @var string
     */
    protected $slug;

    /**
     * @var \DateTime
     */

    protected $createdAt;

    /**
     * @var \DateTime
     */

    protected $updatedAt;

    /**
     * @var \DateTime
     */

    protected $deletedAt;

    public function __construct(){
        $this->createdAt = new \DateTime("now");
        $this->status = 'good';
    }

    .... getters/setters .....
}

以及映射:

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping   xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                xmlns:gedmo="http://gediminasm.org/schemas/orm/doctrine-extensions-mapping"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd">

    <entity name="GP\Bundle\GoodsBundle\Model\Good" table="good">

        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
        </id>

        <field name="serial" type="string" column="serial" length="255"/>

        <field name="manufacturedAt" type="date" column="manufactured_at"/>

        <field name="deliveredAt" type="date" column="delivered_at"/>

        <field name="expiredAt" type="date" column="expired_at"/>

        <field name="checkInterval" type="string" column="check_interval" length="255" nullable="true"/>

        <field name="status" type="string" column="status" length="255"/>

        <field name="slug" type="string" column="slug" length="255">
            <gedmo:slug fields="serial" unique="true" />
        </field>

        <field name="createdAt" type="datetime" column="created_at">
            <gedmo:timestampable on="create"/>
        </field>

        <field name="updatedAt" type="datetime" column="updated_at" nullable="true">
            <gedmo:timestampable on="update"/>
        </field>

        <field name="deletedAt" type="datetime" column="removed_at" nullable="true">
            <gedmo:soft-deleteable field-name="deletedAt"/>
        </field>

    </entity>
</doctrine-mapping>

推荐答案

当您在任何捆绑包之外有实体,或者该位置不是通常的捆绑包时,则必须将config.yml中的学说部分从

When you have entities outside of any bundle or that the location is not the usual one, you'll have to change the doctrine section in config.yml from

doctrine:
    # ...
    orm:
        # ...
        auto_mapping: true

doctrine:
    # ...
    orm:
        # ...
        mappings:
            model: # replace `model` with whatever you want
                type: annotation # could be xml, yml, ...
                dir: %kernel.root_dir%/../path/to/your/model/directory
                prefix: Prefix\Namespace # replace with your actual namespace
                alias: Model # replace with the desired alias
                is_bundle: false

dir参数通知Doctrine在哪里查找映射定义.如果您使用的是注释,它将成为您的模型目录.否则,它将是您的xml/yml文件的目录.

The dir parameter informs Doctrine where to look for the mapping definitions. If you're using annotations, it will be your model directory. Otherwise, it will be the directory of your xml/yml files.

实体的名称(要从Doctrine信息库访问)以Model开头,例如Model:User.它对应于参数alias.

Entities's names — to access from Doctrine repositories — begin with Model in this case, for example, Model:User. It corresponds to the parameter alias.

编辑配置文件时,请不要忘记清除缓存.

此外,在我的问题中,我写道我更改了Bundle类中的某些内容,但是它没有用,因为该捆绑包将不会被其他项目重用.所以我删除了所有内容.

Moreover, in my question, I wrote that I changed something in my Bundle class but it was not useful as the bundle won't be reused by another project. So I removed everything.

有关更多详细信息,请参见此答案: https://stackoverflow.com/a/10001019/2720307

See this answer for more details : https://stackoverflow.com/a/10001019/2720307

感谢Elnur Abdurrakhimov!

Thanks to Elnur Abdurrakhimov!

这篇关于可重用捆绑软件的Symfony2/Doctrine2模型类映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆