DboSource::_mergeAssociation PHP Method

_mergeAssociation() protected method

Merge association of merge into data
protected _mergeAssociation ( &$data, &$merge, string $association, string $type, boolean $selfJoin = false ) : void
$association string The association name to merge.
$type string The type of association
$selfJoin boolean Whether or not this is a self join.
return void
    protected function _mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false)
    {
        if (isset($merge[0]) && !isset($merge[0][$association])) {
            $association = Inflector::pluralize($association);
        }
        $dataAssociation =& $data[$association];
        if ($type === 'belongsTo' || $type === 'hasOne') {
            if (isset($merge[$association])) {
                $dataAssociation = $merge[$association][0];
            } else {
                if (!empty($merge[0][$association])) {
                    foreach ($merge[0] as $assoc => $data2) {
                        if ($assoc !== $association) {
                            $merge[0][$association][$assoc] = $data2;
                        }
                    }
                }
                if (!isset($dataAssociation)) {
                    $dataAssociation = array();
                    if ($merge[0][$association]) {
                        $dataAssociation = $merge[0][$association];
                    }
                } else {
                    if (is_array($merge[0][$association])) {
                        foreach ($dataAssociation as $k => $v) {
                            if (!is_array($v)) {
                                $dataAssocTmp[$k] = $v;
                            }
                        }
                        foreach ($merge[0][$association] as $k => $v) {
                            if (!is_array($v)) {
                                $mergeAssocTmp[$k] = $v;
                            }
                        }
                        $dataKeys = array_keys($data);
                        $mergeKeys = array_keys($merge[0]);
                        if ($mergeKeys[0] === $dataKeys[0] || $mergeKeys === $dataKeys) {
                            $dataAssociation[$association] = $merge[0][$association];
                        } else {
                            $diff = Hash::diff($dataAssocTmp, $mergeAssocTmp);
                            $dataAssociation = array_merge($merge[0][$association], $diff);
                        }
                    } elseif ($selfJoin && array_key_exists($association, $merge[0])) {
                        $dataAssociation = array_merge($dataAssociation, array($association => array()));
                    }
                }
            }
        } else {
            if (isset($merge[0][$association]) && $merge[0][$association] === false) {
                if (!isset($dataAssociation)) {
                    $dataAssociation = array();
                }
            } else {
                foreach ($merge as $row) {
                    $insert = array();
                    if (count($row) === 1) {
                        $insert = $row[$association];
                    } elseif (isset($row[$association])) {
                        $insert = array_merge($row[$association], $row);
                        unset($insert[$association]);
                    }
                    if (empty($dataAssociation) || isset($dataAssociation) && !in_array($insert, $dataAssociation, true)) {
                        $dataAssociation[] = $insert;
                    }
                }
            }
        }
    }

Usage Example

 public function mergeAssociation(&$data, &$merge, $association, $type, $selfJoin = false)
 {
     return parent::_mergeAssociation($data, $merge, $association, $type, $selfJoin);
 }
DboSource