EmadGh
(Emadghasemi 2006)
October 2, 2014, 11:39am
1
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
$product = Product::model()->with(array('user.profile','file'))->findByPk($id);
$product->file->product;
How should I setup the relation to handle this?
Keith
(Kburton)
October 2, 2014, 12:14pm
2
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.
softark
(Softark)
October 2, 2014, 12:36pm
3
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?
EmadGh
(Emadghasemi 2006)
October 2, 2014, 12:45pm
4
Keith:
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.
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
EmadGh
(Emadghasemi 2006)
October 2, 2014, 12:49pm
5
softark:
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?
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 :-/
Keith
(Kburton)
October 2, 2014, 2:56pm
6
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.
EmadGh
(Emadghasemi 2006)
October 2, 2014, 3:40pm
7
Yeah, i just thought there might be a good solution for this
Thank you @Keith