Skip to content

Commit

Permalink
Make load model more precise
Browse files Browse the repository at this point in the history
If it's a plugin model, use the model alias to check for an existing
property

I.e. prevent this:

    $this->loadModel('Foo.Comments');
    $this->loadModel('Bar.Comments');  // Will overwrite $this->Comments
    $this->loadModel('Comments');  // Will NOT overwrite $this->Comments

If a user were to use referenes to multiple models with the same alias,
the first one should always be returned.
  • Loading branch information
AD7six committed Feb 10, 2015
1 parent b86b7e1 commit 8a6e2e1
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions src/Datasource/ModelAwareTrait.php
Expand Up @@ -83,11 +83,11 @@ public function loadModel($modelClass = null, $type = 'Table')
$modelClass = $this->modelClass;
}

if (isset($this->{$modelClass})) {
return $this->{$modelClass};
}
list($plugin, $alias) = pluginSplit($modelClass, true);

list($plugin, $modelClass) = pluginSplit($modelClass, true);
if (isset($this->{$alias})) {
return $this->{$alias};
}

if (!isset($this->_modelFactories[$type])) {
throw new InvalidArgumentException(sprintf(
Expand All @@ -96,11 +96,11 @@ public function loadModel($modelClass = null, $type = 'Table')
));
}
$factory = $this->_modelFactories[$type];
$this->{$modelClass} = $factory($plugin . $modelClass);
if (!$this->{$modelClass}) {
$this->{$alias} = $factory($modelClass);
if (!$this->{$alias}) {
throw new MissingModelException([$modelClass, $type]);
}
return $this->{$modelClass};
return $this->{$alias};
}

/**
Expand Down

0 comments on commit 8a6e2e1

Please sign in to comment.