Karaka-Management / phpOMS

PHP Framework
Other
2 stars 0 forks source link

Create a new read mapper function that returns relation models instead of its own model #320

Open spl1nes opened 1 year ago

spl1nes commented 1 year ago

Requesting related models with additional conditions can be painful. The current solution is painful by creating a query that selects ids which then can be used to select the models:

$query   = new Builder($this->app->dbPool->get());
$results = $query->selectAs(ClientMapper::HAS_MANY['files']['external'], 'file')
    ->from(ClientMapper::TABLE)
    ->leftJoin(ClientMapper::HAS_MANY['files']['table'])
        ->on(ClientMapper::HAS_MANY['files']['table'] . '.' . ClientMapper::HAS_MANY['files']['self'], '=', ClientMapper::TABLE . '.' . ClientMapper::PRIMARYFIELD)
    ->leftJoin(MediaMapper::TABLE)
        ->on(ClientMapper::HAS_MANY['files']['table'] . '.' . ClientMapper::HAS_MANY['files']['external'], '=', MediaMapper::TABLE . '.' . MediaMapper::PRIMARYFIELD)
     ->leftJoin(MediaMapper::HAS_MANY['types']['table'])
        ->on(MediaMapper::TABLE . '.' . MediaMapper::PRIMARYFIELD, '=', MediaMapper::HAS_MANY['types']['table'] . '.' . MediaMapper::HAS_MANY['types']['self'])
    ->leftJoin(MediaTypeMapper::TABLE)
        ->on(MediaMapper::HAS_MANY['types']['table'] . '.' . MediaMapper::HAS_MANY['types']['external'], '=', MediaTypeMapper::TABLE . '.' . MediaTypeMapper::PRIMARYFIELD)
    ->where(ClientMapper::HAS_MANY['files']['self'], '=', $account->id)
    ->where(MediaTypeMapper::TABLE . '.' . MediaTypeMapper::getColumnByMember('name'), '=', 'client_profile_image');

$accountImage = MediaMapper::get()
    ->with('types')
    ->where('id', $results)
    ->limit(1)
    ->execute();

It would be much nicer if we could do something like this:

ItemManagement::getRelations()->with('files')->where(...);

We already have a nasty join implementation maybe this can be used and improved.