XF 2.1 Sending alerts to specific user

shqawe

Member
I'm trying to send an alert to specific user using this code

PHP:
$getuser = $this->finder('XF:User')->where('user_id',$params['user_id'] );
$sender = $options->alert_sender_id;
$alertRepo = \XF::app()->repository('XF:UserAlert');
$alert = $alertRepo->alert($getuser->user_id, $sender, 'post', 1, 'test', 'mention');

But i got this error

Code:
ErrorException: [E_NOTICE] Undefined property: XF\Mvc\Entity\ArrayCollection::$user_id in src\addons\myAddon\XF\Admin\Controller\myController.php at line 107

Any one can help
 
You are not fetching the user. Also, you can use whereId method when using finder.

Try this:
PHP:
$getuser = $this->finder('XF:User')->whereId($params['user_id'])->fetchOne();
 
Last edited:
It's ok but it doesn't work i got this error now

Code:
TypeError: Argument 1 passed to XF\Repository\UserAlert::alert() must be an instance of XF\Entity\User, integer given
 
must be an instance of XF\Entity\User, integer given
Well, from description of this error it is kind of obvious what you need to do... Just pass $getuser entity in alrt(...) method.
PHP:
$alert = $alertRepo->alert($getuser, $sender, 'post', 1, 'test', 'mention');
 
You should have a template named "alert_user_mention".
In that template, you can add your code (i.e. title, body, etc.)

I would recommend that you change the template name to something like "alert_user_YOURADDON_mention" in order to avoid any conflict in the future with XF's templates.
 
Back
Top Bottom