DataWriter Post

Lu Jia

Active member
I want to create a post with DataWriter and I'm using this code:
PHP:
        /** @var $threadModel XenForo_Model_Thread */
        $threadModel = XenForo_Model::create('XenForo_Model_Thread');
        $thread = $threadModel->getThreadById($news['thread_id']);
 
        $writer = XenForo_DataWriter::create('XenForo_DataWriter_DiscussionMessage_Post');
        $writer->set('user_id', $user_id);
        $writer->set('username', $username);
        $writer->set('message', $text);
        $writer->set('message_state', 'visible');
        $writer->set('thread_id', $thread['thread_id']);
        $writer->save();
        $post = $writer->getMergedData();
 
        return $post;

When I try this code it will return this error :(

PHP:
Invalid controller response from JvCMS_ControllerPublic_Blog_News::actionAddComment
XenForo_FrontController->_handleControllerResponse() in XenForo/FrontController.php at line 318
XenForo_FrontController->dispatch() in XenForo/FrontController.php at line 132
XenForo_FrontController->run() in /var/www/vhosts/gw2.tv/httpdocs/index.php at line 13
The post will be created just a <p>text</p>, maybe I should do some function with the var.
Any suggestions?
Thanks in advance
 
Problem is in last line. You are running that code in controller's actionAddComment. That function should return response, but you are returning post. Replace last line with something like this
Code:
            return $this->responseRedirect(
                XenForo_ControllerResponse_Redirect::SUCCESS,
                $this->getDynamicRedirect(),
                new XenForo_Phrase('your_message_has_been_sent')
            );
 
I'll post you my code:
PHP:
        $writer = XenForo_DataWriter::create('XenForo_DataWriter_DiscussionMessage_Post');
        $writer->set('user_id', $user_id);
        $writer->set('username', $username);
        $writer->set('message', $text);
        $writer->set('message_state', 'visible');
        $writer->set('thread_id', $thread['thread_id']);
        $writer->save();
        $post = $writer->getMergedData();
 
        return $post;
 
        $viewParams = array(
            'onlineUsers' => $onlineUsers,
            'boardTotals' => $boardTotals,
        );
       
        return $this->responseRedirect(
            XenForo_ControllerResponse_Redirect::SUCCESS,
            XenForo_Link::buildPublicLink(' blog /news/'.$subSection[0].'.'.$subSection[1].''),
            new XenForo_Phrase('comment_inserted_successfully')
        );
 
Thanks, it works now :D
But it doesn't reload the comment, how can I force it? I guess i have to use some ajax or force a redirect to te same page.

EDIT: I resolved <p></p>. Just strip the html with a php function
PHP:
        $text = strip_tags($this->_request->getPost('message_html'));
 
Top Bottom