Duplicate Error: Call to a member function canView() on null src/addons/XFMG/Entity/MediaItem.php:94

gotski

Active member
Affected version
2
Hello, I received some errors:

  • Error: Call to a member function canView() on null
  • src/addons/XFMG/Entity/MediaItem.php:94
  • Generated by: member
  • Dec 8, 2017 at 9:25 PM
Stack trace
#0 src/addons/XFMG/BbCode/Gallery.php(59): XFMG\Entity\MediaItem->canView()
#1 [internal function]: XFMG\BbCode\Gallery::renderTagGallery(Array, 'media, 65387', Array, Array, Object(XF\BbCode\Renderer\Html))
#2 src/XF/BbCode/Renderer/Html.php(234): call_user_func(Array, Array, 'media, 65387', Array, Array, Object(XF\BbCode\Renderer\Html))
#3 src/XF/BbCode/Traverser.php(49): XF\BbCode\Renderer\Html->renderTag(Array, Array)
#4 src/XF/BbCode/Traverser.php(29): XF\BbCode\Traverser->renderSubTree(Array, Array)
#5 src/XF/BbCode/Traverser.php(18): XF\BbCode\Traverser->renderAst(Array, Object(XF\BbCode\RuleSet), Array)
#6 src/XF/SubContainer/BbCode.php(202): XF\BbCode\Traverser->render('[GALLERY=media,...', Object(XF\BbCode\Parser), Object(XF\BbCode\RuleSet), Array)
#7 src/XF/Template/Templater.php(1839): XF\SubContainer\BbCode->render('[GALLERY=media,...', 'html', 'post', Object(XFMG\XF\Entity\Post), Array)
#8 [internal function]: XF\Template\Templater->fnBbCode(Object(XF\Template\Templater), false, '[GALLERY=media,...', 'post', Object(XFMG\XF\Entity\Post))
#9 src/XF/Template/Templater.php(861): call_user_func_array(Array, Array)
#10 internal_data/code_cache/templates/l1/s2/public/post_macros.php(121): XF\Template\Templater->fn('bb_code', Array, false)
#11 src/XF/Template/Templater.php(643): XF\Template\Templater->{closure}(Object(XF\Template\Templater), Array, Array)
#12 internal_data/code_cache/templates/l1/s2/public/thread_view.php(516): XF\Template\Templater->callMacro('post_macros', 'post', Array, Array)
#13 src/XF/Template/Templater.php(1230): XF\Template\Templater->{closure}(Object(XF\Template\Templater), Array)
#14 src/XF/Template/Template.php(24): XF\Template\Templater->renderTemplate('thread_view', Array)
#15 src/XF/Mvc/Renderer/Html.php(48): XF\Template\Template->render()
#16 src/XF/Mvc/Dispatcher.php(332): XF\Mvc\Renderer\Html->renderView('XF:Thread\\View', 'public:thread_v...', Array)
#17 src/XF/Mvc/Dispatcher.php(303): XF\Mvc\Dispatcher->renderView(Object(XF\Mvc\Renderer\Html), Object(XF\Mvc\Reply\View))
#18 src/XF/Mvc/Dispatcher.php(44): XF\Mvc\Dispatcher->render(Object(XF\Mvc\Reply\View), 'html')
#19 src/XF/App.php(1879): XF\Mvc\Dispatcher->run()
#20 src/XF.php(328): XF\App->run()
#21 index.php(13): XF::runApp('XF\\Pub\\App')
#22 {main}


[SIZE=5]Request state[/SIZE]
array(4) {
["url"] => string(95) "/threads/threadname.4268/page-28"
["referrer"] => string(123) "https://www.sitename.net/threads/threadname.4268/page-27"
["_GET"] => array(0) {
}
["_POST"] => array(0) {
}
}
 
I received this errors if I open a tab "Postings" on some member's profile pages, please help to fix it...

  • Error: Call to a member function canView() on null
  • src/addons/XFMG/Entity/MediaItem.php:94
  • Generated by: admin
  • Dec 9, 2017 at 8:09 AM
