CakePHP virtual fields with two tables -
i have 2 tables in webpage: orders:
id int(11) not null auto_increment, created datetime not null, payed datetime default null, primary key (`id`)
and order_items:
id int(11) not null auto_increment, orders_id int(11) not null, amount int(11) not null, price float not null, primary key (`id`)
one order can have many order_items. example records, orders:
id, created, payed 14, '2013-05-14 22:40:49', null 15, '2013-05-14 22:41:03', null
order_items:
id, orders_id, amount, price 12, 14, 1, 5 13, 15, 6, 5
i need somehow link these tables using model. main thing orders result-set should contain virtual field show order sum of money: sum(amount*price). i'm expecting result of orders table:
id, created, payed, order_sum 14, '2013-05-14 22:40:49', null, 5 15, '2013-05-14 22:40:49', null, 30
is possible, or limited virtual fields limits? or ideas appreciated :)
i can't believe :) have managed solve "problem".
here solution: have added field order_items table - sum has value amount*price. , here virtual field in order model:
public $virtualfields = array( 'amount_price'=>' select sum(sum) order_items (orders_id = `order`.`id`) group orders_id'
hope helps, happy baking :)
Comments
Post a Comment