Skip to content

Commit

Permalink
Initial implementation of Association::bindingKey()
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Jul 1, 2015
1 parent 29ca3d1 commit b6f0b88
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 15 deletions.
32 changes: 32 additions & 0 deletions src/ORM/Association.php
Expand Up @@ -99,6 +99,13 @@ abstract class Association
*/
protected $_className;

/**
* The field name in the owning side table that is used to match with the foreignKey
*
* @var string|array
*/
protected $_bindingKey;

/**
* The name of the field representing the foreign key to the table to load
*
Expand Down Expand Up @@ -196,6 +203,7 @@ public function __construct($alias, array $options = [])
'conditions',
'dependent',
'finder',
'bindingKey',
'foreignKey',
'joinType',
'propertyName',
Expand Down Expand Up @@ -316,6 +324,30 @@ public function conditions($conditions = null)
return $this->_conditions;
}

/**
* Sets the name of the field representing the binding field with the target table.
* When no manually specified the primary key of the owning side table is used.
*
* If no parameters are passed the current field is returned
*
* @param string|null $key the table field to be used to link both tables together
* @return string|array
*/
public function bindingKey($key = null)
{
if ($key !== null) {
$this->_bindingKey = $key;
}

if ($this->_bindingKey === null) {
$this->_bindingKey = $this->isOwningSide($this->source()) ?
$this->source()->primaryKey() :
$this->target()->primaryKey();
}

return $this->_bindingKey;
}

/**
* Sets the name of the field representing the foreign key to the target table.
* If no parameters are passed the current field is returned
Expand Down
14 changes: 7 additions & 7 deletions src/ORM/Association/BelongsTo.php
Expand Up @@ -143,7 +143,7 @@ public function saveAssociated(EntityInterface $entity, array $options = [])

$properties = array_combine(
(array)$this->foreignKey(),
$targetEntity->extract((array)$table->primaryKey())
$targetEntity->extract((array)$this->bindingKey())
);
$entity->set($properties, ['guard' => false]);
return $entity;
Expand All @@ -164,20 +164,20 @@ protected function _joinCondition($options)
$tAlias = $this->target()->alias();
$sAlias = $this->_sourceTable->alias();
$foreignKey = (array)$options['foreignKey'];
$primaryKey = (array)$this->_targetTable->primaryKey();
$bindingKey = (array)$this->bindingKey();

if (count($foreignKey) !== count($primaryKey)) {
if (count($foreignKey) !== count($bindingKey)) {
$msg = 'Cannot match provided foreignKey for "%s", got "(%s)" but expected foreign key for "(%s)"';
throw new RuntimeException(sprintf(
$msg,
$this->_name,
implode(', ', $foreignKey),
implode(', ', $primaryKey)
implode(', ', $bindingKey)
));
}

foreach ($foreignKey as $k => $f) {
$field = sprintf('%s.%s', $tAlias, $primaryKey[$k]);
$field = sprintf('%s.%s', $tAlias, $bindingKey[$k]);
$value = new IdentifierExpression(sprintf('%s.%s', $sAlias, $f));
$conditions[$field] = $value;
}
Expand All @@ -193,7 +193,7 @@ protected function _linkField($options)
$links = [];
$name = $this->alias();

foreach ((array)$this->target()->primaryKey() as $key) {
foreach ((array)$this->bindin as $key) {
$links[] = sprintf('%s.%s', $name, $key);
}

Expand All @@ -210,7 +210,7 @@ protected function _linkField($options)
protected function _buildResultMap($fetchQuery, $options)
{
$resultMap = [];
$key = (array)$this->target()->primaryKey();
$key = (array)$this->bindingKey();

foreach ($fetchQuery->all() as $result) {
$values = [];
Expand Down
4 changes: 2 additions & 2 deletions src/ORM/Association/DependentDeleteTrait.php
Expand Up @@ -40,8 +40,8 @@ public function cascadeDelete(EntityInterface $entity, array $options = [])
}
$table = $this->target();
$foreignKey = (array)$this->foreignKey();
$primaryKey = (array)$this->source()->primaryKey();
$conditions = array_combine($foreignKey, $entity->extract($primaryKey));
$bindingKey = (array)$this->bindingKey();
$conditions = array_combine($foreignKey, $entity->extract($bindingKey));

if ($this->_cascadeCallbacks) {
$query = $this->find('all')->where($conditions);
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association/HasMany.php
Expand Up @@ -98,7 +98,7 @@ public function saveAssociated(EntityInterface $entity, array $options = [])

$properties = array_combine(
(array)$this->foreignKey(),
$entity->extract((array)$this->source()->primaryKey())
$entity->extract((array)$this->bindingKey())
);
$target = $this->target();
$original = $targetEntities;
Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Association/HasOne.php
Expand Up @@ -123,7 +123,7 @@ public function saveAssociated(EntityInterface $entity, array $options = [])

$properties = array_combine(
(array)$this->foreignKey(),
$entity->extract((array)$this->source()->primaryKey())
$entity->extract((array)$this->bindingKey())
);
$targetEntity->set($properties, ['guard' => false]);

Expand Down
8 changes: 4 additions & 4 deletions src/ORM/Association/SelectableAssociationTrait.php
Expand Up @@ -25,8 +25,8 @@ trait SelectableAssociationTrait
{

/**
* Returns true if the eager loading process will require a set of parent table's
* primary keys in order to use them as a filter in the finder query.
* Returns true if the eager loading process will require a set of the owning table's
* binding keys in order to use them as a filter in the finder query.
*
* @param array $options The options containing the strategy to be used.
* @return bool true if a list of keys will be required
Expand Down Expand Up @@ -234,7 +234,7 @@ protected function _buildSubquery($query)
$filterQuery->offset(null);
}

$keys = (array)$this->source()->primaryKey();
$keys = (array)$this->bindingKey();

if ($this->type() === $this::MANY_TO_ONE) {
$keys = (array)$this->foreignKey();
Expand Down Expand Up @@ -271,7 +271,7 @@ protected function _resultInjector($fetchQuery, $resultMap, $options)
$sAlias = $source->alias();
$keys = $this->type() === $this::MANY_TO_ONE ?
$this->foreignKey() :
$source->primaryKey();
$this->bindingKey();

$sourceKeys = [];
foreach ((array)$keys as $key) {
Expand Down

0 comments on commit b6f0b88

Please sign in to comment.