2008年8月6日星期三

Re: [fw-db] Fetchrow of tableX with a Fetchall of tableY

ZF-5 wrote:
>
> I'm trying to fetch a row from 1 table X and then fetch all the rows
> from another table Y
> and then i want keep on doing that until i've had all rows in table X
> (matching a where) and have all the matching rows in Y.
> Then i want to put that all in an array and push it to the view.
>

CREATE TABLE x (
x_id SERIAL PRIMARY KEY
);

CREATE TABLE y (
y_id SERIAL PRIMARY KEY,
x_id BIGINT UNSIGNED REFERENCES x
);

class X extends Zend_Db_Table_Abstract { }

class Y extends Zend_Db_Table_Abstract
{
protected $_referenceMap = array(
'X' => array(
'columns' => 'x_id',
'refTableClass' => 'X'
)
);
}

$xTable = new X();
$xRowset = $xTable->fetchAll();
$data = array();
foreach ($xRowset as $xRow) {
$yRowset = $xRow->findY();
$x = $xRow->toArray();
$x['Y'] = $yRowset->toArray();
$data[] = $x;
}

After this loop, $data should be an array containing all the data you want.

Read http://framework.zend.com/manual/en/zend.db.table.relationships.html
for more information.

Regards,
Bill Karwin
--
View this message in context: http://www.nabble.com/Fetchrow-of-tableX-with-a-Fetchall-of-tableY-tp18858982p18862038.html
Sent from the Zend DB mailing list archive at Nabble.com.

没有评论: