framework icon indicating copy to clipboard operation
framework copied to clipboard

Making select() work in with() queries

Open crissi opened this issue 13 years ago • 17 comments

->with(array('schools' => function($query) { $query->select('name'); } }

It would be nice if we could use select() with with() queries

crissi avatar May 03 '13 15:05 crissi

I have a feeling this has been asked before. But even so I think it'd be cool to be able to limit queries even further when constraining them.

jasonlewis avatar May 05 '13 00:05 jasonlewis

What happens if you try to do that now. What error do you get?

taylorotwell avatar May 12 '13 18:05 taylorotwell

return Country::with(array('schools' => function($query)
{
    $query->select('name');
}
))->get();

this will make all the countries have an empty schools array, even if they have schools

crissi avatar May 12 '13 20:05 crissi

Try add "id" to the select and see if it works.

On May 12, 2013, at 3:26 PM, crissi [email protected] wrote:

return Country::with(array('schools' => function($query) { $query->select('name'); } ))->get(); this will make all the countries have an empty schools array, even if they have schools

— Reply to this email directly or view it on GitHub.

taylorotwell avatar May 12 '13 20:05 taylorotwell

nope same result!

crissi avatar May 12 '13 20:05 crissi

Hmm, I'll play with it later tonight.

On May 12, 2013, at 3:30 PM, crissi [email protected] wrote:

nope same result!

— Reply to this email directly or view it on GitHub.

taylorotwell avatar May 12 '13 20:05 taylorotwell

NEVERMIND: I guess it only worked due to a join.

I got something like this working by using an array in the select() function like

...
$query->select(array('name'));
...

sq2 avatar May 12 '13 21:05 sq2

This works for me. Try updating your framework and verify the issue persists. I'm unable to recreate.

taylorotwell avatar May 18 '13 01:05 taylorotwell

Updated composer. Still not working:(

crissi avatar May 18 '13 14:05 crissi

Paste your models, entire queries, tables, etc

On May 18, 2013, at 9:54 AM, crissi [email protected] wrote:

Updated composer. Still not working:(

— Reply to this email directly or view it on GitHub.

taylorotwell avatar May 18 '13 14:05 taylorotwell

I did some debugging: If this does not helps you out I will post everything!

I can see that this is firing this when I do not use a select: 'select * from schoolswhereschools.country_id in ('AD', 'AE', 'AF', 'AG')'

but as I use ->select('name'), it does not work: 'select namefromschoolswhereschools.country_id in ('AD', 'AE', 'AF', 'AG')'

It works if I do this: ->select('name', 'country_id');

because it is firing this: 'select name, country_idfromschoolswhereschools.country_id in ('AD', 'AE', 'AF', 'AG')'

The query have the country_id so it is possible for the framework to know what country the schools should be added to.

The same thing is happening on many to many relationships, There it is the pivot_something_id that is not being created when you use select()

crissi avatar May 19 '13 11:05 crissi

Yes that is the danger of using select in those situations is you have to include the IDs.

On May 19, 2013, at 6:38 AM, crissi [email protected] wrote:

I did some debugging: If this does not helps you out I will post everything!

I can see that this is firing this when I do not use a select: 'select * from schools where schools.country_id in ('AD', 'AE', 'AF', 'AG')'

but as I use ->select('name'), it does not work: 'select name from schools where schools.country_id in ('AD', 'AE', 'AF', 'AG')'

It works if I do this: ->select('name', 'country_id');

because it is firing this: 'select name, country_id from schools where schools.country_id in ('AD', 'AE', 'AF', 'AG')'

The query have the country_id so it is possible for the framework to know what country the schools should be added to.

The same thing is happening on many to many relationships, There it is the pivot_something_id that is not being created when you use select()

— Reply to this email directly or view it on GitHub.

taylorotwell avatar May 19 '13 14:05 taylorotwell

I see, but wouldn't it be more convenient if the framework automatically took care of that.

crissi avatar May 19 '13 14:05 crissi

It might be possible for us to do that, I'll have to look into it.

On May 19, 2013, at 9:09 AM, crissi [email protected] wrote:

I see, but wouldn't it be more convenient if the framework automatically took care of that.

— Reply to this email directly or view it on GitHub.

taylorotwell avatar May 19 '13 14:05 taylorotwell

Cool!

Sendt fra min iPhone

Den 19/05/2013 kl. 16.34 skrev "Taylor Otwell" [email protected]:

It might be possible for us to do that, I'll have to look into it.

On May 19, 2013, at 9:09 AM, crissi [email protected] wrote:

I see, but wouldn't it be more convenient if the framework automatically took care of that.

— Reply to this email directly or view it on GitHub. — Reply to this email directly or view it on GitHub.

crissi avatar May 19 '13 15:05 crissi

at the moment I am using the IDs to make it work and I am too looking forward to a better solution to this.

raftalks avatar May 19 '13 18:05 raftalks

could this issue be opened?

crissi avatar Oct 04 '13 12:10 crissi