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

JoinWith - assign a column aliases to an attribute of related model #12846

Closed
minitia opened this issue Oct 26, 2016 · 7 comments
Closed

JoinWith - assign a column aliases to an attribute of related model #12846

minitia opened this issue Oct 26, 2016 · 7 comments

Comments

@minitia
Copy link
Contributor

minitia commented Oct 26, 2016

This is my query:

$tripQry = Trip::find()
        ->select(['{{trip}}.*', 'distance' => 'MIN((((acos(sin(('.$coords_data[0].'*pi()/180)) * sin(({{trip_points}}.[[lat]]*pi()/180))+cos(('.$coords_data[0].'*pi()/180)) * cos(({{trip_points}}.[[lat]]*pi()/180)) * cos((('.$coords_data[1].'- {{trip_points}}.[[lng]]) * pi()/180))))*180/pi())*60*1.1515*1.609344))'])
        ->joinWith(['tripPoints'])
        ->where(['{{trip}}.[[matches_id]]' => $match_id])
        ->orderBy(['distance'=>SORT_ASC])
        ->groupBy('id');

I'd like to assign distance value to tripPoints.distance attribute.
It seems that I can assign to Trip attribute.

Any suggestion?

Thank you,
Mattia

@arogachev
Copy link
Contributor

arogachev commented Oct 26, 2016

@minitia Please format your code properly.

As for your problem, seems like it's already covered in offiical docs in Active Record - Selecting extra fields section. The given example uses one model, but instead of assigning to tripPoints.distance attribute you can assign calculation results to tripPointsDistance additional attribute of the main model.

If you want to assign distance as attribute of tripPoints, you can try to use the same technique but customize joinWith like so:

->joinWith(['books' => function (\yii\db\ActiveQuery $query) {
    $query->addSelect('distance' => 'your expression goes here']);
}]

You can find more info about joinWith and addSelect in official docs.

@yii-bot
Copy link

yii-bot commented Oct 26, 2016

This is an automated comment, triggered by adding the label question.

Please note, that the GitHub Issue Tracker is for bug reports and feature requests only.

We are happy to help you on the support forum, on IRC (#yii on freenode), or Gitter.

Please use one of the above mentioned resources to discuss the problem.
If the result of the discussion turns out that there really is a bug in the framework, feel free to
come back and provide information on how to reproduce the issue. This issue will be closed for now.

@yii-bot yii-bot closed this as completed Oct 26, 2016
@minitia
Copy link
Contributor Author

minitia commented Oct 26, 2016

Probably I have not explained well.
I'd like to assign "distance" columns alias not to del principal "Trip model" but to the related one "TripPoint" used in joinWith condition.

@arogachev
Copy link
Contributor

arogachev commented Oct 26, 2016

@minitia I covered this with separate example. Same technique is used, the only difference is you need to move selecting distance part to joinWith relation call.

@minitia
Copy link
Contributor Author

minitia commented Oct 26, 2016

@arogachev thank you.
Now I've understand but adding the selecting part of distance in joinWith relational call the problem is that the $coordsvariable used in calculation is not visible inside the function.

@arogachev
Copy link
Contributor

arogachev commented Oct 26, 2016

@minitia In PHP you can pass additional variables to closure using use keyword:

function (\yii\db\ActiveQuery $query) use ($coords) {
    // Use $coords here
}

@minitia
Copy link
Contributor Author

minitia commented Oct 26, 2016

Thanks you so much!

Il 26 ott 2016 2:00 PM, "Alexey Rogachev" notifications@github.com ha
scritto:

@minitia https://github.com/minitia In PHP you can pass additional
variables to closure using use keyword:

function (\yii\db\ActiveQuery $query) use ($yourAdditionalVariable) { // Use $yourAdditionalVariable here}


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#12846 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/ANZq6tdHAAMsG2spcpp2Oobxvb7DP6vhks5q30DLgaJpZM4Kg9zj
.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants