Re: DB_DataObject linking 3 tables to make an N - N relation.

From: Date: Fri, 21 Jan 2005 20:22:48 +0000
Subject: Re: DB_DataObject linking 3 tables to make an N - N relation.
References: 1 2  Groups: php.pear.general 
Request: Send a blank email to pear-general+get-17091@lists.php.net to get a copy of this message
Justin Patrin, el viernes 21 de enero a las 12:02 me escribiste: > On Fri, 21 Jan 2005 16:41:36 -0300, luca@llucax.hn.org > <luca@llucax.hn.org> wrote: > > Hi! I'm relative new to DB_DataObject and I have a simple database with > > an N to N relation, something like: > > > > table1 table2 table1_table2 > > ------ ------ ------------- > > id1 id2 id1 > > stuff1 stuff2 id2 > > > > I'd like to get all table1 items that belongs to a table2 item (eventually > > adding a filter, like stuff1 > 5, but this is secondary). Is there any way > > of doing this without messing up with SQL? > > I can't find it anywhere and I think is a relative common case... > > > > Any suggestions are welcome! > > You can joinAdd the N-N table to the table1, then set up the filter on > table2 (set the member) and joinAdd that to the N-N table. > > Or, rather, yould joinAdd table2 to the N-N and then joinAdd the N-N to table1. Ok, that worked, what I've done is something like: $t1 = DB_DataObject::factory('table1'); $t1->stuff1 = 35; $t2 = DB_DataObject::factory('table2'); $t2->id2 = 55; $t1_t2 = DB_DataObject::factory('table1_table2'); $t1_t2->selectAs(); $t1_t2->joinAdd($t1); $t1_t2->joinAdd($t2); $t1_t2->find(); $t1s_for_t2_55 = array(); while ($t1_t2->fetch()) { $t1->get($t1_t2->id1); $t1s_for_t2_55[] = $t1; } // I have my array of t1 elements: $t1s_for_t2_55 I was looking for some links.ini magick though =) -- LUCA - Leandro Lucarella - JID: luca(en)lugmen.org.ar - Debian GNU/Linux .------------------------------------------------------------------------, \ GPG: B08B4FB2 // D9E1 4545 0F4B 7928 E82C 375D 4B02 0FE0 B08B 4FB2 / '--------------------------------------------------------------------' Hey you, dont help them to bury the light Don't give in without a fight.

« previous php.pear.general (#17091) next »