Getting data from the Model

Another solution
PHP:
class Andy_ShowDeleted_Model_ShowDeleted extends XenForo_Model{
You just extend the regular model to have access to some of its functions. In this case, you do not need to create a listener. You just do this:
PHP:
$posts = $this->getModelFromCache('Andy_ShowDeleted_Model_ShowDeleted')->getDeletedPosts();
 
Okay! Your solution with the listener is okay, you just need to rename the class to
class Andy_ShowDeleted_Model_ShowDeleted extends
PHP:
XFCP_Andy_ShowDeleted_Model_ShowDeleted
 
You can work with extending XenForo_Model_Post to have specific access to its functions. Or you can work with just extending XenForo_Model (so not the post model, too). Then you have basic access to mysql functions with is ok for your addon.

As you already setup the listener, just go with it. It is more beautiful I think.
 
@Jeremy I am a bit uncertain here if there is a need to have a listener for the model, or if one can just extend XenForo_Model as in #21
 
For a proper add-on, you should never extend a class directly and should use the Proxy System whenever possible.
 
I assume this is correct.

library/Andy/ShowDeleted/Model/ShowDeleted.php

PHP:
<?php

class Andy_ShowDeleted_Model_ShowDeleted extends XFCP_Andy_ShowDeleted_Model_ShowDeleted
{
   public function getDeletedPosts()
   {
     return $this->_getDb()->fetchAll('
     SELECT *
     FROM xf_post
     WHERE message_state = "deleted"
     ORDER BY post_id DESC
     LIMIT 50');
   }
}

?>
 
library/Andy/ShowDeleted/ControllerPublic/ShowDeleted.php

PHP:
<?php

class Andy_ShowDeleted_ControllerPublic_ShowDeleted extends XenForo_ControllerPublic_Abstract
{
	public function actionIndex()
	{
		// if you are not a super admin, return
		if (!XenForo_Visitor::getInstance()->isSuperAdmin())
		{
			return;
		}
				
		//########################################
		// get data
		
		// get deleted post_id's
        $posts = $this->getModelFromCache('Andy_ShowDeleted_Model_ShowDeleted')->getDeletedPosts();	
		
		// get deleted thread_id's
		$threads = $this->getModelFromCache('XenForo_Model_Thread')->getThreads(
			array('discussion_state' => 'deleted', 
			array('order' => 'thread_id', 
			'orderDirection' => 'DESC',
			array('limit' => 50,)))
		);
				
		//########################################
		// prepare output		
		
		$viewParams = array('posts' => $posts,'threads' => $threads,
		); 
		
		return $this->responseView('Andy_ShowDeleted_ViewPublic_ShowDeleted', 'andy_showdeleted', $viewParams);
	}
}

?>
 
Maybe you can do this:

PHP:
$posts = $this->getModelFromCache('XenForo_Model_Post')->getDeletedPosts();
 
Hi Jeremy,

So If I understand correctly the first thing I need to do is change the file here:

library
--Andy
----ShowDeleted
------ControllerPublic
--------ShowDeleted.php
------Model
--------Post.php
----Route
------Prefix
--------ShowDeleted.php
 
Got it! It now works perfect.

Thank you, Jeremy, Marcus and Daniel. You guys are the best.
 
Directory structure:

library
--Andy
----ShowDeleted
------ControllerPublic
--------ShowDeleted.php
----Listener.php
----Model
------Post.php
----Route
------Prefix
--------ShowDeleted.php
 
Listener.php

PHP:
<?php

class Andy_ShowDeleted_Listener
{
   public static function loadClassModel($class, array &$extend)
   {
      $extend[] = 'Andy_ShowDeleted_Model_Post';
   }
}
   
?>
 
library/Andy/ShowDeleted/ControllerPublic/ShowDeleted.php

PHP:
<?php

class Andy_ShowDeleted_ControllerPublic_ShowDeleted extends XenForo_ControllerPublic_Abstract
{
	public function actionIndex()
	{
		// if you are not a super admin, return
		if (!XenForo_Visitor::getInstance()->isSuperAdmin())
		{
			return;
		}
				
		//########################################
		// get data
		
		// get deleted post_id's - this required extening the model post
        $posts = $this->getModelFromCache('XenForo_Model_Post')->getDeletedPosts();	
		
		// get deleted thread_id's - this uses existing Xenforo code
		$threads = $this->getModelFromCache('XenForo_Model_Thread')->getThreads(
			array('discussion_state' => 'deleted', 
			array('order' => 'thread_id', 
			'orderDirection' => 'DESC',
			array('limit' => 50,)))
		);
				
		//########################################
		// display data		
		
		// $viewParams must be an array
		$viewParams = array('posts' => $posts,'threads' => $threads,
		); 
		
		// send to template for display
		return $this->responseView('Andy_ShowDeleted_ViewPublic_ShowDeleted', 'andy_showdeleted', $viewParams);
	}
}

?>
 
Last edited:
You can name your class whatever you want, but its general convention to use similar names (Andy_Model_Post, XenForo_Model_Post, etc.) to associate models together and make reading your code easier. However, the proxy system will understand that Andy_ShowDeleted_Model_ShowDeleted is extending XenForo_Model_Post and perform the extensions as necessary.
 
Post.php

PHP:
<?php

class Andy_ShowDeleted_Model_Post extends XFCP_Andy_ShowDeleted_Model_Post
{
   public function getDeletedPosts()
   {
     return $this->_getDb()->fetchAll('
     SELECT *
     FROM xf_post
     WHERE message_state = "deleted"
     ORDER BY post_id DESC
     LIMIT 50');
   }
}

?>
 
Thanks again everyone.

I'll create a new thread in the tutorial forum with each step needed to create this add-on
 
Back
Top Bottom