Fixed MOTM album creation bug

MtoR

Freaky Coder
Staff member
Joined
Nov 14, 2015
Messages
3,904
Error on 4.0.5 (and probably 4.0.6 as well as the code causing the error seems to be identical in both versions):


Code:
InvalidArgumentException: Attempted to convert NULL to string/binary [username] in src/XF/Mvc/Entity/Entity.php:691


#0 src/XF/Mvc/Entity/Entity.php(575): XF\Mvc\Entity\Entity->_castValueToType(NULL, 'username', 5, Array)

#1 src/XF/Mvc/Entity/Entity.php(505): XF\Mvc\Entity\Entity->set('username', NULL)

#2 src/addons/XFMG/Service/Album/Creator.php(52): XF\Mvc\Entity\Entity->__set('username', NULL)

#3 src/addons/XFMG/Service/Album/Creator.php(43): XFMG\Service\Album\Creator->setUser(Object(XFMG\XF\Entity\User))

#4 src/addons/XFMG/Service/Album/Creator.php(34): XFMG\Service\Album\Creator->setAlbum()

#5 src/XF/Container.php(273): XFMG\Service\Album\Creator->__construct(Object(XF\Pub\App))

#6 src/XF/App.php(1359): XF\Container->createObject('XFA\\XFMGMediaOf...', Array)

#7 src/XF/Container.php(228): XF\App->XF\{closure}('XFA\\XFMGMediaOf...', Array, Object(XF\Container))

#8 src/XF/App.php(2508): XF\Container->create('service', 'XFMG\\Service\\Al...', Array)

#9 src/addons/XFA/XFMGMediaOfTheMonth/Service/Album/Create.php(51): XF\App->service('XFMG\\Service\\Al...')

#10 src/addons/XFA/XFMGMediaOfTheMonth/Cron/XFMGMediaOfTheMonth.php(172): XFA\XFMGMediaOfTheMonth\Service\Album\Create->create()

#11 src/addons/XFA/XFMGMediaOfTheMonth/Cron/XFMGMediaOfTheMonth.php(19): XFA\XFMGMediaOfTheMonth\Cron\XFMGMediaOfTheMonth::cronMode1()

#12 [internal function]: XFA\XFMGMediaOfTheMonth\Cron\XFMGMediaOfTheMonth::cron(Object(XF\Entity\CronEntry))

#13 src/XF/Job/Cron.php(35): call_user_func(Array, Object(XF\Entity\CronEntry))

#14 src/XF/Job/Manager.php(241): XF\Job\Cron->run(8)

#15 src/XF/Job/Manager.php(187): XF\Job\Manager->runJobInternal(Array, 8)

#16 src/XF/Job/Manager.php(76): XF\Job\Manager->runJobEntry(Array, 8)

#17 job.php(14): XF\Job\Manager->runQueue(false, 8)

#18 {main}

This happens when a guest triggers the creation of an album:

The Album Creator Service (\XFMG\Service\Album\Creator) implicitly calls $this->setAlbum() in its constructor which then calls
PHP:
$this->setUser(\XF::visitor())
which finally tries to set the username on the entitiy, but guests do not have a username.


To prevent this, XFA\XFMGMediaOfTheMonth\Service\Album\Creator::create() needs to wrap the entitiy manger call in \XF::asVisitor()


This is actually pretty much the same issue as https://xenforo.com/community/threads/xfa-media-of-the-month-xf2-paid.151970/post-1293110 - this also happend (it should not happen with 4.0.6 due to \XF::asVisitor) because a guest triggered the cronjob and the thread replier service implicitly set the user to \XF::visitor() (and used that further down) in its constructor.
 

JayT

Member
Joined
Jun 25, 2016
Messages
99
Is this error going to be fixed? This is now the third month the add-on has failed to start an album for me. At some point my license will expire without a single contest starting.
 

MtoR

Freaky Coder
Staff member
Joined
Nov 14, 2015
Messages
3,904
Yes for sure, I am going to fix it this weekend.
I am sorry for the inconvenience, as I was manually running the cron in my tests I never came in front of that bug.