yiisoft / yiisoft/active-record

FR ActiveRecod relation parameters in with() call as array

Open
#59 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
119
Forks
38
Avg merge
1h 11m
Merged PRs (30d)
1

Description

Feature Request. Hello. I'm using AR and created a relation for current user's relational record in another table (user's vote result of poll). Relation looks this way:

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserPollStatus($id)
    {
        return $this->hasOne(UserPollStatusItem::className(),
            ['poll_id' => 'id'])
            ->where(['user_id' => $id])
            ->limit(1);
    }

I was expecting that could be able to enable eager loading for this query and that this will look like:

if ($user_id !== null)
  $query->with(['userPollStatus' => [$user_id]]);

But it looks that this doesn't work, it supports only callbacks.
Also, it does not check really passed value.

What about adding ability to pass an array instead of callback to modify relation query?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the ActiveRecord with() eager-loading entry point and trace how relation callbacks are currently handled. Compare that behavior with the requested array form shown in the issue, and consider the existing value-checking concern; done means array parameters modify the relation query without breaking callback support.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.