modelClass)) { $this->modelClass = $name; } } /** * Loads and constructs repository objects required by this object * * Typically used to load ORM Table objects as required. Can * also be used to load other types of repository objects your application uses. * * If a repository provider does not return an object a MissingModelException will * be thrown. * * @param string $modelClass Name of model class to load. Defaults to $this->modelClass * @param string $type The type of repository to load. Defaults to 'Table' which * delegates to Cake\ORM\TableRegistry. * @return bool True when single repository found and instance created. * @throws \Cake\Model\Error\MissingModelException If the model class cannot be found. * @throws \Cake\Error\Exception When using a type that has not been registered. */ public function loadModel($modelClass = null, $type = 'Table') { if ($modelClass === null) { $modelClass = $this->modelClass; } if (isset($this->{$modelClass})) { return true; } list($plugin, $modelClass) = pluginSplit($modelClass, true); if (!isset($this->_modelFactories[$type])) { throw new Exception(sprintf( 'Unknown repository type "%s". Make sure you register a type before trying to use it.', $type )); } $factory = $this->_modelFactories[$type]; $this->{$modelClass} = $factory($plugin . $modelClass); if (!$this->{$modelClass}) { throw new Error\MissingModelException([$modelClass, $type]); } return true; } /** * Register a callable to generate repositories of a given type. * * @param string $type The name of the repository type the factory function is for. * @param callable $factory The factory function used to create instances. * @return void */ public function modelFactory($type, callable $factory) { $this->_modelFactories[$type] = $factory; } }