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

[5.x] Single relationship query builders #9979

Conversation

jacksleight
Copy link
Contributor

@jacksleight jacksleight commented May 1, 2024

This PR is really intended to be an idea rather than a final implementation, the solution is a bit of a hack and a better approach is probably needed, but it would be really nice if this was possible.

At the moment if you're working in PHP and have an entry with either single or multi entries fields you can do this to fetch the published related entries:

$publishedChildren = $parent->children;
$publishedChild = $parent->child;

If you want related entries of any status you can do this with multi entries fields:

$anyChildren = $parent->children()->whereAnyStatus()->get();

But if you want a single related entry of any status there's not really a simple way to do it (I don't think). You can't do this with single entry fields, it throws a child method does not exist error:

$anyChild = $parent->child()->whereAnyStatus()->first();

When working with Eloquent models you can do $model->singleThing() with a belongs to relationship and it'll give you a query builder, it'd be great if Statamic could do the same.

This PR makes the above work by tricking any relationship fieldtypes into returning a query builder even if it's a single entry field. This only happens during __call so shouldn't affect anything else.

@jacksleight jacksleight changed the title [4.x] Single entry relationship query builders [4.x] Single relationship query builders May 1, 2024
@jacksleight jacksleight changed the base branch from 4.x to 5.x May 10, 2024 10:03
@jacksleight jacksleight changed the title [4.x] Single relationship query builders [5.x] Single relationship query builders May 10, 2024
Copy link
Member

@jasonvarga jasonvarga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the delay.

I think a better solution would be to do it in the fieldtype's augment method.

For example, in here:

return $single ? Blink::once($key, fn () => $query->first()) : $query;

Rather than getting ->first(), just continue to return the query.

Obviously this would be a breaking change, so we can make it opt in. You could have an option on the field.

return $single && !$this->config('always_augment_to_query', false)
  ? Blink::once($key, fn () => $query->first())
  : $query;

Wanna try that and see if it works for your use case?

@jacksleight
Copy link
Contributor Author

jacksleight commented Sep 30, 2024

I think a better solution would be to do it in the fieldtype's augment method.

Agreed, much better idea, I will update.

You could have an option on the field.

Do you think it should be a field option or just a config option in statamic.system? I feel like if you want this behaviour you'd want it globally, it could be confusing if some fields behaved one way and some the other.

@jasonvarga
Copy link
Member

I feel like if you want this behaviour you'd want it globally

That's probably true

@jacksleight
Copy link
Contributor Author

Closed in favour of #11086.

@jacksleight jacksleight closed this Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants