2010年8月26日星期四

Re: [fw-db] Difference between JOIN and findDependentRowset

On Aug 26, 2010, at 1:38 PM, milesap wrote:
> So if I need to get the address of a family member, do I use
> findDependentRowset or use the JOIN statement? Could someone briefly
> explain
> when I should use either of them, it would help so much!

Use findDependentRowset() if you have *already* fetched the Account
row and your code subsequently decides it needs to fetch the
FamilyMembers that reference that Account.

$account = $accountTable->find(1234);
//
// ... some time later ...
//
$familyMembers = $account->findDependentRowset();
foreach ($familyMember as $member)
{
// ... do something with each member ...
}

Use an SQL query with a JOIN if you need data for multiple Accounts,
or if you know you'll need the data for an Account and its associated
FamilyMembers at the same time.

$sql = "SELECT * FROM Accounts a JOIN FamilyMembers f ON a.ID =
f.Account_ID";
$stmt = $db->query($sql);
while ($row = $stmt->fetch())
{
// ... do something with joined data ...
}

Regards,
Bill Karwin

没有评论: