LPH
Well-known member
I'm attempting to create a post externally then have an alert sent to the author's XenForo account.
Here is the code:
An exception occurred: [InvalidArgumentException] Attempted to convert NULL to integer [content_id] in src/XF/Mvc/Entity/Entity.php on line 688
Here is the code:
PHP:
/**
* @param $post
*
* @return string
* @throws \Exception
*/
public function insert( $post ) {
$title = $this->getTitle( $post );
$message = $this->getMessage( $post );
// Get the forum using forum_id
$forum_id = $this->getForumId( $post );
$forum = \XF::em()->find( 'XF:Forum', $forum_id );
/** @var \XF\Entity\User $author **/
$author = \XF::app()->find('XF:User', $post->post_author);
$thread = \XF::asVisitor($author, function() use ($forum, $title, $message, $post)
{
/** @var \XF\Service\Thread\Creator $creator */
$creator = \XF::service('XF:Thread\Creator', $forum);
$creator->setContent( $title, $message );
$creator->setPrefix( $this->getForumId( $post ) );
$creator->setTags( $this->getTags( $post ) );
$creator->setIsAutomated();
if (!$creator->validate())
{
return null;
}
$creator->save();
$this->postMeta( $creator, $post );
});
$user = \XF::visitor();
/** @var \XF\Repository\UserAlert $alertRepo */
$alertRepo = \XF::app()->repository('XF:UserAlert');
$alert = $alertRepo->alertFromUser($user, $author, 'post', $thread->first_post_id, 'mention');
return $alert;
}