0

I build a function

public function getBannedByLogin($commentId)
    {
    $sql = $this->getDbAdapter()->select()
            ->from(array('comments' => 'comments'), array())
            ->join(array('users' => 'qengine_users'),
            'comments.bannedBy = users.userId',
            array())
            ->where('commentId = ?', $commentId)
        ;
        $row = $this->fetchRow($sql);
        return $row['login'];

    }

And there are problems, that does'nt work! :D Let's I explain you. Column 'bannedBy' from comments returns id of user, who give a ban. I need to join this with table users to load a login field. Where i have mistakes?

1 Answer 1

0

I assume the code works in the sense of not throwing an exception. If so, your code is OK, you just specifically tell Zend_Db not to select any columns.

public function getBannedByLogin($commentId)
{
$sql = $this->getDbAdapter()->select()
        ->from(array('comments' => 'comments'))
        ->join(array('users' => 'qengine_users'),
        'comments.bannedBy = users.userId')
        ->where('commentId = ?', $commentId)
    ;
    $row = $this->fetchRow($sql);
    return $row['login'];

}

The last argument to from() and join() functions is an array of columns you wish to select. If you pass in an empty array, no columns are selected. No argument = select everything. You can, of course, specify only the columns you need too.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.