Re: [DataObject] many to many relationship
| From: | Justin Patrin | Date: | Wed, 23 Mar 2005 18:12:11 +0000 |
| Subject: | Re: [DataObject] many to many relationship | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-18291@lists.php.net to get a copy of this message | ||
On Wed, 23 Mar 2005 12:45:04 +0100, Toni M. <toni@cargol.net> wrote:
> Hi,
>
> On dc, 2005-03-23 at 09:31 +0800, digit Shine wrote:
> > hi,all
> > How to use getLinks() or getLink() to fetch data from
> > a many to mary relationship?
> >
> > There are 3 tables: ph_team.ph_team_user and ph_user,
> > which user is a member of team.
> > Table ph_team_user has 2 fields: team_id and user_id,
> > link to ph_team and ph_user respectively.
> >
> > my code is:
> >
> > $team->_id->getLinks();
> > while($team->_id->fetch()){
> > $team->_id->getLinks();
> > echo $team->_id->_user_id->username;
> > }
>
> What about doing so:
>
> $team_user->team_id = $t; //if it hasn't value previosly
> $team_user->find();
> while($team_user->fetch()){
> $team_user->getLinks();
> echo $team_user->_user_id->username;
Just going to stick my nose in here. getLinks is inefficient here as
it will get the linked DO for *all* linked records. In this case it
means each getLinks() call is getting two DOs, but this would get
worse if you add any more links. This would be much better:
$user = $team_user->getLink('user_Id');
echo $user->username;
> }
>
> The links.ini file should be diferent:
>
> [ph_team_user]
> team_id = ph_team:id
> user_id = ph_user:id
>
> I hope this is useful for you, but I'm not a veteran of DO ;)
>
> Toni.
>
> > Manually user getLink() will be easy to do. But it can
> > not fetch data into team object, so not good for
> > smarty. Despite of getLinks() waste more resource.
> >
> > Appreciate of any feedback.
> >
--
Justin Patrin