Code.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GoogleOptimizer\Model\ResourceModel;
  7. /**
  8. * Google Experiment Code resource model
  9. *
  10. * @api
  11. * @since 100.0.2
  12. */
  13. class Code extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  14. {
  15. /**
  16. * Resource initialization
  17. *
  18. * @return void
  19. */
  20. protected function _construct()
  21. {
  22. $this->_init('googleoptimizer_code', 'code_id');
  23. }
  24. /**
  25. * Load scripts by entity and store
  26. *
  27. * @param \Magento\GoogleOptimizer\Model\Code $object
  28. * @param int $entityId
  29. * @param string $entityType
  30. * @param int $storeId
  31. * @return $this
  32. */
  33. public function loadByEntityType($object, $entityId, $entityType, $storeId)
  34. {
  35. $connection = $this->getConnection();
  36. $select = $connection->select()->from(
  37. ['t_def' => $this->getMainTable()],
  38. ['entity_id', 'entity_type', 'experiment_script', 'code_id']
  39. )->where(
  40. 't_def.entity_id=?',
  41. $entityId
  42. )->where(
  43. 't_def.entity_type=?',
  44. $entityType
  45. )->where(
  46. 't_def.store_id IN (0, ?)',
  47. $storeId
  48. )->order(
  49. 't_def.store_id DESC'
  50. )->limit(
  51. 1
  52. );
  53. $data = $connection->fetchRow($select);
  54. if ($data) {
  55. $object->setData($data);
  56. }
  57. $this->_afterLoad($object);
  58. return $this;
  59. }
  60. }