If no ORM, it need use this to get all related record
$user=User::findFirstById(3);
echo $user->username. ' - ';
foreach($user->payment as $p)
{
echo $p->amount;
}
$payment=Payment::find('userId='.$user->id);
foreach($payment as $p)
{
echo $p->amount;
}
If I use ORM, I can add this to User
public function initialize()
{
$this->hasMany("id", "Payment", "userId");
}
and I just select once, then I can get all related records
$user=User::findFirstById(3);
echo $user->username. ' - ';
foreach($user->payment as $p)
{
echo $p->amount;
}
Now I have some question
(1.) How to use ORM way to create record? like this
$user=new User();
$user->username='username';
$user->password='password';
$user->payment->amount=1000;
$user->save();
not this
$user=new User();
$user->username='username';
$user->password='password';
$user->save();
$payment=new Payment();
$payment->userId=$user->id;
$payment->payment=1000;
$payment->save();
(2.) I can use index to get the detail record
echo $user->payment[0]->amount;
But this is not work in volt, how to get details without loop in volt?
(3.) How to do some operation with related data when I select main model?
For example:
a. find all user, order by max(payment.amount)
b. find all payment, order by user.id