XF 2.2 \XF\Job\AbstractRebuildJob and $user->delete() error (I have fixed it but needed better solution)

Scandal

Well-known member
Hello all! :)

I have built an \XF\Job\AbstractRebuildJob extension class for running my own rebuilding job.

It uses as intended a protected function getNextIds($start, $batch) method and the protected function rebuildById($id).

We're talking about User entity which fetched as $id.
On rebuildById($id) I do the following:
PHP:
$user = $this->app->em()->find('XF:User', $id, []);
// [...] some checks
$user->delete(false);

The problem: script was returning error:
ErrorException: [E_NOTICE] Trying to get property 'banner_date' of non-object in src\XF\Service\User\ProfileBanner.php at line 246

ProfileBanner.php line 246 is the following (if ($this->user->Profile->banner_date)):
PHP:
    protected function deleteBannerFiles()
    {
        if ($this->user->Profile->banner_date)
        {
            foreach ($this->sizeMap AS $code => $size)
            {
                \XF\Util\File::deleteFromAbstractedPath($this->user->Profile->getAbstractedBannerPath($code));
            }
        }
    }

Currently I have added a class extension to replace the above method and doing this before if ($this->user->Profile->banner_date):
if (isset($this->user->Profile->banner_date))
... so as to not have this error.

How could I fix this via a more normalize way?
 
This implies you have user entities with no corresponding profile entity/relation. That should never be the case, so it's worth trying to figure out how that might have happened to begin with, but ultimately you can just create the corresponding records in your database.
 
Back
Top Bottom