TYPO3 Extbase关系在创建时不起作用

时间:2017-03-06 10:35:40

标签: typo3 extbase

我正在使用Extbase for TYPO3制作示例博客。我的博客有很多帖子,一篇帖子有很多评论。问题是我在创建帖子时收到错误消息:"给予update()的修改对象不属于此存储库管理的类型(Myext \ Myblog \ Domain \ Model \ Blog)。 "

请查看我的Blog.html show.html

...
<p style="text-align: center">
    <f:link.action action="new" controller="Post" arguments="{blog:blog, post: post}"class="btn btn-primary">
      Add Post
    </f:link.action>
</p>
... 
来自帖子模板的

new.html:

<f:layout name="Default" />

This template displays a NEW form for the current domain object.

If you modify this template, do not forget to change the overwrite settings
in /Configuration/ExtensionBuilder/settings.yaml:
  Resources:
    Private:
      Templates:
        New.html: keep

Otherwise your changes will be overwritten the next time you save the extension in the extension builder

<f:section name="main">
<div class="container">
    <div class="clearfix"></div>
    <h1 style="color: black; text-align: center">New Post</h1>
    <f:flashMessages />

    <f:render partial="FormErrors" arguments="{object:Post}" />

    <f:form action="create"  enctype="multipart/form-data" name="newPost" object="{newPost}" >
    <f:render partial="Post/FormFields" arguments="{blog:blog, post:post}"/>
        <f:form.submit value="Create new" class="btn btn-primary" style="margin:10px 0" />
    </f:form>
</div>
</f:section>
来自Post的

部分FormFields.html:

<label for="title">
    <f:translate key="tx_myblog_domain_model_post.title" /> <span class="required">(required)</span>
</label><br />
    <f:form.textfield property="title"  style="width: 100%" /><br />
<label for="content">
    <f:translate key="tx_myblog_domain_model_post.content" />
</label><br />
    <f:form.textarea property="content" cols="60" rows="15" style="width: 100%"/><br />

最后是PostController:

class PostController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{

    protected $postRepository = NULL;


    public function injectPostRepository(\MyExt\Myblog\Domain\Repository\PostRepository $postRepository)
    {
        $this->postRepository = $postRepository;
    }


    /**
     * action list
     *
     * @return void
     */
    public function listAction()
    {
        $posts = $this->postRepository->findAll();
        $this->view->assign('posts', $posts);
    }

    public function showAction(\MyExt\Myblog\Domain\Model\Post $post)
    {
        $this->view->assign('post', $post);
    }

    public function newAction(\MyExt\Myblog\Domain\Model\Blog $blog, \MyExt\Myblog\Domain\Model\Post $post = NULL)
    {
        $this->view->assign('blog',$blog);
        $this->view->assign('post',$post);
    }

    public function createAction(\MyExt\Myblog\Domain\Model\Post $newPost)
    {
        $this->addFlashMessage('The object was created. Please be aware that this action is publicly accessible unless you implement an access check. See http://wiki.typo3.org/T3Doc/Extension_Builder/Using_the_Extension_Builder#1._Model_the_domain', '', \TYPO3\CMS\Core\Messaging\AbstractMessage::ERROR);
        $today = new \DateTime('now');
        $newPost->setPostDate($today);
        $this->postRepository->add($newPost);

        $this->objectManager->get('MyExt\\myblog\\Domain\\Repository\\BlogRepository')->update($blog);

        $this->redirect('list');
    }

1 个答案:

答案 0 :(得分:0)

您无需更新新记录。只需将其添加到存储库即可。

$today = new \DateTime('now');
$newPost->setPostDate($today);
$this->postRepository->add($newPost);

如果这不能继续手动:

$persistenceManager = $this->objectManager->get("TYPO3\\CMS\\Extbase\\Persistence\\Generic\\PersistenceManager");
$persistenceManager->persistAll();

顺便说一句

  • $ blog从未初始化。您的对象名为$ newBlog
  • MyExt \ myblog \ Domain \ Repository \ BlogRepository应为MyExt \ Myblog \ Domain \ Repository \ BlogRepository