I’m trying to add a report to my vps reseller system. It uses three tables in this report, user, user_agent and fund_log.
user(
id
agent_id -- direct parent user_id
...
)
// user-agent relation table, use to save ALL agent relation, such as A-B-C, so there is 2 record describing {agent_id:A, user_id:C, distance:2},{agent_id:B, user_id:C, distance:1}. This relation help me to calculate all A's children and A's parents(even to the top reseller)
user_agent(
agent_id --
user_id --
distance
)
fund_log(
user_id, -- who this log belongs to
type, -- log type, to describe the business type
amount,
)
See, User has many UserAgent, User or UserAgent has many FundLog.
how to define HAS_MANY:HAS_MANY in ar? in SQL, I can get all children first then try "SELECT sum(amount) FROM fund_log WHERE user_id in (:all_children)",
but the STAT defining required user_id
as the third item.
class User {
public function relations(){
return array(
'all_children' => array(self::HAS_MANY, 'UserAgent', 'agent_id'),
);
}
}
class UserAgent {
public function relations(){
return array(
'log_sum' => array(self::STAT, 'FundLog', 'user_id'), //
);
}
}