Skip to content

Commit

Permalink
Use hasAssociation() instead of getAssociation()
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Jan 19, 2018
1 parent 9b7d074 commit 29719a6
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/ORM/Association/BelongsToMany.php
Expand Up @@ -354,14 +354,14 @@ protected function _generateTargetAssociations($junction, $source, $target)
$junctionAlias = $junction->getAlias();
$sAlias = $source->getAlias();

if (!$target->getAssociation($junctionAlias)) {
if (!$target->hasAssociation($junctionAlias)) {
$target->hasMany($junctionAlias, [
'targetTable' => $junction,
'foreignKey' => $this->getTargetForeignKey(),
'strategy' => $this->_strategy,
]);
}
if (!$target->getAssociation($sAlias)) {
if (!$target->hasAssociation($sAlias)) {
$target->belongsToMany($sAlias, [
'sourceTable' => $target,
'targetTable' => $source,
Expand Down Expand Up @@ -391,7 +391,7 @@ protected function _generateTargetAssociations($junction, $source, $target)
protected function _generateSourceAssociations($junction, $source)
{
$junctionAlias = $junction->getAlias();
if (!$source->getAssociation($junctionAlias)) {
if (!$source->hasAssociation($junctionAlias)) {
$source->hasMany($junctionAlias, [
'targetTable' => $junction,
'foreignKey' => $this->getForeignKey(),
Expand Down Expand Up @@ -421,13 +421,13 @@ protected function _generateJunctionAssociations($junction, $source, $target)
$tAlias = $target->getAlias();
$sAlias = $source->getAlias();

if (!$junction->getAssociation($tAlias)) {
if (!$junction->hasAssociation($tAlias)) {
$junction->belongsTo($tAlias, [
'foreignKey' => $this->getTargetForeignKey(),
'targetTable' => $target
]);
}
if (!$junction->getAssociation($sAlias)) {
if (!$junction->hasAssociation($sAlias)) {
$junction->belongsTo($sAlias, [
'foreignKey' => $this->getForeignKey(),
'targetTable' => $source
Expand Down
5 changes: 3 additions & 2 deletions src/ORM/Marshaller.php
Expand Up @@ -88,10 +88,9 @@ protected function _buildPropertyMap($data, $options)
$key = $nested;
$nested = [];
}
$assoc = $this->_table->getAssociation($key);
// If the key is not a special field like _ids or _joinData
// it is a missing association that we should error on.
if (!$assoc) {
if (!$this->_table->hasAssociation($key)) {
if (substr($key, 0, 1) !== '_') {
throw new \InvalidArgumentException(sprintf(
'Cannot marshal data for "%s" association. It is not associated with "%s".',
Expand All @@ -101,6 +100,8 @@ protected function _buildPropertyMap($data, $options)
}
continue;
}
$assoc = $this->_table->getAssociation($key);

if (isset($options['forceNew'])) {
$nested['forceNew'] = $options['forceNew'];
}
Expand Down
4 changes: 2 additions & 2 deletions src/ORM/Query.php
Expand Up @@ -437,10 +437,10 @@ public function clearContain()
protected function _addAssociationsToTypeMap($table, $typeMap, $associations)
{
foreach ($associations as $name => $nested) {
$association = $table->getAssociation($name);
if (!$association) {
if (!$table->hasAssociation($name)) {
continue;
}
$association = $table->getAssociation($name);
$target = $association->getTarget();
$primary = (array)$target->getPrimaryKey();
if (empty($primary) || $typeMap->type($target->aliasField($primary[0])) === null) {
Expand Down

0 comments on commit 29719a6

Please sign in to comment.