Yii Framework Forum: Ar: Get Value From Relations - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Ar: Get Value From Relations Rate Topic: -----

#1 User is offline   Hermans 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 17-February 11

Posted 18 January 2014 - 01:44 AM

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".


2. How to get Total Repayment ?
in sql in can do "select sum(total_payment) as total_payment from LoanRepayment".


3. 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
0

#2 User is offline   Shahcheraghean 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 478
  • Joined: 12-December 12
  • Location:Mashhad, IRAN

Posted 18 January 2014 - 12:13 PM

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'
));

www.shgn.ir
www.shgn.ir/yii-framework/
yii2.swsco.ir
In a world without any fences or walls,Who needs Gates or Windows!
1

#3 User is offline   Hermans 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 17-February 11

Posted 19 January 2014 - 10:49 PM

thank you :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users