Sylius,add/remove/update 相关产品
Sylius, add/remove/update associated product
我正在尝试添加关联产品或替换它(如果存在)。这是我正在使用的代码:
/** @var ProductAssociationInterface $association */
$association = $this->associationFactory->createNew();
/** @var ProductAssociationTypeInterface $associationType */
$associationType = $this->associationTypeRepository->findOneBy(['code' => 'similar_products']);
$association->setType($associationType);
if ($similar_product = $this->productRepository->findOneByCode(trim($row['Similar product']), $this->locale)) {
if (!$association->hasAssociatedProduct($similar_product)) {
$association->addAssociatedProduct($similar_product);
}
if (!$product->hasAssociation($association)) {
$product->addAssociation($association);
$this->associationManager->persist($product);
if (!$this->associationRepository->findOneBy(array('owner' => $similar_product->getId(), 'type' => $associationType->getId()))) {
$this->associationRepository->add($association);
}
};
}
但是如果没有关联产品它工作得很好 - 如果有,或者即使它是相同的产品 - 它会在 "product_association_idx" table 中抛出重复输入错误,我可以'不知道为什么或如何设置检查以查看此产品是否已关联。
非常感谢任何帮助,谢谢
好的,我自己想通了
/** @var ProductAssociationInterface $association */
$association = $this->associationFactory->createNew();
/** @var ProductAssociationTypeInterface $associationType */
$associationType = $this->associationTypeRepository->findOneBy(['code' => 'similar_products']);
$association->setType($associationType);
if ($similar_product = $this->productRepository->findOneByCode(trim($row['Similar product']), $this->locale)) {
$flag = true;
foreach($product->getAssociations() as $productAssociation) {
if ($productAssociation->hasAssociatedProduct($similar_product)) {
$flag = false;
}
}
if ($flag) {
$association->addAssociatedProduct($similar_product);
$product->addAssociation($association);
$this->associationManager->persist($product);
if (!$this->associationRepository->findOneBy(array('owner' => $similar_product->getId(), 'type' => $associationType->getId()))) {
$this->associationRepository->add($association);
}
};
}
我正在尝试添加关联产品或替换它(如果存在)。这是我正在使用的代码:
/** @var ProductAssociationInterface $association */
$association = $this->associationFactory->createNew();
/** @var ProductAssociationTypeInterface $associationType */
$associationType = $this->associationTypeRepository->findOneBy(['code' => 'similar_products']);
$association->setType($associationType);
if ($similar_product = $this->productRepository->findOneByCode(trim($row['Similar product']), $this->locale)) {
if (!$association->hasAssociatedProduct($similar_product)) {
$association->addAssociatedProduct($similar_product);
}
if (!$product->hasAssociation($association)) {
$product->addAssociation($association);
$this->associationManager->persist($product);
if (!$this->associationRepository->findOneBy(array('owner' => $similar_product->getId(), 'type' => $associationType->getId()))) {
$this->associationRepository->add($association);
}
};
}
但是如果没有关联产品它工作得很好 - 如果有,或者即使它是相同的产品 - 它会在 "product_association_idx" table 中抛出重复输入错误,我可以'不知道为什么或如何设置检查以查看此产品是否已关联。
非常感谢任何帮助,谢谢
好的,我自己想通了
/** @var ProductAssociationInterface $association */
$association = $this->associationFactory->createNew();
/** @var ProductAssociationTypeInterface $associationType */
$associationType = $this->associationTypeRepository->findOneBy(['code' => 'similar_products']);
$association->setType($associationType);
if ($similar_product = $this->productRepository->findOneByCode(trim($row['Similar product']), $this->locale)) {
$flag = true;
foreach($product->getAssociations() as $productAssociation) {
if ($productAssociation->hasAssociatedProduct($similar_product)) {
$flag = false;
}
}
if ($flag) {
$association->addAssociatedProduct($similar_product);
$product->addAssociation($association);
$this->associationManager->persist($product);
if (!$this->associationRepository->findOneBy(array('owner' => $similar_product->getId(), 'type' => $associationType->getId()))) {
$this->associationRepository->add($association);
}
};
}