0
votes

I am making the example blog with Extbase for TYPO3. My blog got many posts and one post got many comments. The problem is I got the error message when creating a post: "The modified object given to update() was not of the type (Myext\Myblog\Domain\Model\Blog) this repository manages. "

Please take a look at my show.html of Blog:

...
<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 from Post template:

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

Partial FormFields.html from Post:

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

Finally the 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

1 Answers

0
votes

You dont need to update the new record. Just add it to the repository.

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

If this dont work persist manually:

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

BTW

  • $blog has never been initialized. Your object is named $newBlog
  • MyExt\myblog\Domain\Repository\BlogRepository should be MyExt\Myblog\Domain\Repository\BlogRepository