XF 2.0 LogicException: Entity is read only

Lukas W.

Well-known member
I'm getting a weird error on one of my cron jobs, when the job is run automatically through the job system. It runs fine however, when I manually start it. Any idea what that thing is trying to tell me? Or more important, how to fix it?

Code:
LogicException: Entity is read only src\XF\Mvc\Entity\Entity.php:517
Generated by: Unknown account Mar 19, 2018 at 12:15 PM


Stack trace
#0 src\XF\Mvc\Entity\Entity.php(505): XF\Mvc\Entity\Entity->set('rpgs_character_...', 1)
#1 src\addons\RPGS\Entity\Character.php(353): XF\Mvc\Entity\Entity->__set('rpgs_character_...', 1)
#2 src\XF\Mvc\Entity\Entity.php(1145): RPGS\Entity\Character->_postSave()
#3 src\addons\RPGS\Repository\RPG\Import.php(84): XF\Mvc\Entity\Entity->save()
#4 src\addons\RPGS\Repository\RPG\Import.php(43): RPGS\Repository\RPG\Import->importRPGLandRPG(Array, Object(League\Flysystem\EventableFilesystem\EventableFilesystem), Object(RPGS\Entity\Import))
#5 src\addons\RPGS\Cron\Import.php(16): RPGS\Repository\RPG\Import->processQueue()
#6 [internal function]: RPGS\Cron\Import::processQueue(Object(XF\Entity\CronEntry))
#7 src\XF\Job\Cron.php(36): call_user_func(Array, Object(XF\Entity\CronEntry))
#8 src\XF\Job\Manager.php(241): XF\Job\Cron->run(7.9999969005585)
#9 src\XF\Job\Manager.php(187): XF\Job\Manager->runJobInternal(Array, 7.9999969005585)
#10 src\XF\Job\Manager.php(76): XF\Job\Manager->runJobEntry(Array, 7.9999969005585)
#11 job.php(15): XF\Job\Manager->runQueue(false, 8)
#12 {main}


Request state
array(4) {
  ["url"] => string(8) "/job.php"
  ["referrer"] => string(30) "http://localhost:1337/rpgs/all"
  ["_GET"] => array(0) {
  }
  ["_POST"] => array(0) {
  }
}

PHP:
foreach($characters as $character) {
    /** @var \RPGS\Entity\Character $characterEntity */
    $characterEntity = $this->em->create('RPGS:Character');
    $characterEntity->rpg_id = $rpg->rpg_id;
    $characterEntity->name = trim($character);
    $characterEntity->user_id = $rpg->user_id;
    $characterEntity->save();
}
 
Have you tried looking in src\XF\Mvc\Entity\Entity.php on line 517 to see what causes the error?

When debugging and the error log gives you the stack trace / source of the error, the first step should always be to read the code that threw the exception and figure out why it was thrown, then work your way back from there.


Fillip
 
I did, but it is about as helpful as the original error message.
PHP:
if ($this->_readOnly)
{
    throw new \LogicException("Entity is read only");
}

I can't tell why it would be read-only, mostly because I didn't even know something like that'd exist.
 
What happens if you search for _readOnly in all files in your IDE?

You will find that either your Character entity is setting $this->_readOnly in the entity definition, or somewhere, setReadOnly(true); is being set on your entity.

The next step when debugging once you've found the cause of the exception, and it's not code you've written so you can't debug it directly, is to search for variable names like that.


Fillip
 
It's not called once in the entire execution chain, that's why I thought I may ask. \XF\Mvc\Entity only offers the function and never calls it itself, my own entity obviously doesn't call it either, because I didn't even know it existed, and there's no copy-paste stuff in there. The stack trace - as far as I can tell - suggest, that it's happening in the $rpg entity that is modified in the character post save, yet - just as in the character entity - readOnly is never touched or even mentioned.
 
Not knowing exactly what your code looks like, could it be trying to set $characterEntity for the unregistered user triggering the routine?

Unregistered users are read only by default ($visitor->setReadOnly(true);).
 
Oh that was sneaky! Thanks a bunch @Snog, there actually was a counter increment for the visitor object, hidden away in the postSave! The stack trace unfortunately hid that when cutting off the message. I've added a simple check to verify, that the current visitor has an ID and now it all runs through fine.
 
Back
Top Bottom