$query = $this->getAdapter()->select()->from(['p' => $this->_name])
            ->joinLeft(['u' => $this->_dbprefix.'users'], 'u.userid=.p.userid', ['name', 'username'])
            ->joinLeft(['pi' => $this->_dbprefix.'gallery_images'], 'pi.id=u.profileImage', ['filename'])
            ->joinLeft(['pc' => $this->getTableName()], 'pc.thread_id=p.thread_id', new \Zend_Db_Expr('COUNT(DISTINCT pc.id) AS postcount'))
            ->where('p.id=?', $postid);