![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Soon/Faq/Model/ResourceModel/ |
<?php namespace Soon\Faq\Model\ResourceModel; use Magento\Framework\Model\AbstractModel; use Magento\Framework\Model\ResourceModel\Db\AbstractDb; use Soon\Faq\Model\Category as CategoryModel; class Category extends AbstractDb { /** * @var CategoryModel */ private $currentCategory; /** * @var array */ private $storesToInsert = []; /** * @var array */ private $storesToDelete = []; /** * @var array */ private $oldStores = []; /** * @var array */ private $newStores = []; /** * @var null|string */ private $storesTable; protected function _construct() { $this->_init('soon_faq_category', 'id'); } /** * Retrieve store ids * * @param $category * @return array */ public function getStoreIds($category) { $adapter = $this->getConnection(); $select = $adapter->select() ->from($this->getTable('soon_faq_category_store'), 'store_id') ->where('category_id = :category_id'); $binds = [ ':category_id' => (int)$category->getId() ]; return $adapter->fetchCol($select, $binds); } /** * @return CategoryModel */ public function getCurrentCategory() { return $this->currentCategory; } /** * @param CategoryModel $currentCategory */ public function setCurrentCategory(CategoryModel $currentCategory) { $this->currentCategory = $currentCategory; } /** * @param AbstractModel $object * @return AbstractDb */ protected function _afterLoad(AbstractModel $object) { if ($object->getId()) { /** @var CategoryModel $object */ $this->setCurrentCategory($object); $stores = $this->lookupStoreIds(); $object->setData('store_id', $stores); } return parent::_afterLoad($object); } /** * @param AbstractModel $object * @return AbstractDb */ protected function _afterSave(AbstractModel $object) { /** @var CategoryModel $object */ $this->setCurrentCategory($object); $this->handleStoresAssociations(); return parent::_afterSave($object); } /** * Add / remove stores for category */ private function handleStoresAssociations() { $this->populateStoresTable(); $this->populateOldAndNewStores(); $this->findStoresToInsertAndDelete(); $this->saveChangesToDb(); } /** * Find the table where stores are saved */ private function populateStoresTable() { $this->storesTable = $this->getTable('soon_faq_category_store'); } /** * Find existing and new stores for category */ private function populateOldAndNewStores() { $this->oldStores = $this->lookupStoreIds(); $this->newStores = (array)$this->getCurrentCategory()->getData('store_id'); } /** * @return array */ private function lookupStoreIds() { $adapter = $this->getConnection(); $select = $adapter->select() ->from($this->getTable('soon_faq_category_store'), 'store_id') ->where('category_id = :category_id'); $binds = array( ':category_id' => (int)$this->getCurrentCategory()->getId() ); return $adapter->fetchCol($select, $binds); } /** * Find which stores need to be added or removed */ private function findStoresToInsertAndDelete() { $this->storesToInsert = array_diff($this->newStores, $this->oldStores); $this->storesToDelete = array_diff($this->oldStores, $this->newStores); } /** * Actually make changes to the DB */ private function saveChangesToDb() { if (!empty($this->storesToDelete)) { $this->deleteStoresFromDb(); } if (!empty($this->storesToInsert)) { $this->insertStoresInDb(); } } /** * Delete stores category is not attached to */ private function deleteStoresFromDb() { $where = array( 'category_id = ?' => (int)$this->getCurrentCategory()->getId(), 'store_id IN (?)' => $this->storesToDelete ); $this->getConnection()->delete($this->storesTable, $where); } /** * Add stores category is attached to */ private function insertStoresInDb() { $data = array(); foreach ($this->storesToInsert as $storeId) { $data[] = array( 'category_id' => (int)$this->getCurrentCategory()->getId(), 'store_id' => (int)$storeId ); } $this->getConnection()->insertMultiple($this->storesTable, $data); } }