douyu3145 2016-12-06 14:15
浏览 43

学说动态映射

I try to improve the flexibility of our own CMS System. The functionality of this CMS system can be extended by Plugins. There are several instances of this "Core" CMS system. At the moment it isn't possible to deactivate such a plugin therefore every instance has all plugins activated although not every instance uses every plugin. Unfortunately it's possible that a newly created plugin is destroying some kind of base functionality of the CMS. My goal is the make the Plugin structure much more flexible. One task is to activate only plugins which will be used by an instance. My problem is that the doctrine mapping is handled by XML Files where for each Table every relation is defined.

For example: Let's assume we have the base class File which would be a "core" functionality. Some day we add a tagging Plugin where we add Tags to Files. For example we upload a "Lionel Messi" image and give it the tags: "Primera Division, soccer, argentinia, ...". A possible XML mapping could look like this:

<?xml version="1.0" encoding="utf-8"?>
   <doctrine-mapping
       xmlns="http://doctrine-project.org/schemas/orm/doctrine-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="Db\File" table="file">
    <!-- identical for all tables -->
    <unique-constraints>
        <unique-constraint columns="id"/>
    </unique-constraints>
    <id name="uid" type="integer" column="uid">
        <generator strategy="AUTO"/>
    </id>
    <field name="id" type="integer" column="id" nullable="true"/>
    <field name="title" type="string" column="title" length="255" nullable="true"/>

    <one-to-many field="fileTags" target-entity="Tag" mapped-by="file">
    </one-to-many>
</entity>
</doctrine-mapping>

The "file" table has a one-to-many relation with the "tag". This relation would be created for every CMS instance although the "tag" plugin is possibly not activated. The relation with "tags" should only be created if the "tag" plugin is active, otherwise no plugin specific mapping should be executed.

How is the best way with doctrine to handle such a task? Thanks for your help!

  • 写回答

1条回答 默认 最新

  • dongzi3805 2017-03-28 01:58
    关注

    You can use dynamic mapping.

    Try using the doctrine loadClassMetadata event to add additional mappings to the entities you wish to add tagging to. And to your tag entity itself.

        $mapping['fieldName'] = 'tag';
        $mapping['targetEntity'] = '<your entity>';
        $mapping['sourceEntity'] = '<your tag class>';
        $mapping['sourceToTargetKeyColumns'] = array('tag_id' => 'id');
        $mapping['targetToSourceKeyColumns'] = array('id' => 'tag_id');
        $mapping['fetch'] = 2;
        $mapping['joinColumns'] = array(
            array(
                'name' => 'tag_id',
                'unique' => false,
                'nullable' => false,
                'onDelete' => null,
                'columnDefinition' => null,
                'referencedColumnName' => 'id',
            )
        );
        $mapping['mappedBy'] = 'tag_id';
        $mapping['inversedBy'] = null;
        $mapping['orphanRemoval'] = false;
        $mapping['isOwningSide'] = true;
        $mapping['type'] = ClassMetadataInfo::MANY_TO_ONE;
    
        $vm = $this->em->getClassMetadata('<your tag class>');
    
        $vm->associationMappings["tagId"] = $mapping;
    

    This is not tested and is almost certainly a bit wrong, but it might give you some ideas to get you started.

    You could use XML to create the mappings you want. Then use your debugger to examine the resulting metadata. Then use the event listener to add that metadata dynamically. Finally remove the XML mapping and re-test.

    You will probably want to check the mappings on both the tag and the target entities if you want to reference entities from tags AND tags from entities

    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)