Skip to content

Commit

Permalink
Add classes and interfaces for associations in other datasources
Browse files Browse the repository at this point in the history
  • Loading branch information
Marlinc committed Aug 8, 2016
1 parent e65f13f commit 9f526ac
Show file tree
Hide file tree
Showing 14 changed files with 762 additions and 67 deletions.
Expand Up @@ -12,7 +12,7 @@
* @since 3.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\ORM;
namespace Cake\Datasource;

use ArrayIterator;
use Cake\Datasource\EntityInterface;
Expand All @@ -33,7 +33,7 @@ class AssociationCollection implements IteratorAggregate
/**
* Stored associations
*
* @var array
* @var \Cake\Datasource\AssociationInface[]
*/
protected $_items = [];

Expand All @@ -47,10 +47,9 @@ class AssociationCollection implements IteratorAggregate
* @param \Cake\ORM\Association $association The association to add.
* @return \Cake\ORM\Association The association object being added.
*/
public function add($alias, Association $association)
public function add($alias, AssociationInterface $association)
{
list(, $alias) = pluginSplit($alias);

return $this->_items[strtolower($alias)] = $association;
}

Expand All @@ -66,7 +65,6 @@ public function get($alias)
if (isset($this->_items[$alias])) {
return $this->_items[$alias];
}

return null;
}

Expand All @@ -83,7 +81,6 @@ public function getByProperty($prop)
return $assoc;
}
}

return null;
}

Expand Down Expand Up @@ -117,14 +114,12 @@ public function keys()
*/
public function type($class)
{
$class = array_map('strtolower', (array)$class);
$class = (array)$class;

$out = array_filter($this->_items, function ($assoc) use ($class) {
list(, $name) = namespaceSplit(get_class($assoc));

return in_array(strtolower($name), $class, true);
return in_array($name, $class, true);
});

return array_values($out);
}

Expand Down Expand Up @@ -161,19 +156,18 @@ public function removeAll()
* Parent associations include any association where the given table
* is the owning side.
*
* @param \Cake\ORM\Table $table The table entity is for.
* @param \Cake\Datasource\RepositoryInterface $table The table entity is for.
* @param \Cake\Datasource\EntityInterface $entity The entity to save associated data for.
* @param array $associations The list of associations to save parents from.
* associations not in this list will not be saved.
* @param array $options The options for the save operation.
* @return bool Success
*/
public function saveParents(Table $table, EntityInterface $entity, $associations, array $options = [])
public function saveParents(RepositoryInterface $table, EntityInterface $entity, $associations, array $options = [])
{
if (empty($associations)) {
return true;
}

return $this->_saveAssociations($table, $entity, $associations, $options, false);
}

Expand All @@ -183,26 +177,25 @@ public function saveParents(Table $table, EntityInterface $entity, $associations
* Child associations include any association where the given table
* is not the owning side.
*
* @param \Cake\ORM\Table $table The table entity is for.
* @param \Cake\Datasource\RepositoryInterface $table The table entity is for.
* @param \Cake\Datasource\EntityInterface $entity The entity to save associated data for.
* @param array $associations The list of associations to save children from.
* associations not in this list will not be saved.
* @param array $options The options for the save operation.
* @return bool Success
*/
public function saveChildren(Table $table, EntityInterface $entity, array $associations, array $options)
public function saveChildren(RepositoryInterface $table, EntityInterface $entity, array $associations, array $options)
{
if (empty($associations)) {
return true;
}

return $this->_saveAssociations($table, $entity, $associations, $options, true);
}

/**
* Helper method for saving an association's data.
*
* @param \Cake\ORM\Table $table The table the save is currently operating on
* @param \Cake\Datasource\RepositoryInterface $table The table the save is currently operating on
* @param \Cake\Datasource\EntityInterface $entity The entity to save
* @param array $associations Array of associations to save.
* @param array $options Original options
Expand Down Expand Up @@ -235,7 +228,6 @@ protected function _saveAssociations($table, $entity, $associations, $options, $
return false;
}
}

return true;
}

Expand All @@ -256,7 +248,6 @@ protected function _save($association, $entity, $nested, $options)
if (!empty($nested)) {
$options = (array)$nested + $options;
}

return (bool)$association->saveAssociated($entity, $options);
}

Expand Down

0 comments on commit 9f526ac

Please sign in to comment.