Yii Framework Forum: Load Model With Related Model By Conditions - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Load Model With Related Model By Conditions Rate Topic: -----

#1 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,382
  • Joined: 21-February 11
  • Location:Greece

Posted 09 May 2013 - 04:48 AM

Hi All

I have a Model related with another Model.

I want to get all Records of first Model with the second one.
The second one must be null when a specific condition not satisfied but the first Model have to load normally


for example

$models= Product::model()->with(array(Content'=>array('condition'=>'lang=1')))->findAll(array('condition'=>'enabled=1')


With above code the Models not loaded when the condition of second not satisfied.

I want to Load all enabled Products and only their Content that has lang=1 else Content set to Null
Something Like
LEFT OUTER JOIN ON (... AND 'lang=1') WHERE enabled=1 
and not
LEFT OUTER JOIN ON (...) WHERE enabled=1 AND lang=1


Also I have a suspicious that the eager loads the content when $models->content called...

How achieve those things ?
Many thanks!
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#2 User is offline   mbala 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 174
  • Joined: 21-July 12
  • Location:Chennai, Tamilnadu, India

Posted 09 May 2013 - 04:59 AM

try this one
http://www.bsourceco...ii-findall-join
0

#3 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,382
  • Joined: 21-February 11
  • Location:Greece

Posted 09 May 2013 - 06:12 AM

I found the solution by parameter on

$models= Product::model()->with(array(Content'=>array('on'=>'lang=1')))->findAll(array('condition'=>'enabled=1')


I have an issue (As I excpected)
Although the second Model does not loaded as I want (I checked by var_dump), when I Call Protuct->content the Content is auto loaded.
How can prevent this?

Ok I had missed a parameter in another part of code, now everything works ok!
So the solution for my issue is the parameter on in "with".

Thank @mbala for your response :)
Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users