I have 4 more table that I would like to load at once with the main one, the example given in guide is just for one table attached to the main one, How can I do that?
I have 4 more table that I would like to load at once with the main one, the example given in guide is just for one table attached to the main one, How can I do that?
I think what your looking for is this:
// eager loading both "orders" and "country"
$customers = Customer::find()->with('orders', 'country')->all();
// equivalent to the array syntax below
$customers = Customer::find()->with(['orders', 'country'])->all();
// no SQL executed
$orders= $customers[0]->orders;
// no SQL executed
$country = $customers[0]->country;
// eager loading "orders" and the nested relation "orders.items"
$customers = Customer::find()->with('orders.items')->all();
// access the items of the first order of the first customer
// no SQL executed
$items = $customers[0]->orders[0]->items;
This is from the yii2 documentation, you can find all the information here:yii2 eager loading
I understood that but in that case only two tables but images something like custorm has many orders,places to deliver is product and is interested in many sectors of the organisation. So what I want is to load all those information at once using eager loading.
From the documentation:
If you use the command with() you can get all the relations from 2, 3 OR ‘N’ tables
From the first reply what I understand is all these 3 below eager loading are equivalent if there are any mistake correcte please :
// eager loading both "orders" and "country"
//The columns in child tables have the same name as in parent table
$customers = Customer::find()->with('orders','placetodeliver','interestsector')->all();
// Here I suppose that the column that link the customer model to placetodeliver has different name(in customer table it is customerid and in placetodeliver it is idcustomer so I think by writting it in that way it will be recognize if not so please correct me
$customers = Customer::find()->with(['orders','placetodeliver'=>'idcustomer','interestsector'='idcustomer'])->all();
$customers = Customer::find()->with('orders')->with('placetodeliver','idcustomer')->with('interestsector','idcustomer')->all();
Sorry, but I don’t understand what you are trying to say. (@st1ck do you?)
Would you please show us the involved models (or tables) of yours and the relations among them?
I have four tables: customers(customerid,name,address),orders(orderid,date,total,customerid),placetodeliver(country,city,description,idcustomer),interestsector(sector,description,idcustomer).
The table customer is in relation with orders on (customerid,customerid); with placetodeliver on (customerid,idcustomer) and with interestsector on (customerid,idcustomer)
The models of these four tables have been generated. What I want is when I load customers(by find… query method) I load at the same time(once) their orders,placetodeliver and interestsector. I found the eager loading is the best to do that but after reading the part of eager loading I was unable to apply it in my above case because I need to load from 2 more (placetodeliver,interestsector) tables. In Guide 2.0 example the example is about 1 table(orders:
$customers = Customer::find()->with('orders')
).
So the relations are:
Customer has many orders
Customer has one place to deliver
Customer has one interest sector
Am I right?
Then what you have to do is just:
$customers = Customer::find()->with(['orders', 'placeToDeliver', 'interestSector'])->all();
[P.S.]
F.Y.R., the records and the relations are loaded like the following:
select * from customers;
select * from orders where customerid in (N1, N2, N3, ... NX)
select * from placetodeliver where idcustomer in (N1, N2, N3, ... NX)
select * from interestsector where idcustomer in (N1, N2, N3, ... NX)
In the above ‘N1, N2, N3, … NX’ refers to the primary keys that are in the result set of the first query.
Yes, Yii executes 4 queries: 1 for the main model and 3 for the related models.
And Yii will loop through these 4 arrays of the result set in order to populate all of the main models and their related models.
you got it !!!! ok so names of columns don’t matter all I have to do is make sure that I established well the relationship among the models.
Thanks
That’s it.
Yii doesn’t use “join” when it eagerly load the related models. It means that there’s no need to disambiguate the column names by prefixing the table names (or table aliases).
Thanks a lot for your guidance