Selecting like $product->file->product Run a query again

Hi,

I know i saw the solution somewhere but I cant remember :(

I need to access the Model in its Relation.

This run a query Again :expressionless:





$product = Product::model()->with(array('user.profile','file'))->findByPk($id);

$product->file->product;



How should I setup the relation to handle this?

The following will work, but it’s performing an extra join and essentially fetching a duplicate record:




$product = Product::model()->with(array('user.profile','file.product'))->findByPk($id);

$product->file->product;



Do you really need to do this though? You already have the relevant product and file, so you shouldn’t need to navigate both ways.

What’s the relation between Product and File?

Is it "one to one"? Then I second Keith.

Or is it “one to many”? I mean, A Product BELONGS_TO a File, and a File has many Products? Then, well, I’d like to ask you the meaning of “$product->file->product”. What use case do you have in your mind?

Hi thank for replay,

i know I’ve done that , but isn’t there a way to do it without performing another join ?

and yes I need it :(

File BELONG TO product and Product HAS MANY files.

I need it because File Model has variable Price, Product has Price too

So i have a function named getIPrice that work like this







	public function getIPrice()

	{


		if ($this->price == 0)

			return $this->product->price;

		return $this->price;

	}



then I use iPrice in GridViews …

this is why i need to access it :-/

The easiest solution is the one I suggested first, assuming you’re not dealing with a huge number of records at a time.

The other option would be to iterate through each of the records after fetching them, and adding the link back to the product in each individual file record. Unless you’re dealing with lots of records, I don’t see much value in this.

Yeah, i just thought there might be a good solution for this :D

Thank you @Keith