• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

What is the difference between these 2 methods?

wang

Well-known member
#1
Hello,

In some places, like cron files for example, I see that the model is calld upon with this method.

PHP:
XenForo_Model::create('XenForo_Model_Thread')->functionName();
In some other places, like controllers, I see that the model is called upon with this method.

PHP:
$threadModel = $this->_getThreadModel();
$threadModel->functionName()

/**
    * @return XenForo_Model_Thread
    */
    protected function _getThreadModel()
    {
        return $this->getModelFromCache('XenForo_Model_Thread');
    }
When I use the method used in cron in controllers it works, but the other way around it does not work. Can someone explain to me the difference between these 2 methods?
 

batpool52!

Well-known member
#2
1st Method = Create a new class and use it
2nd Method = Check if the method was used previous functions and if it was the model that existed back

Also you need to check if the parent class if extending has getModelFromCache function and if not you create it using this code
PHP:
/**
* Standard approach to caching model objects for the lifetime of the data writer.
* This is now a static cache to allow data to be reused between data writers, as they
* are often used in bulk.
*
* @var array
*/
protected static $_modelCache = array();

   /**
     * Gets the specified model object from the cache. If it does not exist,
     * it will be instantiated.
     *
     * @param string $class Name of the class to load
     *
     * @return XenForo_Model
     */
    public function getModelFromCache($class)
    {
        if (!isset(self::$_modelCache[$class]))
        {
            self::$_modelCache[$class] = XenForo_Model::create($class);
        }

        return self::$_modelCache[$class];
    }
but in most use cases you don't need that.