Item.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Wishlist item model resource
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Wishlist\Model\ResourceModel;
  12. /**
  13. * @api
  14. * @since 100.0.2
  15. */
  16. class Item extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  17. {
  18. /**
  19. * Initialize connection and define main table
  20. *
  21. * @return void
  22. */
  23. protected function _construct()
  24. {
  25. $this->_init('wishlist_item', 'wishlist_item_id');
  26. }
  27. /**
  28. * Load item by wishlist, product and shared stores
  29. *
  30. * @param \Magento\Wishlist\Model\Item $object
  31. * @param int $wishlistId
  32. * @param int $productId
  33. * @param array $sharedStores
  34. * @return $this
  35. */
  36. public function loadByProductWishlist($object, $wishlistId, $productId, $sharedStores)
  37. {
  38. $connection = $this->getConnection();
  39. $storeWhere = $connection->quoteInto('store_id IN (?)', $sharedStores);
  40. $select = $connection->select()->from(
  41. $this->getMainTable()
  42. )->where(
  43. 'wishlist_id=:wishlist_id AND ' . 'product_id=:product_id AND ' . $storeWhere
  44. );
  45. $bind = ['wishlist_id' => $wishlistId, 'product_id' => $productId];
  46. $data = $connection->fetchRow($select, $bind);
  47. if ($data) {
  48. $object->setData($data);
  49. }
  50. $this->_afterLoad($object);
  51. return $this;
  52. }
  53. /**
  54. * {@inheritdoc}
  55. */
  56. public function save(\Magento\Framework\Model\AbstractModel $object)
  57. {
  58. $hasDataChanges = $object->hasDataChanges();
  59. $object->setIsOptionsSaved(false);
  60. $result = parent::save($object);
  61. if ($hasDataChanges && !$object->isOptionsSaved()) {
  62. $object->saveItemOptions();
  63. }
  64. return $result;
  65. }
  66. }