AbstractResource.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\ProductAlert\Model\ResourceModel;
  7. use Magento\Framework\Model\AbstractModel;
  8. /**
  9. * Product alert for back in abstract resource model
  10. *
  11. * @author Magento Core Team <core@magentocommerce.com>
  12. */
  13. abstract class AbstractResource extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * Retrieve alert row by object parameters
  17. *
  18. * @param AbstractModel $object
  19. * @return array|false
  20. */
  21. protected function _getAlertRow(AbstractModel $object)
  22. {
  23. $connection = $this->getConnection();
  24. if ($this->isExistAllBindIds($object)) {
  25. $select = $connection->select()->from(
  26. $this->getMainTable()
  27. )->where(
  28. 'customer_id = :customer_id'
  29. )->where(
  30. 'product_id = :product_id'
  31. )->where(
  32. 'website_id = :website_id'
  33. )->where(
  34. 'store_id = :store_id'
  35. );
  36. $bind = [
  37. ':customer_id' => $object->getCustomerId(),
  38. ':product_id' => $object->getProductId(),
  39. ':website_id' => $object->getWebsiteId(),
  40. ':store_id' => $object->getStoreId()
  41. ];
  42. return $connection->fetchRow($select, $bind);
  43. }
  44. return false;
  45. }
  46. /**
  47. * Is exists all bind ids.
  48. *
  49. * @param AbstractModel $object
  50. * @return bool
  51. */
  52. private function isExistAllBindIds(AbstractModel $object): bool
  53. {
  54. return ($object->getCustomerId()
  55. && $object->getProductId()
  56. && $object->getWebsiteId()
  57. && $object->getStoreId());
  58. }
  59. /**
  60. * Load object data by parameters
  61. *
  62. * @param AbstractModel $object
  63. * @return $this
  64. */
  65. public function loadByParam(AbstractModel $object)
  66. {
  67. $row = $this->_getAlertRow($object);
  68. if ($row) {
  69. $object->setData($row);
  70. }
  71. return $this;
  72. }
  73. /**
  74. * Delete all customer alerts on website
  75. *
  76. * @param AbstractModel $object
  77. * @param int $customerId
  78. * @param int $websiteId
  79. * @return $this
  80. * @SuppressWarnings(PHPMD.UnusedFormalParameter)
  81. */
  82. public function deleteCustomer(AbstractModel $object, $customerId, $websiteId = null)
  83. {
  84. $connection = $this->getConnection();
  85. $where = [];
  86. $where[] = $connection->quoteInto('customer_id=?', $customerId);
  87. if ($websiteId) {
  88. $where[] = $connection->quoteInto('website_id=?', $websiteId);
  89. }
  90. $connection->delete($this->getMainTable(), $where);
  91. return $this;
  92. }
  93. }