Fixed Error viewing reports

Tom

Active member
Affected version
2.0 Beta 1
I'm not sure if this is caused by an addon or not, but when attempting to view any report I am met by the following error:
Code:
ErrorException: Argument 1 passed to XF::asVisitor() must be an instance of XF\Entity\User, null given, called in /home/afellows/domains/dev.fellowsfilm.co.uk/public_html/src/XF/Repository/Report.php on line 87 and defined in src/XF.php at line 377
XF::handlePhpError() in src/XF.php at line 377
XF::asVisitor() in src/XF/Repository/Report.php at line 87
XF\Repository\Report->getModeratorsWhoCanHandleReport() in src/XF/Pub/Controller/Report.php at line 92
XF\Pub\Controller\Report->actionView() in src/XF/Mvc/Dispatcher.php at line 232
XF\Mvc\Dispatcher->dispatchClass() in src/XF/Mvc/Dispatcher.php at line 85
XF\Mvc\Dispatcher->dispatchLoop() in src/XF/Mvc/Dispatcher.php at line 41
XF\Mvc\Dispatcher->run() in src/XF/App.php at line 1771
XF\App->run() in src/XF.php at line 319
XF::runApp() in index.php at line 13
 
That particular error would suggest that you have a moderator record in the xf_moderator table who does not have a corresponding user record in the xf_user table.

We can perhaps do something to make sure this error doesn't happen in this specific case, though I'm more concerned about how that came to be in the first place.

Can you confirm if my suspicion is correct? Are there any non-existent users listed when you go to admin.php?moderators? Or are there more users listed in the xf_moderator table than are listed on this page?
 
Turns out this was caused by a legacy addon we had installed on XF1, allowing users to be thread moderators. The following is showing on the Moderators page in the ACP, despite the addon not even being installed/upgraded in XF2:

Screen Shot 2017-09-11 at 21.18.44.webp
 
Each of those blue headings should contain a user's name, though clearly the top one is empty - essentially this is the only problem here.

The blank entries underneath each name will basically be the threads, presumably. I suspect these would be populated if the add-on was still enabled / working. That should be safe to ignore for now.

If you can run some database queries, such as with PhpMyAdmin, the result of this query might be interesting:
SQL:
SELECT m.*
FROM xf_moderator AS m
LEFT JOIN xf_user AS user ON
    (m.user_id = user.user_id)
WHERE user.username IS NULL
That will probably tell us which record is the faulty one, then there'd be a couple of more queries after that to get you going for now.

We will still likely need to make some changes but a few things will get you up and running for now. It might be worth reporting it to the add-on developer as I still have a couple of questions as to how it came to be in the first place (by default XF will delete moderators when a user is deleted).
 
After looking into the DB in more detail, it seems that the 'content moderators' are not actually being stored in the xf_moderator table, but in a new table called xf_moderator_content. I would assume this is why XF's default behaviour does not apply here.

I'm going to simply remove this DB table's values for now, and that should hopefully resolve the issue. Thanks :).
 
You know what they say about assumptions ;)

The xf_moderator_content table is not new, it's actually been there since before XF 1.0.

There is likely a single record in the xf_moderator_content table which needed to be removed, but primarily there was likely a record in xf_moderator which pointed at a user who no longer exists. The query I asked you to run was going to ascertain which one that was, and therefore which record from xf_moderator_content should be removed.

The xf_moderator table lists each user who is any sort of moderator. The xf_moderator_content table lists each item of content they are a moderator of.
 
Apologies for the delay in getting back to you on this. I have ran the query, and the following has been returned:

Screen Shot 2017-09-14 at 00.45.36.webp

As expected, no user exists with the ID 3103.
 
Last edited:
It's worth noting that deleting the record in the xf_moderator_content table for the user ID 3103 has not resolved the error.
 
It won't do. You'd need to delete the record(s) in xf_moderator_content and the record in xf_moderator. It's primarily the xf_moderator record that's the problem, but you would want to delete the entries in xf_moderator_content too to avoid leaving orphaned data.

You may want to report the bug / work with the add-on developer to figure out how it came to be that a user was deleted but left the xf_moderator records. XF takes active steps to remove these when a user is deleted.

To decide whether to delete moderators or not, we check the is_moderator value when deleting the user. If this add-on doesn't set that for some reason then it would explain why deleting a user can leave orphaned data. Or there could be some other explanation.

In terms of the issue for XF2 itself, we've just added a fix for Beta 3 which will essentially suppress such an error in this particular case but that doesn't make it any less important to figure out what happened originally to leave the orphaned record.
 
Back
Top Bottom