Dailydeal.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Smartwave\Dailydeals\Model\ResourceModel;
  3. class Dailydeal extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  4. {
  5. /**
  6. * Date time handler
  7. *
  8. * @var \Magento\Framework\Stdlib\DateTime
  9. */
  10. protected $dateTime;
  11. /**
  12. * Date model
  13. *
  14. * @var \Magento\Framework\Stdlib\DateTime\DateTime
  15. */
  16. protected $date;
  17. /**
  18. * constructor
  19. *
  20. * @param \Magento\Framework\Stdlib\DateTime $dateTime
  21. * @param \Magento\Framework\Stdlib\DateTime\DateTime $date
  22. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  23. */
  24. public function __construct(
  25. \Magento\Framework\Stdlib\DateTime $dateTime,
  26. \Magento\Framework\Stdlib\DateTime\DateTime $date,
  27. \Magento\Framework\Model\ResourceModel\Db\Context $context
  28. ) {
  29. $this->dateTime = $dateTime;
  30. $this->date = $date;
  31. parent::__construct($context);
  32. }
  33. /**
  34. * Initialize resource model
  35. *
  36. * @return void
  37. */
  38. protected function _construct()
  39. {
  40. $this->_init('sw_dailydeals_dailydeal', 'dailydeal_id');
  41. }
  42. /**
  43. * Retrieves Dailydeal Product Sku from DB by passed id.
  44. *
  45. * @param string $id
  46. * @return string|bool
  47. */
  48. public function getDailydealSw_product_skuById($id)
  49. {
  50. $adapter = $this->getConnection();
  51. $select = $adapter->select()
  52. ->from($this->getMainTable(), 'sw_product_sku')
  53. ->where('dailydeal_id = :dailydeal_id');
  54. $binds = ['dailydeal_id' => (int)$id];
  55. return $adapter->fetchOne($select, $binds);
  56. }
  57. /**
  58. * before save callback
  59. *
  60. * @param \Magento\Framework\Model\AbstractModel|\Smartwave\Dailydeals\Model\Dailydeal $object
  61. * @return $this
  62. */
  63. protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
  64. {
  65. $object->setUpdatedAt($this->date->date());
  66. if ($object->isObjectNew()) {
  67. $object->setCreatedAt($this->date->date());
  68. }
  69. foreach (['sw_date_from', 'sw_date_to'] as $field) {
  70. $value = !$object->getData($field) ? null : $object->getData($field);
  71. $object->setData($field, $this->dateTime->formatDate($value));
  72. }
  73. return parent::_beforeSave($object);
  74. }
  75. }