TYPO3 extension thought experiment: Editing a news entry and the comment entries in one go?

时光总嘲笑我的痴心妄想 提交于 2020-01-05 08:10:00

问题


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?

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...

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" />

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?


回答1:


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>

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

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



来源:https://stackoverflow.com/questions/48537126/typo3-extension-thought-experiment-editing-a-news-entry-and-the-comment-entries

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!