Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/on model create #59

Merged
merged 4 commits into from
Feb 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/Database/Eloquent/MongoDB/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ abstract class Model extends BaseModel
*/
const SHARED_INFO = [];

/**
* Define list of other models that will be affected
* as the current model is sub-document to it when it gets created
*
* @example ModelClass::class => searchingColumn will be converted to ['searchingColumn['id']', 'columnName', 'sharedInfo']
* @example ModelClass::class => [searchingColumn, creatingColumn]
* @example ModelClass::class => [searchingColumn, creatingColumn, sharedInfoMethod]
*
* @const array
*/
const ON_MODEL_CREATE = [];

/**
* Define list of other models that will be affected as the current object is part of array
* as the current model is sub-document to it when it gets created
*
* @example ModelClass::class => searchingColumn will be converted to ['searchingColumn['id'], 'columnName', 'sharedInfo']
* @example ModelClass::class => [searchingColumn, creatingColumn]
* @example ModelClass::class => [searchingColumn, creatingColumn, sharedInfoMethod]
*
* @const array
*/
const ON_MODEL_CREATE_PUSH = [];

/**
* Define list of other models that will be affected
* as the current model is sub-document to it when it gets updated
Expand Down Expand Up @@ -155,6 +179,76 @@ public static function boot()
}
});

// When model create, detect whether there are any other models that
// shall be created with it
static::created(function ($model) {
$otherModels = config('mongez.database.onModel.create.' . static::class);
if (!empty(static::ON_MODEL_CREATE) || !empty($otherModels)) {
$modelsList = array_merge((array)static::ON_MODEL_CREATE, (array)$otherModels);
foreach ($modelsList as $modelClass => $modelOptions) {
if (is_string($modelOptions)) {
$relationalModel = strtolower(str_replace('Models\\','', strstr($modelClass, 'Models')));
$searchingKey = array_key_exists($relationalModel, $model->toArray()) ? $relationalModel :
array_key_first(array_filter($model->toArray(), function($key) use ($relationalModel) {
return strpos($key, $relationalModel) !== false;
}, ARRAY_FILTER_USE_KEY));

$modelOptions = [$searchingKey, $modelOptions, 'sharedInfo'];
} elseif (count($modelOptions) === 2) {
$modelOptions[] = 'sharedInfo';
}
[$searchingColumn, $creatingColumn, $sharedInfoMethod] = $modelOptions;
if (isset($model->$searchingColumn['id'])) {
$records = $modelClass::query()->where('id', (int) $model->$searchingColumn['id'])->get();
} else {
$searchingIds = array_map(function ($item) {
return (int) $item['id'] ;
}, $model->$searchingColumn);
$records = $modelClass::query()->whereIn('id', $searchingIds)->get();
}

foreach ($records as $record) {
$record->$creatingColumn = $model->$sharedInfoMethod();
$record->save();
}
}
}


$otherModels = config('mongez.database.onModel.createPush.' . static::class);
if (!empty(static::ON_MODEL_CREATE_PUSH) || !empty($otherModels)) {
$modelsList = array_merge((array)static::ON_MODEL_CREATE_PUSH, (array)$otherModels);
foreach ($modelsList as $modelClass => $modelOptions) {
if (is_string($modelOptions)) {
$relationalModel = strtolower(str_replace('Models\\','', strstr($modelClass, 'Models')));
$searchingKey = array_key_exists($relationalModel, $model->toArray()) ? $relationalModel :
array_key_first(array_filter($model->toArray(), function($key) use ($relationalModel) {
return strpos($key, $relationalModel) !== false;
}, ARRAY_FILTER_USE_KEY));

$modelOptions = [$searchingKey, $modelOptions, 'sharedInfo'];
} elseif (count($modelOptions) === 2) {
$modelOptions[] = 'sharedInfo';
}
[$searchingColumn, $creatingColumn, $sharedInfoMethod] = $modelOptions;

if (isset($model->$searchingColumn['id'])) {
$records = $modelClass::query()->where('id', (int) $model->$searchingColumn['id'])->get();
} else {
$searchingIds = array_map(function ($item) {
return (int) $item['id'] ;
}, $model->$searchingColumn);
$records = $modelClass::query()->whereIn('id', $searchingIds)->get();
}

foreach ($records as $record) {
$record->reassociate($model->$sharedInfoMethod(), $creatingColumn)->save();
}

}
}
});

static::updating(function ($model) {
if (static::UPDATED_BY && ($user = user())) {
$model->updatedBy = user()->sharedInfo();
Expand Down