Stack trace
#0 src/addons/XFMG/Entity/Comment.php(49): XFMG\Entity\MediaItem->canView(NULL)
#1 src/XF/Search/Data/AbstractData.php(122): XFMG\Entity\Comment->canView(NULL)
#2 src/XF/Search/Search.php(222): XF\Search\Data\AbstractData->canViewContent(Object(XFMG\Entity\Comment))
#3 [internal function]: XF\Search\Search->XF\Search\{closure}(Object(XFMG\Entity\Comment))
#4 src/XF/Mvc/Entity/AbstractCollection.php(167): array_filter(Array, Object(Closure))
#5 src/XF/Search/Search.php(223): XF\Mvc\Entity\AbstractCollection->filter(Object(Closure))
#6 src/XF/ResultSet.php(228): XF\Search\Search->getResultSetData('xfmg_comment', Array, true, Array)
#7 src/XF/ResultSet.php(212): XF\ResultSet->loadResultsData(true)
#8 src/XF/ResultSet.php(181): XF\ResultSet->limitToViewableResults()
#9 src/XF/ResultSet.php(191): XF\ResultSet->sliceResults(0, '200', true)
#10 src/XF/Search/Search.php(166): XF\ResultSet->limitResults('200', true)
#11 src/XF/Repository/Search.php(34): XF\Search\Search->search(Object(XF\Search\Query\Query))
#12 src/XF/Pub/Controller/Search.php(381): XF\Repository\Search->runSearch(Object(XF\Search\Query\Query), Array, false)
#13 src/XF/Pub/Controller/Search.php(239): XF\Pub\Controller\Search->runSearch(Object(XF\Search\Query\Query), Array, false)
#14 src/XF/Mvc/Dispatcher.php(249): XF\Pub\Controller\Search->actionMember(Object(XF\Mvc\ParameterBag))
#15 src/XF/Mvc/Dispatcher.php(88): XF\Mvc\Dispatcher->dispatchClass('XF:Search', 'Member', 'html', Object(XF\Mvc\ParameterBag), '', Object(XF\Pub\Controller\Search), NULL)
#16 src/XF/Mvc/Dispatcher.php(41): XF\Mvc\Dispatcher->dispatchLoop(Object(XF\Mvc\RouteMatch))
#17 src/XF/App.php(1879): XF\Mvc\Dispatcher->run()
#18 src/XF.php(328): XF\App->run()
#19 index.php(13): XF::runApp('XF\\Pub\\App')
#20 {main}


Request state
array(4) {
["url"] => string(28) "/search/member?user_id=62434"
["referrer"] => string(54) "https://www.sitename.net/members/member.62434/"
["_GET"] => array(1) {
["user_id"] => string(5) "62434"
}
["_POST"] => array(0) {
}
}
 

