Re: DB_DataObject: one-to-many with getLinks()
| From: | Justin Patrin | Date: | Tue, 06 Sep 2005 17:02:58 +0000 |
| Subject: | Re: DB_DataObject: one-to-many with getLinks() | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-20822@lists.php.net to get a copy of this message | ||
On 9/5/05, Patrick Taylor <kaylum@earthlink.net> wrote:
> hello
>
> i would like to use the getLinks() method on a one-to-many table, is this possible?
>
> at this time, i am only retrieving one row although there are two records associated with the
> referenced (foreign key).
>
> i'm sure i could create a work-around by using a subsequent (object->find()), but
> i'm asking before doing that. :)
>
First off, getLinks() is generally a bad thing to use. If you don't
use all of the link records it's a waste of queries. It's much better
to use getLink() on only the links that you're planning on using. (or
to do a joinAdd() to do it all in one query).
getLinks() simply called getLink() multiple times. getLink() gets
*one* record linked to by a single field in the current DO. What
you're looking at is the reverse. Given a record, get all records in a
different table which link back to it. In FormBuilder this is called a
reverseLink. No, there is no built-in way to do this in DB_DO and
there really shouldn't be. It's a simple matter of:
$g =& DB_DataObject::factory('gender');
$g->get('name', 'male');
$p =& DB_DataObject::factory('person');
$p->gender_id = $g->id;
if ($p->find()) {
while ($p->fetch()) {
echo $p->name.' is male<br/>';
}
}
--
Justin Patrin