public function __construct($config = array()) { ....... // Set the default model search path if (array_key_exists('model_path', $config)) { // User-defined dirs $this->addModelPath($config['model_path'], $this->model_prefix); } else { $this->addModelPath($this->basePath . '/models', $this->model_prefix);
public function getModel($name = '', $prefix = '', $config = array()) { if (empty($name)) { $name = $this->getName(); }
if (empty($prefix)) { $prefix = $this->model_prefix; }
if ($model = $this->createModel($name, $prefix, $config)) 调用createModel方法进行类的实例化并返回$model { // Task is a reserved state $model->setState('task', $this->task);
// Let's get the application object and set menu information if it's available $menu = JFactory::getApplication()->getMenu();
if (is_object($menu)) { if ($item = $menu->getActive()) { $params = $menu->getParams($item->id);
// Set default state data $model->setState('parameters.menu', $params); } } }
return $model; }
然后接下来setModel将model Push到view中
1 2 3 4 5 6
// Get/Create the model if ($model = $this->getModel($viewName)) { // Push the model into the view (as default) $view->setModel($model, true); }
// Display the view if ($cachable && $viewType != 'feed' && JFactory::getConfig()->get('caching') >= 1) { $option = $this->input->get('option');
if (is_array($urlparams)) { $app = JFactory::getApplication();
if (!empty($app->registeredurlparams)) { $registeredurlparams = $app->registeredurlparams; } else { $registeredurlparams = new stdClass; }
foreach ($urlparams as $key => $value) { // Add your safe URL parameters with variable type as value {@see JFilterInput::clean()}. $registeredurlparams->$key = $value; }
public function get($property, $default = null) { // If $model is null we use the default model if (is_null($default)) { $model = $this->_defaultModel; } else { $model = strtolower($default); }
// First check to make sure the model requested exists if (isset($this->_models[$model])) { // Model exists, let's build the method name $method = 'get' . ucfirst($property); $property是我们传进的实参也就是'State',那么拼接起来后的方法名就是getState,然后调用这个方法
// Does the method exist? if (method_exists($this->_models[$model], $method)) { // The method exists, let's call it and return what we get $result = $this->_models[$model]->$method();
return $result; } }
// Degrade to JObject::get $result = parent::get($property, $default);
public function getState($property = null, $default = null) { if (!$this->__state_set) { // Protected method to auto-populate the model state. $this->populateState(); 调用populateState函数 // Set the model state set flag to true. $this->__state_set = true; }
public function getItems() { // Get a storage key. $store = $this->getStoreId();
// Try to load the data from internal storage. if (isset($this->cache[$store])) { return $this->cache[$store]; }
try { // Load the list items and add the items to the internal cache. $this->cache[$store] = $this->_getList($this->_getListQuery(), $this->getStart(), $this->getState('list.limit'));调用了一个_getListQuery方法 } catch (RuntimeException $e) { $this->setError($e->getMessage());
return false; }
return $this->cache[$store]; }
跟踪_getListQuery函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
protected function _getListQuery() { // Capture the last store id used. static $lastStoreId;
// Compute the current store id. $currentStoreId = $this->getStoreId();
// If the last store id is different from the current, refresh the query. if ($lastStoreId != $currentStoreId || empty($this->query)) { $lastStoreId = $currentStoreId; $this->query = $this->getListQuery(); }