Re: DBDataObject backlinks
| From: | Justin Patrin | Date: | Mon, 03 Oct 2005 17:27:36 +0000 |
| Subject: | Re: DBDataObject backlinks | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-21208@lists.php.net to get a copy of this message | ||
On 10/3/05, Alessandro Pasotti <ale.pas@tiscali.it> wrote:
> Hello,
>
> I have another question:
>
> is there a way with DBDataObject to easily filter a dataset given a value in a
> (back)linked table?
Sure. Use joinAdd().
>
> I mean:
>
> [person] [department] [dept_pers]
> pers_id PK dept_id PK pers_id FK
> name name dept_id FK
>
>
> I want to get all all persons in a given dept.
>
> I know DBDataObject handles easily direct joins, but how to do in this case?
> SQL would probably be
>
> select person.* from person, dept_pers where dept_pers.dept_id = 1 and
> dept_pers.pers_id = person.pers_id;
Yes, joinAdd() is the right way to do this.
$dept_pers = DB_DataObject::factory('dept_pers');
$pers = DB_DataObject::factory('person');
$dept_pers->dept_id = 1;
$dept_pers->joinAdd($pers);
$dept_pers->selectAdd();
$dept_pers->selectAdd('person.*');
if ($dept_pers->find()) {
while ($dept_pers->fetch()) {
//do something
}
}
>
> The problem is that the modules I'm working on are "table" oriented, i.e. the
> DataObject class I'm working in this case would be "person" and not
> "dept_pers", is there a way to get the tables that link _to_ a given table?
>
No, not directly. It is possible to loop through the global options
set up by DB_DO, but this isn't needed since you already know what you
want to join and how.
--
Justin Patrin