public function findHasMany($model, array $associations)
{
$schema = $model->schema();
$primaryKey = (array) $schema->primaryKey();
$tableName = $schema->name();
$foreignKey = $this->_modelKey($tableName);
$tables = $this->listAll();
foreach ($tables as $otherTableName) {
$otherModel = $this->getTableObject($this->_camelize($otherTableName), $otherTableName);
$otherSchema = $otherModel->schema();
$pregTableName = preg_quote($tableName, '/');
$pregPattern = "/^{$pregTableName}_|_{$pregTableName}\$/";
if (preg_match($pregPattern, $otherTableName) === 1) {
$possibleHABTMTargetTable = preg_replace($pregPattern, '', $otherTableName);
if (in_array($possibleHABTMTargetTable, $tables)) {
continue;
}
}
foreach ($otherSchema->columns() as $fieldName) {
$assoc = false;
if (!in_array($fieldName, $primaryKey) && $fieldName === $foreignKey) {
$assoc = ['alias' => $otherModel->alias(), 'foreignKey' => $fieldName];
} elseif ($otherTableName === $tableName && $fieldName === 'parent_id') {
$className = $this->plugin ? $this->plugin . '.' . $model->alias() : $model->alias();
$assoc = ['alias' => 'Child' . $model->alias(), 'className' => $className, 'foreignKey' => $fieldName];
}
if ($assoc && $this->plugin && empty($assoc['className'])) {
$assoc['className'] = $this->plugin . '.' . $assoc['alias'];
}
if ($assoc) {
$associations['hasMany'][] = $assoc;
}
}
}
return $associations;
}