Yii Framework Forum: Is It Possible To Return A Calc Field In Model - Yii Framework Forum

Jump to content

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

Is It Possible To Return A Calc Field In Model Rate Topic: -----

#1 User is offline   frocco 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 227
  • Joined: 17-February 10

Posted 18 February 2013 - 01:47 PM

Hello,

In my database table, I have price1, price2, price3
I want to return a field called price that is one of those above.

if group = "A"
price = price1
if group = "B"
price = price2

so when I run my query, I can just reference field price

Thanks
Regards,

Frank
0

#2 User is offline   rootbear 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 222
  • Joined: 17-June 11

Posted 18 February 2013 - 02:05 PM

in your model,
public $price;

public function afterFind(){
  if ($this->group === 'A'){
    $this->price = $this->price1;
  } else if  ($this->group === 'B'){
    $this->price = $this->price2;
  } else {
    $this->price = $this->price3;
  }
}



in your view;

echo $this->price;


or in your view,

'value'=>"($data->group === 'A')?$data->price1:(($data->group === 'B')?$data->price2:$data->price3)",

I enjoy the Yii.sy coding life here.
0

#3 User is offline   frocco 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 227
  • Joined: 17-February 10

Posted 18 February 2013 - 02:40 PM

Thank you,
I like the model approach because I want to ensure the correct field is always returned.
I was doing this using raw sql and doing a price1 as price

I like the model approach much better.
Is there any drawbacks?
Regards,

Frank
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