public function findBelongsTo($model, array $associations)
{
$schema = $model->schema();
foreach ($schema->columns() as $fieldName) {
if (!preg_match('/^.+_id$/', $fieldName)) {
continue;
}
if ($fieldName === 'parent_id') {
$className = $this->plugin ? $this->plugin . '.' . $model->alias() : $model->alias();
$assoc = ['alias' => 'Parent' . $model->alias(), 'className' => $className, 'foreignKey' => $fieldName];
} else {
$tmpModelName = $this->_modelNameFromKey($fieldName);
if (!in_array(Inflector::tableize($tmpModelName), $this->_tables)) {
$found = $this->findTableReferencedBy($schema, $fieldName);
if ($found) {
$tmpModelName = Inflector::camelize($found);
}
}
$assoc = ['alias' => $tmpModelName, 'foreignKey' => $fieldName];
if ($schema->column($fieldName)['null'] === false) {
$assoc['joinType'] = 'INNER';
}
}
if ($this->plugin && empty($assoc['className'])) {
$assoc['className'] = $this->plugin . '.' . $assoc['alias'];
}
$associations['belongsTo'][] = $assoc;
}
return $associations;
}