我的扩展程序如何从 TYPO3 CMS 7.6 中的 tt_content 读取正文字段?

How does my extension read bodytext fields from tt_content in TYPO3 CMS 7.6?

我正在使用 TYPO3 CMS 7.6.x LTS 和 Extension Builder 编写 TYPO3 扩展。我的扩展任务之一是扫描 TYPO3 的 tt_content table 的 "header" 和 "bodytext" 字段;匹配 "bodytext" 中的特定文本模式;解析找到的文本;并将生成的数据连同 tt_content "header" 字段中的标签一起放入我的扩展日志 table 中。

"Using Foreign Data Sources" in "Developing TYPO3 Extensions with Extbase and Fluid" at [ https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/4-use-foreign-data-sources.html ] 讨论了使用 TypoScript 映射将数据放入 tt_address。相反,我正在从 tt_content 读取数据,并且宁愿留在扩展中的 PHP 内。

Foreign Key to TYPO3 Frontend User 中的图表为例,我在 Extension Builder 中向扩展的域模型添加了第二个模型。我做了一个"TtContent"模型,用它的"Domain object settings"到"map to existing table",它的入口是"tt_content"。我没有在 "extend using model class" 字段中输入值。我在我的 Log 模型中创建了一个 "relation" 字段,并且 运行 从我的 Log:ttContent 关系字段连接到我的 TtContent 模型的标题栏。然后当我单击 "Save" 时,Extension Builder 回复:“table "tt_content" 的配置与 extbase 不兼容。如果你想映射到这个 [=45],你必须自己配置它=](错误 606)”。我不明白这个错误信息。

在 Extension Builder 中,我是否在我的日志模型中建立与现有外部 class 的关系?如果是这样,我使用哪个外部 class?或者,我是否在 Extension Builder 中制作 tt_content 的模型,并以某种方式解决错误 606?这看起来应该不难,因为我想从 TYPO3 中已有的 table 中读取。

解决方案:创建一个 Extbase 模型和一个表示现有 tt_content table 的存储库,然后编写一个 TypoScript 属性 映射。

在 TYPO3 Extension Builder 的域建模器中,添加一个名为 "Content" 的模型或一些让您想起 tt_content table 的名称。在内容模型的 "domain object settings" 部分,将 "tt_content" 放入 "map to existing table" 框中。没关系 "extend existing model class" 框,因为 Extbase 没有 tt_content.

这样的 class

同样在域对象设置中,将对象类型设置为"Entity",选中"is aggregate root"框,取消选中"add deleted field"、"add hidden field"、"add starttime/endtime fields" 和 "enable categorization" 个框。将对象类型设置为 "entity" 并选中 "is aggregate root?" 框会导致 Extension Builder 为您的内容模型创建存储库。

向您的内容模型添加属性,这些属性表示您要在 tt_content 数据库 table 中访问的列。在我的内容模型中,我只添加了 "header" 和 "bodytext" 属性。

注意:您不需要将 TYPO3 的 uid 或 pid 属性添加到内容模型。这些属性已从父 \TYPO3\CMS\Extbase\DomainObject\AbstractDomainObject class.

扩展而来

在 Extension Builder 中单击 "Save" 以保存您的新域模型。您将收到一条消息:“警告!table "tt_content" 的配置与 extbase 不兼容。如果要映射到此 table,您必须自己配置它(错误 606) . 还是要保存吗?是的,无论如何都要保存。 Extension Builder 将响应,"Success. The Extension was saved. Your Extension is not installed yet." 退出 Extension Builder。

注意:如果您在 Extension Builder 中进行了多次建模迭代,您可能会在扩展的最终代码中发现早期迭代遗留下来的工件。 Extension Builder 会覆盖您的一些扩展区域,但不会影响其他区域。请参阅位于 [ https://docs.typo3.org/typo3cms/extensions/extension_builder/Configuration/Index.html ] 的扩展构建器配置参考。

使用 Extbase table 映射来配置,从而访问来自 TYPO3 tt_content table 的内容。使用 "typo3conf/ext/yourextensionkey/ext_typoscript_setup.txt" 文件中的 TypoScript "config.tx_extbase.persistence.classes" 映射参数执行此配置。 Extension Builder 在您保存领域模型时创建了这个文件。这是错误606中提到的配置。

请参阅 [https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/4-use-foreign-data-sources.html ]. This TypoScript code example defines the mapping lines themselves. You may wish to use "config.tx_extbase" instead of "plugin.tx_myextension". The next page, "Modeling the Class Hierarchy" at [ https://docs.typo3.org/typo3cms/ExtbaseFluidBook/6-Persistence/5-modeling-the-class-hierarchy.html ] "Using Foreign Data Sources" 中的代码示例,其中有 "config.tx_extbase" 的代码示例;但它不显示映射线本身。

在我的情况下,我在 ext_typoscript_setup.txt 中添加了 TypoScript 指令来映射 "header" 和 "bodytext" 列。我还删除了 Extension Builder 写的 recordType = Tx_Myextensionkey_Content 行,因为我想读取 tt_content 已经存在的记录,而不是我的扩展创建的记录。

config.tx_extbase{
    persistence{
        classes{

            Mynamespace\Myextensionkey\Domain\Model\Content {
                mapping {
                    tableName = tt_content
                    columns {
                        header.mapOnProperty = header
                        bodytext.mapOnProperty = bodytext
                    }
                }
            }

        }
    }
}

我的 TYPO3 扩展现在可以读取 tt_content。

注意:在 ext_typoscript_setup.txt 中为 recordType 赋值会导致 Extbase 持久层在基础 tt_content.CType 列中搜索 一个值 。 Extbase 通过它的 \TYPO3\CMS\Extbase\Configuration\AbstractConfigurationManager getConfiguration() $frameworkConfiguration 数组和它的 \TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapFactory buildDataMapInternal() 函数来做到这一点。在撰写本文时,您 不能 在 recordType 值中使用通配符,例如 *%;而你不能给出一个值列表,例如text, textmedia.