XF 2.0 Delete thread in Cron

AndyB

Well-known member
In an add-on called Auto delete I would like to have a cron entry delete a thread. When I run this code from the cron entry:

PHP:
foreach ($threads AS $k => $v)
{
	$threadId = $v['thread_id'];
	$type = 'hard';
	$reason = '';

	$finder = \XF::finder('XF:Thread');
	$thread = $finder
		->where('thread_id', '=', $threadId)
		->fetch();

	$deleter = \XF::app()->service('XF:Thread\Deleter', $thread);

	$deleter->delete($type, $reason);
}

I get the following error:
Code:
An exception occurred: [TypeError] Argument 2 passed to XF\Service\Thread\Deleter::__construct() must be an instance of XF\Entity\Thread, instance of XF\Mvc\Entity\ArrayCollection given, called in /home/southbay/public_html/forums200/src/XF/Container.php on line 274 in src/XF/Service/Thread/Deleter.php on line 19


Which code do we need in order to delete a thread in a Cron entry?

Thank you.
 
Thank you, Chris.

FetchOne was indeed the problem, although I changed the code to delete the first post like this:

PHP:
foreach ($postIds AS $k => $v)
{
	$postId = $v['first_post_id'];
	$type = 'hard';
	$reason = '';

	$finder = \XF::finder('XF:Post');
	$post = $finder
		->where('post_id', '=', $postId)
		->fetchOne();

	$deleter = \XF::app()->service('XF:Post\Deleter', $post);

	$deleter->delete($type, $reason);
}
 
Looks like this way also works:

PHP:
foreach ($threads AS $k => $v)
{
	$threadId = $v['thread_id'];
	$type = 'hard';
	$reason = '';

	$finder = \XF::finder('XF:Thread');
	$thread = $finder
		->where('thread_id', '=', $threadId)
		->fetchOne();

	$deleter = \XF::app()->service('XF:Thread\Deleter', $thread);

	$deleter->delete($type, $reason);
}


Is there a preferred way?
 
If you’re trying to delete the thread, use the thread Deleter. If you’re trying to delete a post, use the post deleter.
 
Top Bottom