-
Notifications
You must be signed in to change notification settings - Fork 248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Issue 1306 rename website breaks grid #1344
Conversation
If a website was renamed/removed, sales channels were still tying to search for it
…te code is changed See #1306
private function getCodeFromDatabase( | ||
\Magento\Store\Model\ResourceModel\Website $resourceModel, | ||
int $websiteId | ||
) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pls,
- use return type hinting
$qry
is not clear name for the variable- Need to use
$this->resource
instead of$resourceModel->getConnection()
,
generally we don't need dependency onResourceModel\Website
for obtaining website code - Also, need to think about extracting this logic to separate class
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used \Magento\Store\Model\ResourceModel\Website
with intention because it can be potentially stored in a different database from the standard one.
$tableName = $this->resource->getTableName('inventory_stock_sales_channel'); | ||
$connection->update($tableName, [ | ||
SalesChannelInterface::CODE => $newCode, | ||
], $connection->quoteInto(SalesChannelInterface::CODE . "=? and type='website'", $oldCode)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Use `\Magento\InventorySalesApi\Api\Data\SalesChannelInterface::TYPE_WEBSITE
- Second parameter can be array with clauses instead of
sql string
if ($object->getId()) { | ||
// Keep database consistency while updating the website code | ||
// See https://github.com/magento-engcom/msi/issues/1306 | ||
$oldCode = $this->getCodeFromDatabase($subject, (int) $object->getId()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bad interface of method
'code' => $code, | ||
]; | ||
} catch (NoSuchEntityException $e) { | ||
// We must handle the scenario in which a previous website was removed/renamed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that is good idea to skip exception... especially if we fixed initial case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, but this is the only way to fix the already done mistake.
int $websiteId | ||
) { | ||
$connection = $resourceModel->getConnection(); | ||
$qry = $connection->select()->from($resourceModel->getMainTable(), 'code')->where('website_id = ?', $websiteId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please don't use shortening in variable names
like $qry
it's better to have long variable names than shortening
* @param \Magento\Store\Model\ResourceModel\Website $subject | ||
* @param callable $proceed | ||
* @param \Magento\Store\Model\Website $object | ||
* @return mixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you have mixed as return type?
you can specify \Magento\Store\Model\ResourceModel\Website
as resource model::save returns this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Phpstorm autocompletition... my fault I did not notice it.
…o around plugin to keep data consistent while chaning the website code on an existing website. This commit is realted to issue #1306
@naydav @maghamed , tests are failing because Magento/Catalog/etc/module.xml:
It happens on this module because the fixture
Should I fix this issue in the core or temporary skip this exception? Skipping the exception in |
private function getCodeFromDatabase(int $websiteId): string | ||
{ | ||
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $connection->getTableName('store_website'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to extract table name via $this->resourceConnection->getTableName()
Because $connection->getTableName
is only wrapper which doesn't take into account table prefix
$connection = $this->resourceConnection->getConnection(); | ||
$tableName = $connection->getTableName('store_website'); | ||
$selectQry = $connection->select()->from($tableName, 'code')->where('website_id = ?', $websiteId); | ||
return (string) $connection->fetchOne($selectQry); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure that we need to return ''
(empty string) if the record was not found (website id is not existing)
More clear is return null in this case
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That method can't return an empty string. If it is called, it is called from aroundSave
and only if the website exists.
Maybe I should change the initialization of $oldCode = '';
to $oldCode = null;
.
And change the if
statement accordingly:
...
if (($oldCode !== null) && ($oldCode !== $newCode)) {
...
use Magento\InventorySales\Model\AssignWebsiteToDefaultStock; | ||
use Magento\InventorySales\Model\ResourceModel\UpdateSalesChannelWebsiteCode; | ||
|
||
class WebsiteResourcePlugin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No bad to have what does the plugin do
in the class name
@@ -6,9 +6,6 @@ | |||
*/ | |||
--> | |||
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> | |||
<event name="website_save_after"> | |||
<observer name="assign_website_to_default_stock" instance="Magento\InventorySales\Observer\Website\AssignWebsiteToDefaultStock"/> | |||
</event> | |||
<event name="website_delete_after"> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like we also need to update delete logic website_delete_after
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An observer after delete seems to be an efficient way and seems to be working, do you prefer to pluginize it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in: #1356
…o correct getTableName method
…and eventual stock_link deletion This commit is related to PR #1344 . The logic was implmented with a plugin on website save and to keep code consistency I decided to apply the same logic to website delete.
1f48ab7
to
fabd833
Compare
…ame-website-breaks-grid
-- fix static test
@phoenix128 Problem is not fixed, please recheck. I changed code(or name) of Main Website and saved it - and 've got fatal error: |
@smoskaluk , seems it is not coming from my original code, but from other commits, let me check it. |
Sorry, It was a mistake from my side |
Fixed Issues (if relevant)
Contribution checklist