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

Request: Derived columns for lists #940

Open
benmajor opened this issue Mar 6, 2024 · 2 comments
Open

Request: Derived columns for lists #940

benmajor opened this issue Mar 6, 2024 · 2 comments
Labels

Comments

@benmajor
Copy link
Contributor

benmajor commented Mar 6, 2024

As far as I'm aware, there's currently no way of obtaining an aggregated column from a bean's own or shared list.

For example, if we have a Product bean which may have many Review beans in its ownReviewList, we sometimes need to get the average score for a given Product bean. When using Models, it would be super-handy to be able to use derived columns, similar to the with() or withCondition() methods. For example, the Product model may contain a method as follows (using psuedo-code for the missing RedBean functionality):

public function getAverageRating(): float
{
    return $this->bean->aggregate('AVG(score)')->ownReviewList;
}

Is this currently possible with RedBean (without computing the value manually using a loop of the ownReviewList), or do we have to use R::getCell() for the time-being? If it's the latter, it would be nice to implement this feature so that we don't need to taint models with calls to R-spaced methods.

Thanks!
Ben.

@gabordemooij
Copy link
Owner

I always use R::cell for this. It's what SQL really shines at, making reports/aggregates etc.

@gabordemooij
Copy link
Owner

gabordemooij commented Mar 16, 2024

If you don't want to use static methods in your models, simply pass the adapter.

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

No branches or pull requests

2 participants