Skip to content

Commit

Permalink
MC-17147: Integrity constraint violation: 1452 Cannot add or update a…
Browse files Browse the repository at this point in the history
… child row: a foreign key constraint fails
  • Loading branch information
rganin committed Jul 25, 2019
1 parent a242f2d commit fa604dc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 11 deletions.
35 changes: 33 additions & 2 deletions app/code/Magento/Catalog/Model/Product/Compare/ListCompare.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
*/
namespace Magento\Catalog\Model\Product\Compare;

use Magento\Catalog\Model\ProductRepository;
use Magento\Catalog\Model\ResourceModel\Product\Compare\Item\Collection;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\NoSuchEntityException;

/**
* Product Compare List Model
Expand Down Expand Up @@ -51,6 +54,11 @@ class ListCompare extends \Magento\Framework\DataObject
*/
protected $_compareItemFactory;

/**
* @var ProductRepository
*/
private $productRepository;

/**
* Constructor
*
Expand All @@ -60,20 +68,23 @@ class ListCompare extends \Magento\Framework\DataObject
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Customer\Model\Visitor $customerVisitor
* @param array $data
* @param ProductRepository|null $productRepository
*/
public function __construct(
\Magento\Catalog\Model\Product\Compare\ItemFactory $compareItemFactory,
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item\CollectionFactory $itemCollectionFactory,
\Magento\Catalog\Model\ResourceModel\Product\Compare\Item $catalogProductCompareItem,
\Magento\Customer\Model\Session $customerSession,
\Magento\Customer\Model\Visitor $customerVisitor,
array $data = []
array $data = [],
ProductRepository $productRepository = null
) {
$this->_compareItemFactory = $compareItemFactory;
$this->_itemCollectionFactory = $itemCollectionFactory;
$this->_catalogProductCompareItem = $catalogProductCompareItem;
$this->_customerSession = $customerSession;
$this->_customerVisitor = $customerVisitor;
$this->productRepository = $productRepository ?: ObjectManager::getInstance()->create(ProductRepository::class);
parent::__construct($data);
}

Expand All @@ -82,6 +93,7 @@ public function __construct(
*
* @param int|\Magento\Catalog\Model\Product $product
* @return $this
* @throws \Exception
*/
public function addProduct($product)
{
Expand All @@ -90,14 +102,33 @@ public function addProduct($product)
$this->_addVisitorToItem($item);
$item->loadByProduct($product);

if (!$item->getId() && $item->getProductId()) {
if (!$item->getId() && $this->productExists($product)) {
$item->addProductData($product);
$item->save();
}

return $this;
}

/**
* Check product exists.
*
* @param int|\Magento\Catalog\Model\Product $product
* @return bool
*/
private function productExists($product)
{
if ($product instanceof \Magento\Catalog\Model\Product && $product->getId()) {
return true;
}
try {
$product = $this->productRepository->getById((int)$product);
return !empty($product->getId());
} catch (NoSuchEntityException $e) {
return false;
}
}

/**
* Add products to compare list
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

namespace Magento\Catalog\Model\Product\Compare;

/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
class ListCompareTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -45,7 +41,8 @@ protected function tearDown()
}

/**
* @magentoDataFixture Magento/Catalog/_files/second_product_simple.php
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testAddProductWithSession()
{
Expand All @@ -58,13 +55,29 @@ public function testAddProductWithSession()
$product2 = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->create(\Magento\Catalog\Model\Product::class)
->load(6);
$this->_model->addProducts([$product->getId(), $product2->getId(), 'none', 99]);
$products = [$product->getId(), $product2->getId()];
$this->_model->addProducts($products);

$this->assertTrue($this->_model->hasItems(1, $this->_visitor->getId()));
$this->assertTrue($this->_model->hasItems(6, $this->_visitor->getId()));
$this->assertFalse($this->_model->hasItems('none', $this->_visitor->getId()));
$this->assertFalse($this->_model->hasItems(99, $this->_visitor->getId()));
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testAddProductWithSessionNeg()
{
$this->_session->setCustomerId(1);
$products = ['none', 99];
$this->_model->addProducts($products);

$this->assertFalse($this->_model->hasItems(1, $this->_visitor->getId()));
}

/**
* @magentoDataFixture Magento/Catalog/_files/product_simple.php
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testAddProductWithoutSession()
{
/** @var $product \Magento\Catalog\Model\Product */
Expand Down

0 comments on commit fa604dc

Please sign in to comment.