Skip to content
Permalink
Browse files

Make sure the target table is an instance of a correct class.

  • Loading branch information...
robertpustulka committed Feb 2, 2017
1 parent 7671cff commit 17ef6fd96dd5988db0e190d51d36ae5726c0a86e
Showing with 26 additions and 0 deletions.
  1. +26 −0 src/ORM/Association.php
@@ -15,6 +15,7 @@
namespace Cake\ORM;
use Cake\Collection\Collection;
use Cake\Core\App;
use Cake\Core\ConventionsTrait;
use Cake\Database\Expression\IdentifierExpression;
use Cake\Datasource\EntityInterface;
@@ -400,6 +401,15 @@ public function getTarget()
$config = ['className' => $this->_className];
}
$this->_targetTable = $tableLocator->get($registryAlias, $config);
$className = $this->_getClassName($registryAlias, ['className' => $this->_className]);
if (!is_a($this->_targetTable, $className)) {
throw new RuntimeException(sprintf(
'Invalid Table retrieved from a registry. Requested: %s, got: %s',
get_class($this->_targetTable),
$className
));
}
}
return $this->_targetTable;
@@ -1278,6 +1288,22 @@ protected function _extractFinder($finderData)
return [key($finderData), current($finderData)];
}
/**
* Gets the table class name.
*
* @param string $alias The alias name you want to get.
* @param array $options Table options array.
* @return string
*/
protected function _getClassName($alias, array $options = [])
{
if (empty($options['className'])) {
$options['className'] = Inflector::camelize($alias);
}
return App::className($options['className'], 'Model/Table', 'Table') ?: 'Cake\ORM\Table';
}
/**
* Proxies property retrieval to the target table. This is handy for getting this
* association's associations

0 comments on commit 17ef6fd

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