CodeIgniter-MY_Model icon indicating copy to clipboard operation
CodeIgniter-MY_Model copied to clipboard

Querying multiple with in the array?

Open asakapab0i opened this issue 8 years ago • 7 comments

Say I have this as example

$this->country_model->with_cities(array('fields'=>'name,id,population','with'=>array('relation'=>'companies','fields'=>'name,phone_number'))->get($country_id);

But I have another relationship with many locations and I want that location to be pulled as well. How do i do that?

'with'=>array(array('relation' => 'locations'), array('relation'=>'companies','fields'=>'name,phone_number'))

This is my sample code but it doesn't work or not implemented yet?

asakapab0i avatar Jan 27 '17 13:01 asakapab0i

A bit lost here! The "locations" relationship is to country, then this should work:

$this->country_model->with_cities(array('fields'=>'name,id,population','with'=>array('relation'=>'companies','fields'=>'name,phone_number'))
->with_locations(array('fields'=>'name,id','with'=>array('relation'=>'companies','fields'=>'name,phone_number'))
->get($country_id);

salain avatar Jan 27 '17 14:01 salain

In your example you are only querying one relationship in a model.

Suppose in in the Cities Model we have another relationship

So in: City_model.php

...

$this->has_one['country'] => array('Country_model');

And then:

$this->has_many['locations'] => array('Locations_model');

In your example you only included the cities. But what I want is to include the locations.

So therefore my code should make sense.

'with'=>array(array('relation' => 'locations'), array('relation'=>'companies'))

The model may not make sense but what I want is to include multiple relation in the model I called with_

asakapab0i avatar Jan 27 '17 15:01 asakapab0i

First of all, in your Country_model you should have a has_many['cities'] relation. Assuming you have that and you have a has_many['locations'] inside your City_model, you should do something like:

$this->country_model->with_cities(array('fields'=>'name,id,population','with'=>array('relation'=>'locations','fields'=>'location_name,whatever_field'))->get($country_id);

PS: Please define the relations the right way and not the fast way...

avenirer avatar Jan 27 '17 15:01 avenirer

Okay @avenirer , Assuming we already established that code.

What if I will add another relationship in the City Model:

$this->has_many['states'] => array('State_model')

How do I pull that using your code altogether with locations, So I'm querying two relations.

Apologies for my poor example in the previews post. @salain

PS. Nevermind the logic of the data, I just want to make multiple query on relations.

asakapab0i avatar Jan 27 '17 15:01 asakapab0i

ok... I see what you're asking. Up until now I didn't understand what you wanted. You want to retrieve multiple same level relationships inside a relationship. Didn't think of that. It would be interesting to expand the with_ relationships.

avenirer avatar Jan 27 '17 15:01 avenirer

Yes, right now I'm having child models in many parent models and I want to query them all one in one go.

So the output should be something like this:

Object => array( ['country_name'] => 'Philippines', ['cities'] => Object[0] => array( array( 'city' => 'Manila', 'states' => Object[0] => array( array( 'state' => 'Taguig' ) ), 'locations' => Object[0] => array( array( 'location' => 'Something else' ) ) ) ) )

PS. Sorry for bad example again I will refine it later, thank you for your time.

asakapab0i avatar Jan 27 '17 15:01 asakapab0i

@asakapab0i

Now I understand, you want nested relationships. It works as per your example in the first post, providing you have the relationship defined in your cities model.

$this->country_model->with_cities(array('fields'=>'name,id,population',
'with'=>array(
array('relation'=>'states','fields'=>'name'),
array('relation'=>'regions','fields'=>'name'),
array('relation'=>'whatever','fields'=>'name')))
->get($country_id);

I do it with the main relation being one to many and the nested relation are one to one. Not sure if it works one to many for the nested relationships.

salain avatar Jan 30 '17 10:01 salain