Data from one model to third

Hi, I have a very interesting problem. I have a list of tables, some of them have relations. Based on them I have some models. I will create example:

table model

product Product

customers Customers

description Description

I found a tutorial how to pass data from one model to other using controller.

http://www.satusoftware.com/frontend/web/site/post/9/Tutorial-How-to-Display-data-from-related-table-using-DetailView::widget-in-Yii2-Framework

Mai main table and main model is customer, based on this I joint it with Products, I have a view witch echo components from both, My question is how can I echo in the same view data witch are in Description if Customer and Description have no related data, I tried to use the same method and pass data from Description to Product, and after that to Customer, but it not works.

Any tips how to do it ?

What are the relations among those 3 models?

Customers have relation to Product (customer have the product.id_product)

Product have relation with Description (product.id have relation with description, where product.id is foreign key)

Product is making the connection from firs to third model.

I’m sorry, but it’s not clear enough for me.

Do you say that a customer has one product? Or, a customer may have many products?

So a product can have multiple descriptions?

Anyway, there’s nothing special here.

Product is a relation of Customer, and Description is a relation of Product, right?

Then you can easily access the descriptions from a customer, using something like "$user->product->description".

it is 1 to n to 1

One customer can have n products,

one product have 1 description

I see. Then, assuming you have set up the relations appropriately, you may write like this:




$user = User::find()->...->one();

foreach($user->products as $product) {

    $description = $product->description;

}



When a relation is established between 2 models, then you can access the related objects using "->" notation. "$user->products" is an array of Product objects here, since the relation is "hasMany".

I want to do it with models and crud generator, module baste on first table have to give relations for or other tables, this is my main problem.

I have an image of my DB relation. And after generation on one model, it is showing me relations only for next table


public function getIDPRODUKT()

    {

        return $this->hasOne(PRODUKT::className(), ['ID' => 'ID_PRODUKT']);

    }

I need not only to view all but also to have search function, so I cant use more then one search and one controller.

This image show is shot example, if I will have more relations, how to handle with this?

If you want to search models by the values of the related models, you have to write additional code in your search model on your own. Gii won’t help you in this aspect.

Take a look at the following wikis first.

http://www.yiiframework.com/wiki/653/displaying-sorting-and-filtering-model-relations-on-a-gridview

http://www.yiiframework.com/wiki/780/drills-search-by-a-has_many-relation-in-yii-2-0

I have done the connection between 3 models. It is working, the point is that now I see what I need, but when I go to detail view it is going to show me date based on first(main) model, how can I pass the same date from gridView to detailView ???

Tx in advance !!!

I found the solution for going only one step,

I have 4 models

UserLogin->UsersInfo->Products->ProductDescriprion

I can pass data only from 2 models in one view, based on userlogin info I have UserInfo, but when I try to pass data from Products based on UserInfo it all the tame give me error:


Getting unknown property: app\models\UserLogin::id_product 

even that I have add them on use and have connection in eache model something like this


public function getProducts()	{

	           

    return 	$this->hasMany(SslProducts::className(),['id' =>'id_produkt']);		

	}

for connection between all steps

Any clue how to this because all tutorials are about connection of 2 models

tx in advance !

What are the relations among those models?

I mean, for example, you could describe them as something like the following:




UserLogin : UserInfo = 1 : 1

UserInfo : Products = 1 : N

Product : ProductDescription = 1 : 1



Or, in other words:




UserLoign has one UserInfo

UserInfo has many Products

Product has one ProductDescription



Assuming those relations, you could write something like the following:




$user = UserLogin::find()->...->one();

$userInfo = $user->userInfo;

$products = $userInfo->products;

foreach($products as $product) {

   echo $product->productDescription;

}



And if you want to find a user who has a product whose description contains some word:




$word = 'something';

$query = UserLogin::find()->with('userInfo.products.productDescription')

   ->where('like', 'product_desctiption.description', $word);

   ...