Re: getLinks from an M-N jointure : multiple records ?
| From: | Justin Patrin | Date: | Mon, 18 Jul 2005 16:51:44 +0000 |
| Subject: | Re: getLinks from an M-N jointure : multiple records ? | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-20039@lists.php.net to get a copy of this message | ||
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