XF 2.1 Archive previous entries by user

Matt C.

Well-known member
In my matchmaking add-on, I want users to only be able to have one entry visible at a time. If a user creates a new match, then their previous match will be archived.

Code:
/** @var \AH\Matchmaking\Finder\Match $matchFinder */
$matchFinder = $this->finder('AH\Matchmaking:Match');
$oldMatch = $matchFinder
            ->where('user_id', '=', $visitor->user_id)
            ->where('archived', '=', false)
            ->fetch();
           
if ($oldMatch)
{
    $oldMatch->archived = true;
}

Here is the save code all together (Yes I know it's not pretty, but this is the only I know how to save entities):
Code:
public function actionSave(ParameterBag $params)
{
    $this->assertPostOnly();

    $visitor = \XF::visitor();
    if (!$visitor->canAddMatch())
    {
        throw $this->exception($this->noPermission());
    }

    /** @var \AH\Matchmaking\Finder\Match $matchFinder */
    $matchFinder = $this->finder('AH\Matchmaking:Match');
    $oldMatch = $matchFinder
                ->where('user_id', '=', $visitor->user_id)
                ->where('archived', '=', false)
                ->fetch();

    if ($params->match_id)
    {
        $match = $this->assertMatchExists($params->match_id);
    }
    else
    {
        $match = $this->em()->create('AH\Matchmaking:Match');
    }

    $title = $this->filter('title', 'str');
    $platform = $this->filter('platform', 'str');
    $voice = $this->filter('voice', 'bool');
    $match_username = $this->filter('match_username', 'str');
    $description = $this->filter('description', 'str');

    $match->title = $title;
    $match->platform = $platform;
    $match->voice = $voice;
    $match->match_username = $match_username;
    $match->description = $description;
    $match->user_id = $visitor->user_id;
    $match->username = $visitor->username;
    $match->warning_id = '';
    $match->warning_message = '';
    $match->match_state = 'visible';
    $match->archived = false;

    $match->save();

    if ($oldMatch)
    {
        $oldMatch->archived = true;
    }

    return $this->redirect($this->buildLink('matchmaking'));
}

This didn't work. If someone could help me out, that would be great.
 
Last edited:
Back
Top Bottom