Skip to content

Commit 2044297

Browse files
committed
fix save identifier already exists.
1 parent 64053eb commit 2044297

File tree

3 files changed

+53
-31
lines changed

3 files changed

+53
-31
lines changed

Api/Data/TagProductLinkInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
*/
1515
interface TagProductLinkInterface extends ExtensibleDataInterface
1616
{
17+
const KEY_SKU = 'sku';
18+
const KEY_POSITION = 'position';
19+
const KEY_TAG_ID = 'tag_id';
1720
/**
1821
* @return string|null
1922
*/

Controller/Adminhtml/Tag/Save.php

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -54,37 +54,58 @@ public function execute()
5454
/** @var \Lof\ProductTags\Model\Tag $model */
5555
$model = $this->TagFactory->create();
5656
$id = $this->getRequest()->getParam('tag_id');
57-
if ($id) {
58-
try {
59-
$model = $model->load($id);
57+
$identifier = $this->getRequest()->getParam('identifier');
58+
//print_r($identifier);die();
59+
$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); // Instance of object manager
60+
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
61+
$connection = $resource->getConnection();
62+
$tableName = $resource->getTableName('lof_producttags_tag');
63+
$select = $connection->select()->from(
64+
['main_table' => $tableName],
65+
[new \Zend_Db_Expr('COUNT(main_table.identifier)')]
66+
)->where(
67+
'main_table.identifier = :identifier'
68+
);
69+
70+
$bind = ['identifier' => $identifier];
71+
$counts = $connection->fetchOne($select, $bind);
72+
73+
if($counts > 0){
74+
$this->messageManager->addErrorMessage(__('The identifier already exists'));
75+
}
76+
else{
77+
if ($id) {
78+
try {
79+
$model = $model->load($id);
80+
} catch (LocalizedException $e) {
81+
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
82+
return $resultRedirect->setPath('*/*/');
83+
}
84+
}
85+
$model->setData($data);
86+
if (isset($data['tag_products'])
87+
&& is_string($data['tag_products'])) {
88+
$products = json_decode($data['tag_products'], true);
89+
$model->setPostedProducts($products);
90+
}
91+
$this->_eventManager->dispatch(
92+
'lof_producttags_prepare_save',
93+
['tag' => $model, 'request' => $this->getRequest()]
94+
);
95+
$products = $model->getPostedProducts();
96+
try{
97+
$model->save($model);
98+
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
99+
$this->dataPersistor->clear('lof_productags_tag');
100+
return $this->processBlockReturn($model, $data, $resultRedirect);
60101
} catch (LocalizedException $e) {
61-
$this->messageManager->addErrorMessage(__('This tag no longer exists.'));
62-
return $resultRedirect->setPath('*/*/');
102+
$this->messageManager->addErrorMessage($e->getMessage());
103+
} catch (\Exception $e) {
104+
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the tag.'));
63105
}
106+
$this->dataPersistor->set('lof_productags_tag', $data);
107+
return $resultRedirect->setPath('*/*/edit', ['tag_id' => $id]);
64108
}
65-
$model->setData($data);
66-
if (isset($data['tag_products'])
67-
&& is_string($data['tag_products'])) {
68-
$products = json_decode($data['tag_products'], true);
69-
$model->setPostedProducts($products);
70-
}
71-
$this->_eventManager->dispatch(
72-
'lof_producttags_prepare_save',
73-
['tag' => $model, 'request' => $this->getRequest()]
74-
);
75-
$products = $model->getPostedProducts();
76-
try{
77-
$model->save($model);
78-
$this->messageManager->addSuccessMessage(__('You saved the tag.'));
79-
$this->dataPersistor->clear('lof_productags_tag');
80-
return $this->processBlockReturn($model, $data, $resultRedirect);
81-
} catch (LocalizedException $e) {
82-
$this->messageManager->addErrorMessage($e->getMessage());
83-
} catch (\Exception $e) {
84-
$this->messageManager->addExceptionMessage($e, __('Something went wrong while saving the tag.'));
85-
}
86-
$this->dataPersistor->set('lof_productags_tag', $data);
87-
return $resultRedirect->setPath('*/*/edit', ['tag_id' => $id]);
88109
}
89110
return $resultRedirect->setPath('*/*/');
90111
}

Model/Data/TagProductLink.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ class TagProductLink extends \Magento\Framework\Api\AbstractExtensibleObject imp
1515
/**#@+
1616
* Constant for confirmation status
1717
*/
18-
const KEY_SKU = 'sku';
19-
const KEY_POSITION = 'position';
20-
const KEY_TAG_ID = 'tag_id';
18+
2119
/**#@-*/
2220

2321
/**

0 commit comments

Comments
 (0)