DboSource::_mergeHasMany PHP Method

_mergeHasMany() protected method

Note: this function also deals with the formatting of the data.
protected _mergeHasMany ( &$resultSet, array $assocResultSet, string $association, Model $Model ) : void
$assocResultSet array Data to merge.
$association string Name of Model being merged.
$Model Model Model being merged onto.
return void
    protected function _mergeHasMany(&$resultSet, $assocResultSet, $association, Model $Model)
    {
        $modelAlias = $Model->alias;
        $primaryKey = $Model->primaryKey;
        $foreignKey = $Model->hasMany[$association]['foreignKey'];
        foreach ($resultSet as &$result) {
            if (!isset($result[$modelAlias])) {
                continue;
            }
            $resultPrimaryKey = $result[$modelAlias][$primaryKey];
            $merged = array();
            foreach ($assocResultSet as $data) {
                if ($resultPrimaryKey !== $data[$association][$foreignKey]) {
                    continue;
                }
                if (count($data) > 1) {
                    $data = array_merge($data[$association], $data);
                    unset($data[$association]);
                    foreach ($data as $key => $name) {
                        if (is_numeric($key)) {
                            $data[$association][] = $name;
                            unset($data[$key]);
                        }
                    }
                    $merged[] = $data;
                } else {
                    $merged[] = $data[$association];
                }
            }
            $result = Hash::mergeDiff($result, array($association => $merged));
        }
    }
DboSource