Permalink
Browse files

Fixed eagerLoading of HasMany now that primaryKey returns an array

  • Loading branch information...
1 parent 193951c commit 5cde1590a7137520670a39dd5c22effbfa6ff6cd @lorenzo lorenzo committed Jan 8, 2014
Showing with 22 additions and 11 deletions.
  1. +2 −0 Cake/ORM/Association/BelongsTo.php
  2. +20 −11 Cake/ORM/Association/ExternalAssociationTrait.php
@@ -137,6 +137,8 @@ public function save(Entity $entity, $options = []) {
*
* @param array $options list of options passed to attachTo method
* @return array
+ * @throws \RuntimeException if the number of columns in the foreignKey do not
+ * match the number of columns in the target table primaryKey
*/
protected function _joinCondition(array $options) {
$conditions = [];
@@ -121,19 +121,28 @@ public function transformRow($row) {
*
* @param array $options list of options passed to attachTo method
* @return array
+ * @throws \RuntimeException if the number of columns in the foreignKey do not
+ * match the number of columns in the source table primaryKey
*/
protected function _joinCondition(array $options) {
- $field = sprintf(
- '%s.%s',
- $this->_sourceTable->alias(),
- $this->_sourceTable->primaryKey()
- );
- $value = new IdentifierExpression(sprintf(
- '%s.%s',
- $this->_targetTable->alias(),
- $options['foreignKey']
- ));
- return [$field => $value];
+ $conditions = [];
+ $tAlias = $this->target()->alias();
+ $sAlias = $this->_sourceTable->alias();
+ $foreignKey = (array)$options['foreignKey'];
+ $primaryKey = $this->_sourceTable->primaryKey();
+
+ if (count($foreignKey) !== count($primaryKey)) {
+ $msg = 'Cannot match provided foreignKey, got %d columns expected %d';
+ throw new \RuntimeException(sprintf($msg, count($foreignKey), count($primaryKey)));
+ }
+
+ foreach ($foreignKey as $k => $f) {
+ $field = sprintf('%s.%s', $sAlias, $primaryKey[$k]);
+ $value = new IdentifierExpression(sprintf('%s.%s', $tAlias, $f));
+ $conditions[$field] = $value;
+ }
+
+ return $conditions;
}
/**

0 comments on commit 5cde159

Please sign in to comment.