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
Catalog promotion channel pricing relation #13375
Catalog promotion channel pricing relation #13375
Conversation
Rafikooo
commented
Dec 9, 2021
Q | A |
---|---|
Branch? | master |
Bug fix? | no |
New feature? | yes |
BC breaks? | no |
Deprecations? | no |
License | MIT |
|
||
foreach ($channelPricings as $channelPricing) { | ||
$catalogPromotions = array_merge($catalogPromotions, $channelPricing->getAppliedPromotions()->toArray()); |
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 left ->toArray
on purpose although foreach
below can handle array collection objects.
On line 42 I get appliedPromotions and store them in a variable but in the second step, I clear promotions stored in channelPricing object. After that, it occurs that removing catalog promotions inside channelPricing object also affect local variable $catalogPromotions and clear it also. Alternatively I could use instead of ->toArray
clone
function which can be more explicit in the future, that we're dealing with a pointer
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 could write a spec to assure the right behavior
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 ok with this implementation, but if the catalog promotion was deleted this by accident, we should check doctrine mapping, to not delete something by accident. I left a few comments related to cascading on mapping and migration. Can you apply them and check if the error still exists?
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 resolved mapping-related stuff. I'm not sure if we are on the same page with my comment above, because the problem happens in the application state rather than DB.
A moment ago I discovered that I was wrong. Indeed, there is that pointer/reference case, but it is in src/Sylius/Bundle/CoreBundle/Processor/CatalogPromotionClearer.php:42
. After getting ArrayCollection
, storing it in $appliedPromotions
and callingclearChannelPricing
, foreach
is skipped. ->toArray
produces deep copy so foreach
is executed correctly
tests/Api/Responses/Expected/shop/catalog_promotion/get_catalog_promotion_response.json
Show resolved
Hide resolved
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.
Added two notes about the code
I think you should squash part of your commits (30 commits IMO is too many) |
src/Sylius/Bundle/ApiBundle/Serializer/ProductVariantNormalizer.php
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/ApiBundle/spec/Serializer/ProductVariantNormalizerSpec.php
Show resolved
Hide resolved
src/Sylius/Bundle/CoreBundle/spec/Applicator/CatalogPromotionApplicatorSpec.php
Show resolved
Hide resolved
src/Sylius/Bundle/ShopBundle/Resources/private/js/sylius-variants-prices.js
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/ShopBundle/Resources/views/Product/Box/_content.html.twig
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/ShopBundle/Resources/views/Product/Show/_price.html.twig
Outdated
Show resolved
Hide resolved
tests/Api/Responses/Expected/shop/catalog_promotion/get_catalog_promotion_response.json
Show resolved
Hide resolved
src/Sylius/Bundle/ApiBundle/Serializer/ProductVariantNormalizer.php
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/CoreBundle/Doctrine/ORM/ChannelPricingRepository.php
Outdated
Show resolved
Hide resolved
|
||
foreach ($channelPricings as $channelPricing) { | ||
$catalogPromotions = array_merge($catalogPromotions, $channelPricing->getAppliedPromotions()->toArray()); |
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 ok with this implementation, but if the catalog promotion was deleted this by accident, we should check doctrine mapping, to not delete something by accident. I left a few comments related to cascading on mapping and migration. Can you apply them and check if the error still exists?
src/Sylius/Bundle/CoreBundle/Resources/config/doctrine/model/ChannelPricing.orm.xml
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/CoreBundle/Resources/config/doctrine/model/ChannelPricing.orm.xml
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/CoreBundle/Migrations/Version20211129213836.php
Outdated
Show resolved
Hide resolved
src/Sylius/Bundle/CoreBundle/Migrations/Version20211129213836.php
Outdated
Show resolved
Hide resolved
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.
Checked if I applied all review changes
foreach ($this->appliedPromotions as $appliedPromotion) { | ||
if($appliedPromotion->isExclusive()) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; |
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.
really minor thing
foreach ($this->appliedPromotions as $appliedPromotion) { | |
if($appliedPromotion->isExclusive()) { | |
return true; | |
} | |
} | |
return false; | |
return $this->appliedPromotions->exists(function($key, CatalogPromotionInterface $value): bool { | |
return $value->isExclusive(); | |
}); |
or something like this
foreach ($this->appliedPromotions as $appliedPromotion) { | |
if($appliedPromotion->isExclusive()) { | |
return true; | |
} | |
} | |
return false; | |
return $this->appliedPromotions->exists(fn($key, CatalogPromotionInterface $value) => $value->isExclusive()); |
* @var ArrayCollection | ||
* @psalm-var ArrayCollection<array-key, CatalogPromotionInterface> | ||
*/ | ||
protected $appliedPromotions; |
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.
* @var ArrayCollection | |
* @psalm-var ArrayCollection<array-key, CatalogPromotionInterface> | |
*/ | |
protected $appliedPromotions; | |
* @psalm-var ArrayCollection<array-key, CatalogPromotionInterface> | |
*/ | |
protected Collection $appliedPromotions; |
Thank you, Rafał! 🥇 |