From cd2db76e25ba0d149054690f2031204421f5c8d9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Wac=C5=82awczyk?= Date: Fri, 5 Jul 2024 14:17:42 +0200 Subject: [PATCH] #94738 wip adjust loading customer group assignments --- Model/Menu/Node.php | 4 +- Model/Menu/Node/Image/Node.php | 2 +- Model/Menu/NodeRepository.php | 1 + Model/ResourceModel/Menu/Node.php | 23 ++++ Model/ResourceModel/Menu/Node/Collection.php | 12 +++ etc/db_schema.xml | 104 ++++++++++--------- 6 files changed, 93 insertions(+), 53 deletions(-) diff --git a/Model/Menu/Node.php b/Model/Menu/Node.php index 26d4f234..6da4c34d 100644 --- a/Model/Menu/Node.php +++ b/Model/Menu/Node.php @@ -351,8 +351,8 @@ public function setSelectedItemId($selectedItemId) public function getCustomerGroups() { $customerGroups = $this->_getData(NodeInterface::CUSTOMER_GROUPS); - if (!empty($customerGroups)) { - $customerGroups = $this->serializer->unserialize($customerGroups); + if ($customerGroups !== null) { + $customerGroups = explode(',', $customerGroups); if (is_array($customerGroups) && !empty($customerGroups)) { return $customerGroups; } diff --git a/Model/Menu/Node/Image/Node.php b/Model/Menu/Node/Image/Node.php index 8eef98b0..1ac72231 100644 --- a/Model/Menu/Node/Image/Node.php +++ b/Model/Menu/Node/Image/Node.php @@ -67,7 +67,7 @@ public function updateNodeImage(int $nodeId, ?string $image): void public function getNodeListImages(array $nodeIds): array { $searchCriteria = $this->searchCriteriaBuilder - ->addFilter(NodeInterface::NODE_ID, $nodeIds, 'in') + ->addFilter('main_table.' . NodeInterface::NODE_ID, $nodeIds, 'in') ->addFilter(NodeInterface::IMAGE, true, 'notnull') ->addFilter(NodeInterface::IMAGE, '', 'neq') ->create(); diff --git a/Model/Menu/NodeRepository.php b/Model/Menu/NodeRepository.php index dc69be42..5ef0831f 100644 --- a/Model/Menu/NodeRepository.php +++ b/Model/Menu/NodeRepository.php @@ -15,6 +15,7 @@ use Snowdog\Menu\Model\Menu\NodeFactory; use Snowdog\Menu\Model\ResourceModel\Menu\Node\CollectionFactory; use Magento\Framework\Api\SortOrder; +use Magento\Framework\DB\Sql\Expression; class NodeRepository implements NodeRepositoryInterface { diff --git a/Model/ResourceModel/Menu/Node.php b/Model/ResourceModel/Menu/Node.php index 4f65adc5..cba5a813 100644 --- a/Model/ResourceModel/Menu/Node.php +++ b/Model/ResourceModel/Menu/Node.php @@ -13,6 +13,29 @@ protected function _construct() $this->_init('snowmenu_node', 'node_id'); } + protected function _afterSave(\Magento\Framework\Model\AbstractModel $object) + { + $connection = $this->getConnection(); + $connection->delete('snowmenu_customer', ['node_id = ?' => $object->getNodeId()]); + $currentCustomerGroups = $object->getData('customer_groups'); + if ($currentCustomerGroups && is_string($currentCustomerGroups)) { + $currentCustomerGroups = json_decode($currentCustomerGroups); + } + $insertData = []; + foreach ($currentCustomerGroups as $customerGroup) { + $insertData[] = [ + 'node_id' => $object->getNodeId(), + 'group_id' => $customerGroup + ]; + } + if ($currentCustomerGroups) { + $connection->insertMultiple('snowmenu_customer', $insertData); + } + + return parent::_afterSave($object); // TODO: Change the autogenerated stub + + } + public function getFields(): array { return $this->getConnection()->describeTable($this->getMainTable()); diff --git a/Model/ResourceModel/Menu/Node/Collection.php b/Model/ResourceModel/Menu/Node/Collection.php index dc015074..5ee789f3 100644 --- a/Model/ResourceModel/Menu/Node/Collection.php +++ b/Model/ResourceModel/Menu/Node/Collection.php @@ -1,6 +1,7 @@ getSelect()->joinLeft( + ['customer' => 'snowmenu_customer'], + 'main_table.node_id = customer.node_id', + ['customer_groups' => new Expression('GROUP_CONCAT(group_id SEPARATOR \',\')')] + )->group('main_table.node_id'); +// die ((string) $collection->getSelect()); + } } diff --git a/etc/db_schema.xml b/etc/db_schema.xml index 37469e32..d7330b00 100644 --- a/etc/db_schema.xml +++ b/etc/db_schema.xml @@ -1,53 +1,57 @@ - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - -
+ + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + +
+ + + + +