Ar: Get Value From Relations

Hello All,

i have two table let say "Loan" and "LoanRepayment", here for detail:




Loan:

-------------------------

loan_id	| amount	| 

-------------------------

1	| 2000		|

2	| 3000		|

-------------------------


LaonRepayment:

-------------------------------------------------

payment_id	| loan_id	| total_payment	|

-------------------------------------------------

1		| 1		| 50		|

2		| 1		| 1000		|

3		| 2		| 1500		|

-------------------------------------------------



  1. How to get Total Loan use AR ?



in sql i can do "select sum(amount) as total_loan from Loan".



  1. How to get Total Repayment ?



in sql in can do "select sum(total_payment) as total_payment from LoanRepayment".



  1. How to get Loan Balance ?



in sql i can do 

"select loan_id,amount-sum(total_payment) 

from Loan left join LoanRepayment on Loan.loan_id = LoanRepayment.loan_id 

group by TLoan.loan_id"



I still use sql to get 3 conditions, and still have problem when use AR.

thank you

In simple in AR:

1-




$model = Loan::model()->findAll(array(

     	'select'=>'sum(amount) as total_loan',

));



2-




$model = LoanRepayment::model()->findAll(array(

     	'select'=>'sum(total_payment) as total_payment',

));



3- In this case you have to implement your releations in the model:




$model = Loan::model()->with(array(

   	'LoanRepayment'=>array(

                	'select'=>'amount-sum(total_payment)'

     	),

))->findAll(array(

    	'select'=>'loan_id',

    	'group'=>'loan_id ASC'

));



thank you :)