2008年10月14日星期二

Re: [fw-db] zend db table relationships

On Oct 14, 2008, at 3:30 AM, Václav Vaník wrote:

>
> could you help me how to fetch users from same company if I know
> userId?
>
> SELECT u.* FROM users u
> JOIN companies c ON u.companyId = c.id
> JOIN users u2 ON u2.companyId = c.id
> WHERE u2.id = $userId

I would honestly use the SQL query you show above.

To do it with the Table interface:

$user = new User();
$usersFromSameCompany = $user->find($userId)->current()
->findParentRow('Company')
->findDependentRowset('User');

(this is not tested)

Regards,
Bill Karwin

没有评论: