public getModel ( string $name = null, array $config = [] ) : |
||
$name | string | The Model name. If null we'll use the modelName variable or, if it's empty, the same name as the Controller |
$config | array | Configuration parameters to the Model. If skipped we will use $this->config |
return | The instance of the Model known to this Controller |
public function getModel($name = null, $config = array())
{
if (!empty($name)) {
$modelName = $name;
} elseif (!empty($this->modelName)) {
$modelName = $this->modelName;
} else {
$modelName = $this->view;
}
if (!array_key_exists($modelName, $this->modelInstances)) {
if (empty($config) && isset($this->config['modelConfig'])) {
$config = $this->config['modelConfig'];
}
if (empty($name)) {
$config['modelTemporaryInstance'] = true;
} else {
// Other classes are loaded with persistent state disabled and their state/input blanked out
$config['modelTemporaryInstance'] = false;
$config['modelClearState'] = true;
$config['modelClearInput'] = true;
}
$this->modelInstances[$modelName] = $this->container->factory->model(ucfirst($modelName), $config);
}
return $this->modelInstances[$modelName];
}
/** * Returns a named Model object. Makes sure that the Model is a database-aware model, throwing an exception * otherwise, when $name is null. * * @param string $name The Model name. If null we'll use the modelName * variable or, if it's empty, the same name as * the Controller * @param array $config Configuration parameters to the Model. If skipped * we will use $this->config * * @return DataModel The instance of the Model known to this Controller * * @throws NotADataModel When the model type doesn't match our expectations */ public function getModel($name = null, $config = array()) { $model = parent::getModel($name, $config); if (is_null($name) && !$model instanceof DataModel) { throw new NotADataModel('Model ' . get_class($model) . ' is not a database-aware Model'); } return $model; }