_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('`Gallery`.`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', 'photoCount' => $photoCountExpr]; $select = $this->_db->select(); $select->from($this->_getPair('Gallery'), $columns); Qs_Db::joinTranslation( $select, 'Gallery', $this->getLanguage(), 'GalleryContent', array('title', 'description') ); $select->where("`Gallery`.`enabled` = 'y'"); $select->order("Gallery.sorter"); return $select; } protected function _getDefaultAlbumImageSelect() { $select = $this->_db->select(); $select->from($this->_getPair('GalleryImage', 'GalleryImage'), ['image']); $select->where('`GalleryImage`.`galleryId` = `Gallery`.`id`'); $select->where('`GalleryImage`.`enabled` = "y"'); $select->order('GalleryImage.sorter'); $select->limit('1'); return $select; } public function getAllAlbumItems() { return $this->_db->fetchAssoc($this->getAlbumItemsSelect()); } public function getAlbumItems($id = null) { if (null === $id) { $id = $this->getPrimaryKey(); } $select = $this->getAlbumItemsSelect(); $select->where('`galleryId` = ?', $id, Qs_Db::INT_TYPE); return $this->_db->fetchAssoc($select); } protected function getAlbumItemsSelect() { $select = $this->_db->select(); $select->from($this->_getPair('GalleryImage')); Qs_Db::joinTranslation( $select, 'GalleryImage', $this->getLanguage(), 'GalleryImageContent', array('title') ); $select->where('`enabled` = "y"'); $select->order('sorter'); return $select; } protected function _getFromDbSelect($primaryKey) { $select = parent::_getFromDbSelect($primaryKey); $select->where('`Gallery`.`enabled` = "y"'); return $select; } }