_getAlbumSelect(); $select->having('photoCount > 0'); $albums = $this->_db->fetchAssoc($select); $noImage = []; foreach ($albums as $album) { if (empty($album['image'])) { $noImage[] = $album['id']; } } if ($noImage) { $columns = ['id', 'image' => new Zend_Db_Expr('(' . $this->_getDefaultAlbumImageSelect() . ')')]; $select = $this->_db->select(); $select->from($this->_getPair(), $columns); $select->where('`g`.`id` IN (?)', $noImage, Qs_Db::INT_TYPE); $images = $this->_db->fetchPairs($select); foreach ($images as $galleryId => $image) { $albums[$galleryId]['image'] = $image; } } return $albums; } protected function _getAlbumSelect() { $photoCountExpr = new Zend_Db_Expr('(' . $this->_getImagesCountSelect(true) . ')'); $columns = ['id', 'alias', 'image', 'title', 'description', 'photoCount' => $photoCountExpr]; $select = $this->_db->select(); $select->from($this->_getPair(), $columns); $select->where("`g`.`enabled` = 'y'"); $select->order("g.sorter"); return $select; } protected function _getDefaultAlbumImageSelect() { $select = $this->_db->select(); $select->from($this->_getPair('GalleryImage', 'gi'), ['image']); $select->where('`gi`.`galleryId` = `g`.`id`'); $select->where('`gi`.`enabled` = "y"'); $select->order('gi.sorter'); $select->limit('1'); return $select; } public function getAlbumItems($id = null) { if (null === $id) { $id = $this->getPrimaryKey(); } $select = $this->_db->select(); $select->from($this->_getPair('GalleryImage', 'gi')); $select->where('`galleryId` = ?', $id, Qs_Db::INT_TYPE); $select->where('`enabled` = "y"'); $select->order('sorter'); return $this->_db->fetchAssoc($select); } protected function _getFromDbSelect($primaryKey) { $select = parent::_getFromDbSelect($primaryKey); $select->where('`g`.`enabled` = "y"'); return $select; } }