Skip to content
Permalink
Browse files

Add the ability to set the default model type

  • Loading branch information...
Marlinc committed Oct 20, 2015
1 parent 9f90a04 commit 2fd7ac50139d8ab969ca8a9bc15ad2717f1b1dd6
Showing with 65 additions and 7 deletions.
  1. +40 −7 src/Datasource/ModelAwareTrait.php
  2. +25 −0 tests/TestCase/Datasource/ModelAwareTraitTest.php
@@ -16,6 +16,7 @@
use Cake\Datasource\Exception\MissingModelException;
use InvalidArgumentException;
use UnexpectedValueException;
/**
* Provides functionality for loading table classes
@@ -46,6 +47,13 @@ trait ModelAwareTrait
*/
protected $_modelFactories = [];
/**
* The model type to use.
*
* @var string
*/
protected $_modelType = 'Table';
/**
* Set the modelClass and modelKey properties based on conventions.
*
@@ -71,34 +79,41 @@ protected function _setModelClass($name)
* be thrown.
*
* @param string|null $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.
* @param string|null $modelType The type of repository to load. Defaults to the modelType() value.
* @return object The model instance created.
* @throws \Cake\Datasource\Exception\MissingModelException If the model class cannot be found.
* @throws \InvalidArgumentException When using a type that has not been registered.
* @throws \UnexpectedValueException If no model type has been defined
*/
public function loadModel($modelClass = null, $type = 'Table')
public function loadModel($modelClass = null, $modelType = null)
{
if ($modelClass === null) {
$modelClass = $this->modelClass;
}
if ($modelType === null) {
$modelType = $this->modelType();
if ($modelType === null) {
throw new UnexpectedValueException('No model type has been defined');
}
}
list(, $alias) = pluginSplit($modelClass, true);
if (isset($this->{$alias})) {
return $this->{$alias};
}
if (!isset($this->_modelFactories[$type])) {
if (!isset($this->_modelFactories[$modelType])) {
throw new InvalidArgumentException(sprintf(
'Unknown repository type "%s". Make sure you register a type before trying to use it.',
$type
$modelType
));
}
$factory = $this->_modelFactories[$type];
$factory = $this->_modelFactories[$modelType];
$this->{$alias} = $factory($modelClass);
if (!$this->{$alias}) {
throw new MissingModelException([$modelClass, $type]);
throw new MissingModelException([$modelClass, $modelType]);
}
return $this->{$alias};
}
@@ -114,4 +129,22 @@ public function modelFactory($type, callable $factory)
{
$this->_modelFactories[$type] = $factory;
}
/**
* Set or get the model type to be used by this class
*
* @param string|null $modelType The model type or null to retrieve the current
*
* @return string|$this
*/
public function modelType($modelType = null)
{
if ($modelType === null) {
return $this->_modelType;
}
$this->_modelType = $modelType;
return $this;
}
}
@@ -60,6 +60,7 @@ public function testLoadModel()
$stub = new Stub();
$stub->setProps('Articles');
$stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
$stub->modelType('Table');
$result = $stub->loadModel();
$this->assertInstanceOf('Cake\ORM\Table', $result);
@@ -83,6 +84,7 @@ public function testLoadModelPlugin()
$stub = new Stub();
$stub->setProps('Articles');
$stub->modelFactory('Table', ['\Cake\ORM\TableRegistry', 'get']);
$stub->modelType('Table');
$result = $stub->loadModel('TestPlugin.Comments');
$this->assertInstanceOf('TestPlugin\Model\Table\CommentsTable', $result);
@@ -115,6 +117,29 @@ public function testModelFactory()
$this->assertEquals('Magic', $stub->Magic->name);
}
/**
* test alternate default model type.
*
* @return void
*/
public function testModelType()
{
$stub = new Stub();
$stub->setProps('Articles');
$stub->modelFactory('Test', function ($name) {
$mock = new \StdClass();
$mock->name = $name;
return $mock;
});
$stub->modelType('Test');
$result = $stub->loadModel('Magic');
$this->assertInstanceOf('\StdClass', $result);
$this->assertInstanceOf('\StdClass', $stub->Magic);
$this->assertEquals('Magic', $stub->Magic->name);
}
/**
* test MissingModelException being thrown
*

0 comments on commit 2fd7ac5

Please sign in to comment.
You can’t perform that action at this time.