duanhuang2150
2018-07-20 05:32
浏览 63
已采纳

如何在TYPO3的钩子中更新关系1:n的对象

I have an A entity and this have a property call B as relation 1:n from B to A. When I update A in TCA backend interface, when an particular field is active, the solution runs a hook of type function processDatamap_postProcessFieldArray (...)

I have to create a new element of B and save in ObjectStorage attribute of A. This works in execute time, create an objet and attaching it, but can not save in DB. I have tried with functions of extbase and Repository but it does not work. In my reserch identified the framework Doctrine for create queries, similar to persistence behavior, but in this case I could save the new object of B.

My question is: how could I use Doctrine for build query that allows to make update for object A, adding the new element B and save this element in the relation in DB.

I am working with TYPO3 7.6

图片转代码服务由CSDN问答提供 功能建议

我有一个A实体,它有一个属性调用B作为关系1:n从B到A.当我 更新A在TCA后端接口,当一个特定字段处于活动状态时,该解决方案运行一个类型函数的钩子processDatamap_postProcessFieldArray(...)

我必须创建 B的新元素并保存在A的ObjectStorage属性中。这在执行时,创建对象并附加它,但无法保存在DB中。 我已尝试使用extbase和Repository的功能,但它不起作用。 在我的研究中确定了用于创建查询的框架Doctrine,类似于持久性行为,但在这种情况下,我可以保存B的新对象。

我的问题是:我怎么能使用Doctrine for 构建查询,允许对对象A进行更新,添加新元素B并将此元素保存在DB中的关系中。

我正在使用TYPO3 7.6 < / DIV>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • doulv1760 2018-07-20 07:32
    已采纳

    You shouldn't use Extbase within the DataHandler hooks. Also plain database queries (neither with Dotrine or TYPO3_DB) are not good idea for creating entities within BE. Better way is to use TYPO3 DataHandler API. Example creation of Entity B during create/edit of Entity A could look like that.

    Register hook typo3conf/ext/example/ext_localconf.php

    defined('TYPO3_MODE') || die('Access denied.');
    
    $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass']['example'] = 'Vendor\\Example\\DataHandling\\DataHandler';
    

    typo3conf/ext/example/Classes/DataHandling/DataHandler.php

    namespace Vendor\Example\DataHandling;
    
    use TYPO3\CMS\Core\SingletonInterface;
    use TYPO3\CMS\Core\Utility\StringUtility;
    
    class DataHandler implements SingletonInterface
    {
        public function processDatamap_afterDatabaseOperations(
            string $status,
            string $table,
            $id,
            $fieldArray,
            \TYPO3\CMS\Core\DataHandling\DataHandler $dataHandler
        ) {
            // Do nothing if other table is processed
            if ($table !== 'tx_example_domain_model_entitya') {
                return;
            }
    
            // Get real UID of entity A if A is new record
            $idOfEntityA = $dataHandler->substNEWwithIDs[$id];
    
            // Example fields of entity B
            $entityB = [
                'sys_language_uid' => '0',
                'entitya' => $idOfEntityA,
                'hidden' => '0',
                'title' => 'I\'m entitty B',
                'starttime' => '0',
                'endtime' => '0',
                'pid' => $fieldArray['pid'],
            ];
    
            // Add entity B associated with entity A
            $dataHandler->start(
                [
                    'tx_example_domain_model_entityb' => [
                        StringUtility::getUniqueId('NEW') => $entityB
                    ]
                ],
                []
            );
            $dataHandler->process_datamap();
        }
    }
    

    Tested on 8.7, but will work on 7.6 too. Here you can read more about DataHandler https://docs.typo3.org/typo3cms/CoreApiReference/8.7/ApiOverview/Typo3CoreEngine/Database/

    评论
    解决 无用
    打赏 举报
  • 查看更多回答(1条)

相关推荐 更多相似问题