XF 2.0 [E_USER_WARNING] Accessed unknown getter

Cupara

Well-known member
As I'm still new to the XF2 code, this is all new to me.

So I followed the Demo Portal doc and got things up and going for a simple portal for me, but the issue I have when testing the Featured Thread part is when I delete a thread that is currently featured the following error shows:
Code:
ErrorException: [E_USER_WARNING] Accessed unknown getter 'FeaturedThread' on XF:NewsFeed[81] in src/XF/Mvc/Entity/Entity.php at line 175
[LIST=1]
[*]XF::handlePhpError()
[*]trigger_error() in src/XF/Mvc/Entity/Entity.php at line 175
[*]XF\Mvc\Entity\Entity->get() in src/XF/Mvc/Entity/Entity.php at line 92
[*]XF\Mvc\Entity\Entity->__get() in src/addons/GoblinTimes/xenPortal/Listener.php at line 50
[*]GoblinTimes\xenPortal\Listener::threadEntityPostDelete()
[*]call_user_func_array() in src/XF/Extension.php at line 35
[*]XF\Extension->fire() in src/XF.php at line 561
[*]XF::fire() in src/XF/Mvc/Entity/Entity.php at line 1516
[*]XF\Mvc\Entity\Entity->delete() in src/XF/Repository/NewsFeed.php at line 83
[*]XF\Repository\NewsFeed->unpublish() in src/XF/Behavior/NewsFeedPublishable.php at line 129
[*]XF\Behavior\NewsFeedPublishable->postDelete() in src/XF/Mvc/Entity/Entity.php at line 1513
[*]XF\Mvc\Entity\Entity->delete() in src/XF/Service/Thread/Deleter.php at line 71
[*]XF\Service\Thread\Deleter->delete() in src/XF/Pub/Controller/Thread.php at line 846
[*]XF\Pub\Controller\Thread->actionDelete() in src/XF/Mvc/Dispatcher.php at line 249
[*]XF\Mvc\Dispatcher->dispatchClass() in src/XF/Mvc/Dispatcher.php at line 89
[*]XF\Mvc\Dispatcher->dispatchLoop() in src/XF/Mvc/Dispatcher.php at line 41
[*]XF\Mvc\Dispatcher->run() in src/XF/App.php at line 1879
[*]XF\App->run() in src/XF.php at line 328
[*]XF::runApp() in index.php at line 13
[/LIST]
Now following the stuff above, I go to my Listener file:
PHP:
    public static function threadEntityPostDelete(\XF\Mvc\Entity\Entity $entity)
    {
        $featuredThread = $entity->FeaturedThread;
        if ($featuredThread)
        {
            $featuredThread->delete();
        }
    }

The part it points to is:
PHP:
$featuredThread = $entity->FeaturedThread;

This then points me to my entity file where FeaturedThread is defined:
PHP:
<?php

namespace GoblinTimes\xenPortal\Entity;

use XF\Mvc\Entity\Structure;

class FeaturedThread extends \XF\Mvc\Entity\Entity
{
    public static function getStructure(Structure $structure)
    {
        $structure->table = 'xenportal_featured_thread';
        $structure->shortName = 'XF:FeaturedThread';
        $structure->primaryKey = 'thread_id';
        $structure->columns = [
            'thread_id' => ['type' => self::UINT, 'required' => true],
            'featured_date' => ['type' => self::UINT, 'default' => time()]
        ];
        $structure->getters = [];
        $structure->relations = [
            'Thread' => [
                'entity' => 'XF:Thread',
                'type' => self::TO_ONE,
                'conditions' => 'thread_id',
                'primary' => true
            ],
        ];
   
        return $structure;
    }
}

I made sure I had shortName set properly and double checked my code, even triple checked it. Double checked to make sure I had the database set right. So I'm now scratching my head knowing it has to be something I'm just not seeing.

Edit: Just discovered that this also references my Editor file which is included:
PHP:
<?php

namespace GoblinTimes\xenPortal\XF\Service\Thread;

class Editor extends XFCP_Editor
{
    protected $featureThread;

    public function setFeatureThread($featureThread)
    {
        $this->featureThread = $featureThread;
    }

    protected function _save()
    {
        $thread = parent::_save();

        if ($this->featureThread !== null && $thread->discussion_state == 'visible')
        {
            /** @var \GoblinTimes\xenPortal\Entity\FeaturedThread $featuredThread */
            $featuredThread = $thread->getRelationOrDefault('FeaturedThread', false);

            if ($this->featureThread)
            {
                if (!$featuredThread->exists())
                {
                    $featuredThread->save();
                    $thread->fastUpdate('xenportal_featured', true);
                }
            }
            else
            {
                if ($featuredThread->exists())
                {
                    $featuredThread->delete();
                    $thread->fastUpdate('xenportal_featured', false);
                }
            }
        }

        return $thread;
    }
}

Line referenced here is:
PHP:
$featuredThread = $thread->getRelationOrDefault('FeaturedThread', false);

Which leads to my entity file so again full circle.
 
Looking at the error, it's essentially telling you what's happening:
Accessed unknown getter 'FeaturedThread' on XF:NewsFeed[81]
You're trying to access that on a XF:NewsFeed entity, which will logically fail (and indicates a bug).

If you look at the trace, it's coming from your threadEntityPostDelete function. You'll probably find that this function is running more often than you think -- I'm guessing it doesn't have a hint.
 
Looking at the error, it's essentially telling you what's happening:

You're trying to access that on a XF:NewsFeed entity, which will logically fail (and indicates a bug).

If you look at the trace, it's coming from your threadEntityPostDelete function. You'll probably find that this function is running more often than you think -- I'm guessing it doesn't have a hint.
I gave everything I added a hint but with this, its straight from the Let's build an add-on document so I can't see why it would be causing issues. I followed the document step by step.

Any suggestions for fixing this or do you think I should redo the entire document and just quadruple check everything?

EDIT: @Mike I disabled the event listener and it deletes fine but not from my xenportal_featured table. So definitely need to fix this.
 
Last edited:
PHP:
public static function threadEntityPostDelete(\XF\Mvc\Entity\Entity $entity)
{
     $featuredThread = $entity->FeaturedThread;
     if ($featuredThread)
     {
          $featuredThread->delete();
     }
}
That is my delete process causing issues.

@Kier figured I should tag you since you wrote the documents but I'm assuming. I followed the guide to a 'T' and still have this issue after testing.
 
Last edited:
Screenshot your listener configuration where you registered your threadEntityPostDelete method.
 
Second screenshot.

Also, I added a hint and it deletes threads and does what it should but when I delete the thread the listener isn't firing deleting from my xenportal_featured_thread table.
 
Well the lack of hint is the problem I was mentioning -- if you don't hint, it fires for every type of entity. You need to use a hint. (This probably isn't as clear as it can be in the example; it's implying that you use essentially the same configuration as the previous listener which has a hint, except for the noted changes.)

If your code isn't firing when you enter a hint, then that would likely indicate that the hint is wrong. If the code is firing but it's not doing what you expect, that would appear to point to an issue within your code itself.
 
@Mike I added the hint but did the wrong one. I got it solved, that was my fault for not paying attention. LOL

You said it in your first post and it didn't register with me but now I know so when someone else points out the same thing I'll check that first.
 
Back
Top Bottom