Is It Possible To Return A Calc Field In Model


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


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)",

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?