CakePHP virtual field that is a sum of three other virtual fields?

jdubs

New Member
I have a virtual field in CakePHP that needs to be a SUM of three very disparate SQL queries in my User model. I'm trying to accomplish this by having a virtual field that is a sum of 3 other virtual fields.\[code\]var $virtualFields = array ( 'field_one' => 'select coalesce(sum(coalesce(t_a.field, 0)), 0)*10 as field_one from t_a join t_b on t_a.t_b_id = t_b.id where t_b.user_id=User.id', 'field_two' => 'select coalesce(sum(coalesce(t_c.field, 0)), 0)*2 as field_two from t_d left join (t_c) on (t_d.id=t_c.t_d_id) where t_d.user_id = User.id', 'field_three' => 'select coalesce(sum(coalesce(value, 0)), 0) as field_three from t_e where user_id=User.id', 'field_sum' => 'User.field_one+User.field_two+User.field_three' );\[/code\]This doesn't work. I get the error 'field_one doesn't exist' when it gets to the 'field_sum'. I've asked before how to combine the three sql statements and haven't really gotten a satisfactory answer. It proved much better and easier to simply run them each alone and sum them after the fact. Is there any way to do that with in the context of CakePHP?EditHere is cake's generated SQL:\[code\]SELECT /* Users fields */ (select coalesce(sum(coalesce(t_a.field, 0)), 0)*10 as field_one from t_a join t_b on t_a.t_b_id = t_b.id where t_b.user_id=User.id) AS `User__field_one`, (select coalesce(sum(coalesce(t_c.field, 0)), 0)*2 as field_two from t_d left join (t_c) on (t_d.id=t_c.t_d_id) where t_d.user_id = User.id) AS `User__field_two`, (select coalesce(sum(coalesce(value, 0)), 0) as bonus_reputation from reputation_bonuses where user_id=User.id) AS `User__field_three`, (`User`.`field_one`+`User`.`field_two`+`User`.`field_three`) AS `field_sum`, FROM `users` AS `User` WHERE `User`.`email` = '/* redacted */' AND `User`.`password` = '/* redacted */' LIMIT 1 \[/code\]Having seen that I tried changing the definition to \[code\](User__field_one+User__field_two+User__field_three)\[/code\] to take advantage of how they were named. No luck.The exact error is: SQL Error: 1054 unknown column \[code\]User.field_one\[/code\] in field list.
 
Back
Top