2017-03-06 75 views
0

我正在用TYPO3的Extbase創建示例博客。我的博客有很多帖子,一篇文章有​​很多評論。問題是我在創建帖子時收到了錯誤消息:「給update()的修改對象不是該存儲庫管理的類型(Myext \ Myblog \ Domain \ Model \ Blog)。」創建TYPO3 Extbase關係不起作用

請看看我的博客的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> 

從後部分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'); 
    } 

回答

0

你不需要更新的新紀錄。只需將其添加到存儲庫即可。

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

如果不工作手動堅持:

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

BTW

  • $博客從未被初始化。你的對象被命名爲$ newBlog
  • MyExt \ myblog \域\庫\ BlogRepository應該MyExt \ Myblog \域\庫\ BlogRepository