I checked and I have another problem, media files in non-existent albums :( I do not know how it happened, but that's what causes the problem.... how can I fix this? query in mysql?




ErrorException: [E_NOTICE] Undefined variable: canView src/addons/XFMG/Entity/MediaItem.php:99
Generated by: admin Dec 10, 2017 at 9:30 PM
Stack trace
#0 src/addons/XFMG/Entity/MediaItem.php(99): XF::handlePhpError(8, '[E_NOTICE] Unde...', '/var/www/admin/d...', 99, Array)
#1 src/addons/XFMG/Pub/Controller/AbstractController.php(61): XFMG\Entity\MediaItem->canView(NULL)
#2 src/addons/B2W/ViewThumbnailsPermissions/Pub/Controller/Media.php(32): XFMG\Pub\Controller\AbstractController->assertViewableMediaItem('2660')
#3 src/addons/B2W/ViewThumbnailsPermissions/Pub/Controller/Media.php(20): B2W\ViewThumbnailsPermissions\Pub\Controller\Media->_assertCanViewFullMedia(Object(XF\Mvc\ParameterBag))
#4 src/XF/Mvc/Dispatcher.php(249): B2W\ViewThumbnailsPermissions\Pub\Controller\Media->actionView(Object(XF\Mvc\ParameterBag))
#5 src/XF/Mvc/Dispatcher.php(88): XF\Mvc\Dispatcher->dispatchClass('XFMG\\Pub\\Contro...', 'View', 'html', Object(XF\Mvc\ParameterBag), 'xfmg', Object(B2W\ViewThumbnailsPermissions\Pub\Controller\Media), Object(XF\Mvc\Reply\Reroute))
#6 src/XF/Mvc/Dispatcher.php(41): XF\Mvc\Dispatcher->dispatchLoop(Object(XF\Mvc\RouteMatch))
#7 src/XF/App.php(1879): XF\Mvc\Dispatcher->run()
#8 src/XF.php(328): XF\App->run()
#9 index.php(13): XF::runApp('XF\\Pub\\App')
#10 {main}
Request state
array(4) {
["url"] => string(32) "/media/20171208_214322-jpg.2660/"
["referrer"] => bool(false)
["_GET"] => array(0) {
}
["_POST"] => array(0) {
}
}
 
There's two parts to the fix, that error suggests you didn't do the second bit. Here's the second part:

https://xenforo.com/community/threads/server-errors-every-few-minutes.138425/post-1198293

this query

SELECT * FROM xf_mg_media_item
WHERE category_id = 0 AND album_id = 0

and

UPDATE xf_mg_media_item SET category_id=18 WHERE category_id=0 AND album_id=0

do not work for me, because my media have a album_id.... but the albums with this ID does not exist in xf_mg_album table
how do I compose a query to move all media wtih ID of nonexistent albums to an existing one?
 
Try:
SQL:
UPDATE xf_mg_media_item SET album_id = <existing_album_id> WHERE album_id = <non_existing_album_id>
Messing around in the database really isn't recommended though. The fix I've provided should be enough to stop the errors.
 
Try:
SQL:
UPDATE xf_mg_media_item SET album_id = <existing_album_id> WHERE album_id = <non_existing_album_id>
Messing around in the database really isn't recommended though. The fix I've provided should be enough to stop the errors.
Thank you!
You are right about the mess, and to solve it, I need to find all the nonexistent album ID.

The request you provide solves a single problem, but I have thousands of albums, I do not know how many of them do not exist.
 
I found a lot of media files from the albums that were deleted, and the files remained. But the reason is in version 1, I checked my backup that I made before upgrade.

This is a small script that helped me get a list of nonexistent albums, perhaps in the future it will help someone else.

Code:
$dbh = new PDO('mysql:host=localhost;dbname=db_name', 'db_username', 'db_password');

    foreach($dbh->query('SELECT `album_id` FROM `xf_mg_album`') as $row) {
        $albumarr[] = $row[0];
    }
   
    foreach($dbh->query('SELECT `album_id` FROM `xf_mg_media_item`') as $row) {
        if ($row[0] != 0) {
        $mediaarr[] = $row[0];
        }
    }  
   

foreach(array_unique($mediaarr) as $ID) {

if (!in_array($ID, $albumarr)) {
    echo $ID . '<br />';
}
}


$dbh = null;


After you get the list, you can use mysql query suggested by the Chris.

UPDATE xf_mg_media_item SET album_id = <existing_album_id> WHERE album_id = <non_existing_album_id>

this will help move all media listed in non-existent albums.
 
Last edited:
if you have many files, then you can use this script to not send tens or hundreds of queries manually.

PHP:
$existing_album_id = 'YOUR existing ALBUM ID';

$dbh = new PDO('mysql:host=localhost;dbname=db_name', 'db_username', 'db_password');

    foreach($dbh->query('SELECT `album_id` FROM `xf_mg_album`') as $row) {
        $albumarr[] = $row[0];
    }
    
    foreach($dbh->query('SELECT `album_id` FROM `xf_mg_media_item`') as $row) {
        if ($row[0] != 0) {
        $mediaarr[] = $row[0];
        }
    }   
    

foreach(array_unique($mediaarr) as $ID) {

if (!in_array($ID, $albumarr)) {
    echo $ID . '<br />';
    $stmt = $dbh->prepare('UPDATE `xf_mg_media_item` SET `album_id` = '.$existing_album_id.' WHERE `album_id` ='.$ID);
    $stmt->execute();
}
}

$dbh = null;
 
Back
Top Bottom