How to join two tables in Zend and read the data

I have the following query:

$usersTable = new Users();

$rowset = $usersTable->select()
    ->setIntegrityCheck(false)
    ->where( 'id = ?', $userId )
    ->join( 'Books', 'Books.userid = Users.id' );

However, I can’t understand for life how to read the resulting set of lines (a list of books related to the user).

Can I do the following?

foreach ($book in $rowset->Books) {
    print_r($book["book_name"]);
}
+3
source share
2 answers

What you call $ rowset is actually a sql expression. This should work:

$usersTable = new Users();

$sql = $usersTable->select()
    ->setIntegrityCheck(false)
    ->where( 'id = ?', $userId )
    ->join( 'Books', 'Books.userid = Users.id' );
$rowset = $usersTable->getAdapter()->fetchAll($sql);
0
source

Try something like:

$sql = $this->select()
->setIntegrityCheck(false)
->where( 'id = ?', $userId )
->join( 'Books', 'Books.userid = Users.id' )
);
$rowset = $this->fetchRow($sql);
foreach ($rowset as $book) {
    print_r($book["book_name"]);
}

Does this happen in your model?

0
source

Source: https://habr.com/ru/post/1775833/


All Articles