RE: [PEAR] getLinks from an M-N jointure : multiple records ?
| From: | David Alia | Date: | Mon, 18 Jul 2005 22:53:43 +0000 |
| Subject: | RE: [PEAR] getLinks from an M-N jointure : multiple records ? | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-20048@lists.php.net to get a copy of this message | ||
Thanks a lot for this code.
Maybe the question is regularly asked because there are no examples in the
documentation ?
I have not looked at FormBuilder yet, but maybe it would be the time...
David
-----Message d'origine-----
De : Justin Patrin [mailto:papercrane@gmail.com]
Envoyé : lundi 18 juillet 2005 18:52
À : David Alia
Cc : pear-general@lists.php.net
Objet : Re: [PEAR] getLinks from an M-N jointure : multiple records ?
On 7/18/05, David Alia <david.alia@gmail.com> wrote:
> Hello all,
>
> Beginning with DataObjects, I've succeeded in using getLink and
> getLinks features when tables are joined with a single key so far.
> I mean (table names are uppercase) :
>
[snip]
This is what is called a crossLink in the DB_DataObject_FormBuilder package.
There is no way in DB_DataObject to get these connections with a single
funciton call like getLink. However this is still very easy. I'm actually
very surprised at how often this question seems to be asked...
$person =& DB_DataObject::factory('person'); $person->get(13);
$group_connect =& DB_DataObject::factory('persgroup');
$group_connect->personID = $person->ID;
if ($group_connect->find()) {
while ($group_connect->fetch()) {
$group =& $group_connect->getLink('groupID');
echo $group->name.'<br/>';
}
}
Or alternatively, you can use joinAdd to reduce your queries:
$person =& DB_DataObject::factory('person'); $person->get(13);
$group_connect =& DB_DataObject::factory('persgroup');
$group_connect->personID = $person->ID;
$group =& DB_DataObject::factory('group'); $group_connect->joinAdd($group);
if ($group_connect->find()) {
while ($group_connect->fetch()) {
echo $group_connect->name.'<br/>';
}
}
Or even further:
$group_connect =& DB_DataObject::factory('persgroup');
$person =& DB_DataObject::factory('person'); $person->ID = 13;
$group_connect->joinAdd($person); $group =& DB_DataObject::factory('group');
$group_connect->joinAdd($group); $group_connect->selectAdd(); //technically
these should be quoted with DB::quoteIdentifier()
$group_connect->selectAdd('group.name');
if ($group_connect->find()) {
while ($group_connect->fetch()) {
echo $group_connect->name.'<br/>';
}
}
--
Justin Patrin