TYPO3 扩展思想实验:一次编辑新闻条目和评论条目? [英] TYPO3 extension thought experiment: Editing a news entry and the comment entries in one go?

查看:26
本文介绍了TYPO3 扩展思想实验:一次编辑新闻条目和评论条目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天我发布了一个类似但更复杂/长/案例特定的问题时,我想重新编写它并使其更容易相关.但请记住,这只是一个简化的例子,不是现实生活中的例子,所以我无法提供现实生活中的代码.我只需要知道这个想法是否可行,或者我是否应该寻找其他解决方案并且不要浪费时间:

As i posted a similar, but much more complex/long/case specific question yesterday, i thought of reformulating it and make it more easy to relate to. But keep in mind, it is just a simplified, not real life example, so i can't provide real life code. I just need to know if the idea is possible or if i should search for another solution and don't waste time on it:

我有一门课叫新闻.此类当然在其特定控制器中具有编辑和更新操作.在它旁边它有附加到它的类注释的对象(与这些注释对象的关系存储在对象存储中).通常,正如您想象的那样,您可以单独编辑新闻和评论,因为它们具有单独的控制器和它们的操作.但是:假设有没有办法一次性编辑新闻对象/记录及其评论?

I have a class called news. This class of course has an edit and update Action in its specific controller. Next to this it has objects of the class comment attached to it (the relation to these comment objects is stored in an Object Storage). Normally, as you can imagine, you can edit the news and the comments seperately as they have seperate controllers and their Actions. BUT: Is there a way to edit the news object/record and the comments of it in one go, hypothetically?

所以基本上新闻类的 editAction 将具有相应对象存储属性评论"的新闻对象分配给视图,在视图中,我可以通过它们在对象存储中的关系轻松访问每个评论并将它们实现为表单也改变他们的价值观...

So basically the editAction of the news class assigns the news object with its corresponding Object Storage property "comments" to the view and in the view i can easily access each comment through their relation in the Object Storage and implement them into a form to also change their values...

简化的 updateAction 看起来像这样(请记住,新闻的评论"属性是评论的对象存储):

The simplified updateAction would look like this (keep in mind the property "comments" of the news is the Object Storage for the comments):

       public function updateAction(\...\...\Domain\Model\news $news)
        {
            $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\CMS\Extbase\Object\ObjectManager');
            $commentRepository = $objectManager->get(\...\...\Domain\Repository\commentRepository::class);

             foreach($news->getComments() as $comment){
                $commentRepository->update($comment);   
            }

            $this->newsRepository->update($news);
            $this->redirect('list');
        }

(简化的)编辑模板如下所示:

The (simplified) Edit template would look like this:

<f:form action="update" name="news" object="{news}" >

<f:form.textarea property="newsentry" cols="40" rows="15" /><br />

<f:for each="{news.comments}" as="comment" >
        <f:form.textarea property="commententry" value="{comment.commententry}" /><br />
</f:for>

<f:form.submit value="Save" />

您在这个假设示例中看到了问题吗?只有新闻对象被传递给 updateAction,当然,存储与相应评论对象的关系的对象存储属性(评论")被传递给 updateAction,而不是更改的评论本身.因此,即使我在 updateAction 中有必要的代码来执行此操作,也只会保留已更改的新闻条目,而不是已更改的 commnetentries.

Do you see the problem in this hypothetical example? Only the news object is passed to the updateAction and of course the Object Storage property ("comments") where the relations to the corresponding comment objects are stored are passed to the updateAction, not the changed comments themselves. So only the changed newsentry is persisted not the changed commnetentries, even though i have the necessary code in the updateAction to do so.

所以,伙计们...有没有办法解决这个问题,或者根本不可能,我应该寻找其他方法来找到解决方案,不要在这上面浪费时间?

So, guys... Is there any solution to this problem or is it not possible at all and i should look for other means to find a solution and don't waste time on this?

推荐答案

我在我的项目中通过获取每个评论的键来处理它,因此您可以使用该属性.示例:

I handle it in my projects by getting the key of each comment, so you can use the property. Example:

<f:for each="{news.comments}" as="comment" key="commentIndex">
    <f:form.hidden property="comments.{commentIndex}.__identity" value="{comment.uid}" />
    <f:form.textarea property="comments.{commentIndex}.commententry" value="{comment.commententry}" />
</f:for>

重要的是 __identity 参数给 Extbase 重要的属性,在这里编辑评论记录.

Important is the __identity argument to give Extbase the important attribute which comment record is edited here.

您不需要更新 updateAction 中的每个评论,它会通过更新新闻记录自动完成.

You don't need to update each comment in the updateAction, it's done automatically by updating the news record.

这篇关于TYPO3 扩展思想实验:一次编辑新闻条目和评论条目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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