Skip to content

Commit 95d4d60

Browse files
authored
Merge pull request #606 from bohdan-harniuk/entity-web-api-generation
Used a constant in all places where entity id was used as hardcoded value
2 parents 7895d34 + 4949ace commit 95d4d60

File tree

44 files changed

+458
-42
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+458
-42
lines changed

resources/fileTemplates/internal/Magento Delete Entity By Id Command.php.ft

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ class ${CLASS_NAME}
5656
try {
5757
/** @var ${MODEL} $model */
5858
$model = $this->modelFactory->create();
59-
$this->resource->load($model, $entityId, '${ENTITY_ID}');
59+
$this->resource->load($model, $entityId, ${ENTITY_ID_REFERENCE});
6060

61-
if (!$model->getData('${ENTITY_ID}')) {
61+
if (!$model->getData(${ENTITY_ID_REFERENCE})) {
6262
throw new ${NO_SUCH_ENTITY_EXCEPTION}(
6363
__('Could not find ${ENTITY_NAME} with id: `%id`',
6464
[

resources/fileTemplates/internal/Magento Entity Delete Controller Class.php.ft

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class ${CLASS_NAME} extends ${EXTENDS} implements ${IMPLEMENTS_POST}, ${IMPLEMEN
4747
/** @var ${RESULT_INTERFACE} $resultRedirect */
4848
$resultRedirect = $this->resultFactory->create(${RESULT_FACTORY}::TYPE_REDIRECT);
4949
$resultRedirect->setPath('*/*/');
50-
$entityId = (int) $this->getRequest()->getParam('${ENTITY_ID}');
50+
$entityId = (int) $this->getRequest()->getParam(${ENTITY_ID_REFERENCE});
5151

5252
try {
5353
$this->deleteByIdCommand->execute($entityId);

resources/fileTemplates/internal/Magento Entity Save Controller Class.php.ft

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class ${CLASS_NAME} extends ${EXTENDS} implements ${IMPLEMENTS}
7777
$this->dataPersistor->set('entity', $params);
7878

7979
return $resultRedirect->setPath('*/*/edit', [
80-
'${ENTITY_ID}'=> $this->getRequest()->getParam('${ENTITY_ID}')
80+
${ENTITY_ID_REFERENCE} => $this->getRequest()->getParam(${ENTITY_ID_REFERENCE})
8181
]);
8282
}
8383

resources/fileTemplates/internal/Magento Grid Ui Component Action Column Class.php.ft

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ class ${CLASS_NAME} extends ${PARENT_CLASS}
5959
*/
6060
public function prepareDataSource(array $dataSource): array
6161
{
62-
#set($entityIdAccessor = "['" + ${ENTITY_ID} + "']")
62+
#set($entityIdAccessor = "[" + ${ENTITY_ID_REFERENCE} + "]")
6363
if (isset($dataSource['data']['items'])) {
6464
foreach ($dataSource['data']['items'] as &$item) {
6565
if (isset($item$entityIdAccessor)) {
6666
$entityName = static::ENTITY_NAME;
67-
$urlData = ['${ENTITY_ID}' => $item$entityIdAccessor];
67+
$urlData = [${ENTITY_ID_REFERENCE} => $item$entityIdAccessor];
6868

6969
$editUrl = $this->urlBuilder->getUrl(static::EDIT_URL_PATH, $urlData);
7070
$deleteUrl = $this->urlBuilder->getUrl(static::DELETE_URL_PATH, $urlData);

resources/fileTemplates/internal/Magento PHP Form Generic Button Block Class.php.ft

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class ${CLASS_NAME}
4040
*/
4141
public function ${ENTITY_ID_GETTER}(): int
4242
{
43-
return (int) $this->context->getRequest()->getParam('${ENTITY_ID}');
43+
return (int) $this->context->getRequest()->getParam(${ENTITY_ID_REFERENCE});
4444
}
4545

4646
/**

resources/fileTemplates/internal/Magento Resource Model Class.php.ft

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ class ${NAME}#if (${EXTENDS}) extends ${EXTENDS}#end#if (${IMPLEMENTS}) implemen
2121
*/
2222
protected function _construct()
2323
{
24+
#if (${ENTITY_ID_REFERENCE})
25+
$this->_init('${DB_NAME}', ${ENTITY_ID_REFERENCE});
26+
#else
2427
$this->_init('${DB_NAME}', '${ENTITY_ID_COLUMN}');
28+
#end
2529
$this->_useIsObjectNew = true;
2630
}
2731
}

resources/fileTemplates/internal/Magento Save Entity Command Model.php.ft

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class ${CLASS_NAME}
5959
$model->addData($${DTO_PROPERTY}->getData());
6060
$model->setHasDataChanges(true);
6161

62-
if (!$model->getId()) {
62+
if (!$model->getData(${ENTITY_ID_CONST})) {
6363
$model->isObjectNew(true);
6464
}
6565
$this->resource->save($model);

resources/fileTemplates/internal/Magento UI Component Custom Data Provider Class.php.ft

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class ${CLASS_NAME} extends ${EXTENDS}
8787
$result->getItems(),
8888
$result->getTotalCount(),
8989
$searchCriteria,
90-
'#if(${ENTITY_ID})${ENTITY_ID}#{else}entity_id#end'
90+
${ENTITY_ID_REFERENCE}
9191
);
9292
}
9393

@@ -104,14 +104,14 @@ class ${CLASS_NAME} extends ${EXTENDS}
104104
$this->loadedData = parent::getData();
105105
$itemsById = [];
106106

107-
#set($entityId = "#if(${ENTITY_ID})${ENTITY_ID}#{else}entity_id#end")
108-
#set($entityIdAccessor = '[(int) $item[' + "'$entityId'" + ']]')
107+
#set($entityId = "${ENTITY_ID_REFERENCE}")
108+
#set($entityIdAccessor = '[(int) $item[' + "${ENTITY_ID_REFERENCE}" + ']]')
109109
#set($getIdAccessor = "[(int) $id]")
110110
foreach ($this->loadedData['items'] as $item) {
111111
$itemsById$entityIdAccessor = $item;
112112
}
113113

114-
if ($id = $this->request->getParam('$entityId', null)) {
114+
if ($id = $this->request->getParam($entityId)) {
115115
$this->loadedData['entity'] = $itemsById$getIdAccessor;
116116
}
117117

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.context;
7+
8+
import com.intellij.openapi.util.Key;
9+
import com.intellij.openapi.util.UserDataHolderBase;
10+
11+
public class EntityCreatorContext extends UserDataHolderBase implements GenerationContext {
12+
13+
public static final Key<String> DTO_TYPE = Key.create("DTO_TYPE");
14+
public static final Key<String> ENTITY_ID = Key.create("ENTITY_ID");
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.context;
7+
8+
public interface GenerationContext {
9+
}

0 commit comments

Comments
 (0)