Best way to make Inner Join using the Zend Framework?

There seem to be several different ways to join the two tables using the Zend Framework, but I have never done this before, so I don’t know which one is best to do.

This is what I'm trying to do ...

I have 3 tables in my database:

users
    ( id , name )
groups
    ( id , name )
group_members
    ( id , group_id , user_id )

I am trying to find the groups the user belongs to and display them for the user. This SQL statement pretty much does the job (although there may be a better way to write it). It returns only those columns that are associated with group identifiers and name.

    SELECT groups.id, groups.title
    FROM group_members
        INNER JOIN groups
        ON groups.id = group_members.group_id
    WHERE user_id = $userId

How can I do this using the Zend Framework?

+5
2

, . , , .

$db = Zend_Db_Table::getDefaultAdapter(); //set in my config file
$select = new Zend_Db_Select($db);
$select->from('groups', array('id', 'title')) //the array specifies which columns I want returned in my result set
    ->joinInner(
        'group_members',
        'groups.id = group_members.group_id',
        array()) //by specifying an empty array, I am saying that I don't care about the columns from this table
    ->where('user_id = ?', $userId);
$resultSet = $db->fetchAll($select);

id title. array() , . - .

foreach ($resultSet as $row) {
    //do something with $row->id or $row->title
}
+9

Join, Zend_Db_Table MVC. , β„– 10 Filip ( , "Gate Data Gateway"?)

0

All Articles