XF 2.0 how to create report with multi content?

HQCoder

Member
in xf1 i can create a report with multi content with this code
PHP:
$reportContent = array(
            "1" => array
            (
               "user_id" => '1',
               "username" => 'user 1',
            ),
            
            "2" => array
            (
               "user_id" => '2',
               "username" => 'user 2',
            ),
            
            "3" => array
            (
               "user_id" => '3',
               "username" => 'user 3',
            )
         );
$reportModel = XenForo_Model::create('XenForo_Model_Report');
$reportModel->reportContent('spamer', $reportContent, $message);

but in xf i use the code
Code:
$reportContent = array(
            "1" => array
            (
               "user_id" => '1',
               "username" => 'user 1',
            ),
            "2" => array
            (
               "user_id" => '2',
               "username" => 'user 2',
            ),
            "3" => array
            (
               "user_id" => '3',
               "username" => 'user 3',
            )
         );
$creator = \XF::service('XF:Report\Creator', 'spamer', $reportContent);
$creator->setMessage($message);
$creator->save();
'
it show an error
ErrorException: [E_RECOVERABLE_ERROR] Argument 3 passed to XF\Service\Report\Creator::__construct() must be an instance of XF\Mvc\Entity\Entity, array given, called in \src\XF\Container.php on line 275 and defined src\XF\Service\Report\Creator.php:37

is xf2 not support it???
 
As ever, you can't just assume that the approaches that were available in XF1 are available in XF2.

You will have to essentially forget how you did stuff in XF1 and look into the code to see how we do it in XF2 (which is presumably how you approached XF1 in the first place!).
 
As ever, you can't just assume that the approaches that were available in XF1 are available in XF2.

You will have to essentially forget how you did stuff in XF1 and look into the code to see how we do it in XF2 (which is presumably how you approached XF1 in the first place!).
the function to create the report content in xf 1. we can see array $content support
PHP:
public function reportContent($contentType, array $content, $message, array $viewingUser = null)
    {
        $this->standardizeViewingUserReference($viewingUser);

        if (!$viewingUser['user_id'])
        {
            return false;
        }  
        }

in xf2 it use Entity $content

PHP:
public function createReport($contentType, Entity $content)
    {
        $contentId = $content->getIdentifierValues();
        if (!$contentId || count($contentId) != 1)
        {
            throw new \InvalidArgumentException("Entity does not have an ID or does not have a simple key");
        }
        $contentId = intval(reset($contentId));

        $report = $this->finder('XF:Report')
            ->where('content_type', $contentType)
            ->where('content_id', $contentId)
            ->fetchOne();

        if (!$report)
        {
            $report = $this->em()->create('XF:Report');
            $report->content_type = $contentType;
            $report->content_id = $contentId;
        }

        /** @var \XF\Repository\Report $reportRepo */
        $reportRepo = $this->repository('XF:Report');
        $handler = $reportRepo->getReportHandler($contentType, true);
        if (!$handler)
        {
            throw new \LogicException("Cannot find report handler for '$contentType'");
        }

        $handler->setupReportEntityContent($report, $content);
        $this->report = $report;
        $this->content = $content;

        if ($forumId = $this->app->options()->reportIntoForumId)
        {
            $forum = $this->em()->find('XF:Forum', $forumId, 'Node');
            if ($forum)
            {
                $this->sendReportIntoForum($forum);
            }
        }
    }
i can write new function to do it in x2 but i want find the best way to do it via XF::Sevice
 
Top Bottom