/** * Returns a named Model object * * @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 Model The instance of the Model known to this Controller */ public function getModel($name = null, $config = array()) { if (!empty($name)) { $modelName = strtolower($name); } elseif (!empty($this->modelName)) { $modelName = strtolower($this->modelName); } else { $modelName = strtolower($this->view); } if (!array_key_exists($modelName, $this->modelInstances)) { $appName = $this->container->application->getName(); if (empty($config)) { $config = $this->config; } 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->container['mvc_config'] = $config; $this->modelInstances[$modelName] = Model::getInstance($appName, $modelName, $this->container); } return $this->modelInstances[$modelName]; }
/** * Pushes the default Model to the View * * @param Model $model The model to push */ public function setDefaultModel(Model &$model) { $name = $model->getName(); $this->setDefaultModelName($name); $this->setModel($this->defaultModel, $model); }
/** * Public constructor. Overrides the parent constructor, adding support for database-aware models. * * You can use the $container['mvc_config'] array to pass some configuration values to the object: * * tableName String. The name of the database table to use. Default: #__appName_viewNamePlural (Ruby on Rails convention) * idFieldName String. The table key field name. Default: appName_viewNameSingular_id (Ruby on Rails convention) * knownFields Array. The known fields in the table. Default: read from the table itself * autoChecks Boolean. Should I turn on automatic data validation checks? * fieldsSkipChecks Array. List of fields which should not participate in automatic data validation checks. * aliasFields Array. Associative array of "magic" field aliases. * behavioursDispatcher EventDispatcher. The model behaviours event dispatcher. * behaviourObservers Array. The model behaviour observers to attach to the behavioursDispatcher. * behaviours Array. A list of behaviour names to instantiate and attach to the behavioursDispatcher. * fillable_fields Array. Which fields should be auto-filled from the model state (by extent, the request)? * guarded_fields Array. Which fields should never be auto-filled from the model state (by extent, the request)? * relations Array (hashed). The relations to autoload on model creation. * * Setting either fillable_fields or guarded_fields turns on automatic filling of fields in the constructor. If both * are set only guarded_fields is taken into account. Fields are not filled automatically outside the constructor. * * @see \Awf\Mvc\Model::__construct() * * @param Container $container */ public function __construct(\Awf\Container\Container $container = null) { if (!is_object($container)) { $container = Application::getInstance()->getContainer(); } // First call the parent constructor. It also populates $this->config from $container['mvc_config'] parent::__construct($container); // Should I use a different database object? $this->dbo = $container->db; // Do I have a table name? if (isset($this->config['tableName'])) { $this->tableName = $this->config['tableName']; } elseif (empty($this->tableName)) { // The table name is by default: #__appName_viewNamePlural (Ruby on Rails convention) $viewPlural = Inflector::pluralize($this->getName()); $this->tableName = '#__' . strtolower($this->container->application->getName()) . '_' . strtolower($viewPlural); } // Do I have a table key name? if (isset($this->config['idFieldName'])) { $this->idFieldName = $this->config['idFieldName']; } elseif (empty($this->idFieldName)) { // The default ID field is: appName_viewNameSingular_id (Ruby on Rails convention) $viewSingular = Inflector::singularize($this->getName()); $this->idFieldName = strtolower($this->container->application->getName()) . '_' . strtolower($viewSingular) . '_id'; } // Do I have a list of known fields? if (isset($this->config['knownFields'])) { $this->knownFields = $this->config['knownFields']; } else { // By default the known fields are fetched from the table itself (slow!) $this->knownFields = $this->getTableFields(); } if (empty($this->knownFields)) { throw new NoTableColumns(sprintf('Model %s could not fetch column list for the table %s', $this->getName(), $this->tableName)); } // Should I turn on autoChecks? if (isset($this->config['autoChecks'])) { $this->autoChecks = $this->config['autoChecks']; } // Should I exempt fields from autoChecks? if (isset($this->config['fieldsSkipChecks'])) { $this->fieldsSkipChecks = $this->config['fieldsSkipChecks']; } // Do I have alias fields? if (isset($this->config['aliasFields'])) { $this->aliasFields = $this->config['aliasFields']; } // Do I have a behaviours dispatcher? if (isset($this->config['behavioursDispatcher']) && $this->config['behavioursDispatcher'] instanceof EventDispatcher) { $this->behavioursDispatcher = $this->config['behavioursDispatcher']; } else { $this->behavioursDispatcher = new EventDispatcher($this->container); } // Do I have an array of behaviour observers if (isset($this->config['behaviourObservers']) && is_array($this->config['behaviourObservers'])) { foreach ($this->config['behaviourObservers'] as $observer) { $this->behavioursDispatcher->attach($observer); } } // Do I have a list of behaviours? if (isset($this->config['behaviours']) && is_array($this->config['behaviours'])) { foreach ($this->config['behaviours'] as $behaviour) { $this->addBehaviour($behaviour); } } // Do I have a list of fillable fields? if (isset($this->config['fillable_fields']) && is_array($this->config['fillable_fields'])) { $this->fillable = array(); $this->autoFill = true; foreach ($this->config['fillable_fields'] as $field) { if (array_key_exists($field, $this->knownFields)) { $this->fillable[] = $field; } elseif (isset($this->aliasFields[$field])) { $this->fillable[] = $this->aliasFields[$field]; } } } // Do I have a list of guarded fields? if (isset($this->config['guarded_fields']) && is_array($this->config['guarded_fields'])) { $this->guarded = array(); $this->autoFill = true; foreach ($this->config['guarded_fields'] as $field) { if (array_key_exists($field, $this->knownFields)) { $this->guarded[] = $field; } elseif (isset($this->aliasFields[$field])) { $this->guarded[] = $this->aliasFields[$field]; } } } // Do I have to auto-fill the fields? if ($this->autoFill) { // If I have guarded fields, I'll try to fill everything, using such fields as a "blacklist" if (!empty($this->guarded)) { $fields = array_keys($this->knownFields); } else { // Otherwise I'll fill only the fillable ones (act like having a "whitelist") $fields = $this->fillable; } foreach ($fields as $field) { if (in_array($field, $this->guarded)) { // Do not set guarded fields continue; } $stateValue = $this->getState($field, null); if (!is_null($stateValue)) { $this->setFieldValue($field, $stateValue); } } } // Create a relation manager $this->relationManager = new RelationManager($this); // Do I have a list of relations? if (isset($this->config['relations']) && is_array($this->config['relations'])) { foreach ($this->config['relations'] as $name => $relConfig) { if (!is_array($relConfig)) { continue; } $defaultRelConfig = array('type' => 'hasOne', 'foreignModelClass' => null, 'localKey' => null, 'foreignKey' => null, 'pivotTable' => null, 'pivotLocalKey' => null, 'pivotForeignKey' => null); $relConfig = array_merge($defaultRelConfig, $relConfig); $this->relationManager->addRelation($name, $relConfig['type'], $relConfig['foreignModelClass'], $relConfig['localKey'], $relConfig['foreignKey'], $relConfig['pivotTable'], $relConfig['pivotLocalKey'], $relConfig['pivotForeignKey']); } } // Initialise the data model foreach ($this->knownFields as $fieldName => $information) { // Initialize only the null or not yet set records if (!isset($this->recordData[$fieldName])) { $this->recordData[$fieldName] = $information->Default; } } }