You can use the $config array to pass some configuration values to the object:
state stdClass|array. The state variables of the Model.
use_populate Boolean. When true the model will set its state from populateState() instead of the request.
ignore_request Boolean. When true getState will not automatically load state data from the request.
public __construct ( |
||
$container | The configuration variables to this model | |
$config | array | Configuration values for this model |
/** * Assigns callback functions to the class, the $methods array should be an associative one, where * the keys are the method names, while the values are the closure functions, e.g. * * array( * 'foobar' => function(){ return 'Foobar'; } * ) * * @param $container * @param array $config * @param array $methods */ public function __construct(Container $container, array $config = array(), array $methods = array()) { foreach ($methods as $method => $function) { $this->methods[$method] = $function; } parent::__construct($container, $config); }
/** * Public constructor. Initialises the protected members as well. Useful $config keys: * update_component The component name, e.g. com_foobar * update_version The default version if the manifest cache is unreadable * update_site The URL to the component's update XML stream * update_extraquery The extra query to append to (commercial) components' download URLs * update_sitename The update site's name (description) * * @param array $config */ public function __construct($config = array()) { $container = Container::getInstance('com_FOOBAR'); parent::__construct($container); // Get an instance of the updater class $this->updater = JUpdater::getInstance(); // Get the component name if (isset($config['update_component'])) { $this->component = $config['update_component']; } else { $this->component = $this->input->getCmd('option', ''); } // Get the component version if (isset($config['update_version'])) { $this->version = $config['update_version']; } // Get the update site if (isset($config['update_site'])) { $this->updateSite = $config['update_site']; } // Get the extra query if (isset($config['update_extraquery'])) { $this->extraQuery = $config['update_extraquery']; } // Get the extra query if (isset($config['update_sitename'])) { $this->updateSiteName = $config['update_sitename']; } // Get the extension type list($extensionPrefix, $extensionName) = explode('_', $this->component); switch ($extensionPrefix) { default: case 'com': $type = 'component'; $name = $this->component; break; case 'pkg': $type = 'package'; $name = $this->component; break; } // Find the extension ID $db = $this->container->db; $query = $db->getQuery(true)->select('*')->from($db->qn('#__extensions'))->where($db->qn('type') . ' = ' . $db->q($type))->where($db->qn('element') . ' = ' . $db->q($name)); $db->setQuery($query); $extension = $db->loadObject(); if (is_object($extension)) { $this->extension_id = $extension->extension_id; $data = json_decode($extension->manifest_cache, true); if (isset($data['version'])) { $this->version = $data['version']; } } }
/** * Public constructor. * * @param Container $container The configuration variables to this model * @param array $config Configuration values for this model */ public function __construct(Container $container, array $config) { $defaultConfig = ['tableName' => '#__ak_stats', 'idFieldName' => 'id']; if (!is_array($config) || empty($config)) { $config = []; } $config = array_merge($defaultConfig, $config); parent::__construct($container, $config); $platform = $this->container->platform; $defaultLimit = $platform->getConfig()->get('list_limit', 10); $limit = $platform->getUserStateFromRequest('global.list.limit', 'limit', $this->input, $defaultLimit); $limitstart = $platform->getUserStateFromRequest('com_akeeba.stats.limitstart', 'limitstart', $this->input, 0); if ($platform->isFrontend()) { $limit = 0; $limitstart = 0; } // Set the page pagination variables $this->setState('limit', $limit); $this->setState('limitstart', $limitstart); }
public function __construct(Container $container, array $config) { parent::__construct($container, $config); $this->knownFilterTypes = ['directories', 'files', 'skipdirs', 'skipfiles']; }
/** * Public constructor. Overrides the parent constructor, adding support for database-aware models. * * You can use the $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. * contentType String The UCM content type, e.g. "com_foobar.items" * * 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 Model::__construct() * * @param Container $container The configuration variables to this model * @param array $config Configuration values for this model * * @throws \FOF30\Model\DataModel\Exception\NoTableColumns */ public function __construct(Container $container, array $config = array()) { // First call the parent constructor. parent::__construct($container, $config); // Should I use a different database object? $this->dbo = $container->db; // Do I have a table name? if (isset($config['tableName'])) { $this->tableName = $config['tableName']; } elseif (empty($this->tableName)) { // The table name is by default: #__appName_viewNamePlural (Ruby on Rails convention) $viewPlural = $container->inflector->pluralize($this->getName()); $this->tableName = '#__' . strtolower($this->container->bareComponentName) . '_' . strtolower($viewPlural); } // Do I have a table key name? if (isset($config['idFieldName'])) { $this->idFieldName = $config['idFieldName']; } elseif (empty($this->idFieldName)) { // The default ID field is: appName_viewNameSingular_id (Ruby on Rails convention) $viewSingular = $container->inflector->singularize($this->getName()); $this->idFieldName = strtolower($this->container->bareComponentName) . '_' . strtolower($viewSingular) . '_id'; } // Do I have a list of known fields? if (isset($config['knownFields']) && !empty($config['knownFields'])) { if (!is_array($config['knownFields'])) { $config['knownFields'] = explode(',', $config['knownFields']); } $this->knownFields = $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($config['autoChecks'])) { if (!is_bool($config['autoChecks'])) { $config['autoChecks'] = strtolower($config['autoChecks']); $config['autoChecks'] = in_array($config['autoChecks'], array('yes', 'true', 'on', 1)); } $this->autoChecks = $config['autoChecks']; } // Should I exempt fields from autoChecks? if (isset($config['fieldsSkipChecks'])) { if (!is_array($config['fieldsSkipChecks'])) { $config['fieldsSkipChecks'] = explode(',', $config['fieldsSkipChecks']); $config['fieldsSkipChecks'] = array_map(function ($x) { return trim($x); }, $config['fieldsSkipChecks']); } $this->fieldsSkipChecks = $config['fieldsSkipChecks']; } // Do I have alias fields? if (isset($config['aliasFields'])) { $this->aliasFields = $config['aliasFields']; } // Do I have a behaviours dispatcher? if (isset($config['behavioursDispatcher']) && $config['behavioursDispatcher'] instanceof Dispatcher) { $this->behavioursDispatcher = $config['behavioursDispatcher']; } else { $this->behavioursDispatcher = new Dispatcher($this->container); } // Do I have an array of behaviour observers if (isset($config['behaviourObservers']) && is_array($config['behaviourObservers'])) { foreach ($config['behaviourObservers'] as $observer) { $this->behavioursDispatcher->attach($observer); } } // Do I have a list of behaviours? if (isset($config['behaviours']) && is_array($config['behaviours'])) { foreach ($config['behaviours'] as $behaviour) { $this->addBehaviour($behaviour); } } // Add extra behaviours foreach (array('Created', 'Modified') as $behaviour) { $this->addBehaviour($behaviour); } // Do I have a list of fillable fields? if (isset($config['fillable_fields']) && !empty($config['fillable_fields'])) { if (!is_array($config['fillable_fields'])) { $config['fillable_fields'] = explode(',', $config['fillable_fields']); $config['fillable_fields'] = array_map(function ($x) { return trim($x); }, $config['fillable_fields']); } $this->fillable = array(); $this->autoFill = true; foreach ($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($config['guarded_fields']) && !empty($config['guarded_fields'])) { if (!is_array($config['guarded_fields'])) { $config['guarded_fields'] = explode(',', $config['guarded_fields']); $config['guarded_fields'] = array_map(function ($x) { return trim($x); }, $config['guarded_fields']); } $this->guarded = array(); $this->autoFill = true; foreach ($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]; } } } // If we are tracking assets, make sure an access field exists and initially set the default. $asset_id_field = $this->getFieldAlias('asset_id'); $access_field = $this->getFieldAlias('access'); if (in_array($asset_id_field, $this->knownFields)) { \JLoader::import('joomla.access.rules'); $this->_trackAssets = true; } if (in_array($access_field, $this->knownFields)) { $this->{$access_field} = (int) $this->container->platform->getConfig()->get('access'); } $assetKey = $this->container->componentName . '.' . strtolower($container->inflector->singularize($this->getName())); $this->setAssetKey($assetKey); // Set the UCM content type if applicable if (isset($config['contentType'])) { $this->contentType = $config['contentType']; } // 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($config['relations']) && is_array($config['relations'])) { foreach ($config['relations'] as $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($relConfig['itemName'], $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; } } // Trigger the onAfterConstruct event. This allows you to set up model state etc. $this->triggerEvent('onAfterConstruct'); }
/** * Constructs the model. Also sets the protected $integration property to the object of the active integration (or * null if none is available). * * @param Container $container The component's DI container * @param array $config Configuration overrides */ public function __construct(Container $container, array $config = array()) { parent::__construct($container, $config); $this->integration = $this->getIntegration(); }
/** * Overridden constructor * * Sets up the encapsulation. * * @param Container $container The configuration variables to this model * @param array $config Configuration values for this model */ public function __construct(Container $container, array $config) { parent::__construct($container, $config); $this->encapsulation = new Encapsulation($this->serverKey()); }
public function __construct(Container $container, array $config) { parent::__construct($container, $config); $this->knownFilterTypes = ['tables', 'tabledata']; }