Option.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Eav\Model\ResourceModel\Entity\Attribute;
  7. /**
  8. * Entity attribute option resource model
  9. *
  10. * @author Magento Core Team <core@magentocommerce.com>
  11. */
  12. class Option extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  13. {
  14. /**
  15. * Resource initialization
  16. *
  17. * @return void
  18. * @codeCoverageIgnore
  19. */
  20. protected function _construct()
  21. {
  22. $this->_init('eav_attribute_option', 'option_id');
  23. }
  24. /**
  25. * Add Join with option value for collection select
  26. *
  27. * @param \Magento\Eav\Model\Entity\Collection\AbstractCollection $collection
  28. * @param \Magento\Eav\Model\Entity\Attribute $attribute
  29. * @param \Zend_Db_Expr $valueExpr
  30. * @return $this
  31. */
  32. public function addOptionValueToCollection($collection, $attribute, $valueExpr)
  33. {
  34. $connection = $this->getConnection();
  35. $attributeCode = $attribute->getAttributeCode();
  36. $optionTable1 = $attributeCode . '_option_value_t1';
  37. $optionTable2 = $attributeCode . '_option_value_t2';
  38. $tableJoinCond1 = "{$optionTable1}.option_id={$valueExpr} AND {$optionTable1}.store_id=0";
  39. $tableJoinCond2 = $connection->quoteInto(
  40. "{$optionTable2}.option_id={$valueExpr} AND {$optionTable2}.store_id=?",
  41. $collection->getStoreId()
  42. );
  43. $valueExpr = $connection->getCheckSql(
  44. "{$optionTable2}.value_id IS NULL",
  45. "{$optionTable1}.option_id",
  46. "{$optionTable2}.option_id"
  47. );
  48. $collection->getSelect()->joinLeft(
  49. [$optionTable1 => $this->getTable('eav_attribute_option_value')],
  50. $tableJoinCond1,
  51. []
  52. )->joinLeft(
  53. [$optionTable2 => $this->getTable('eav_attribute_option_value')],
  54. $tableJoinCond2,
  55. [$attributeCode => $valueExpr]
  56. );
  57. return $this;
  58. }
  59. /**
  60. * Retrieve Select for update Flat data
  61. *
  62. * @param \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute
  63. * @param int $store
  64. * @param bool $hasValueField flag which require option value
  65. * @return \Magento\Framework\DB\Select
  66. */
  67. public function getFlatUpdateSelect(
  68. \Magento\Eav\Model\Entity\Attribute\AbstractAttribute $attribute,
  69. $store,
  70. $hasValueField = true
  71. ) {
  72. $connection = $this->getConnection();
  73. $attributeTable = $attribute->getBackend()->getTable();
  74. $attributeCode = $attribute->getAttributeCode();
  75. $joinConditionTemplate = "%s.entity_id = %s.entity_id" .
  76. " AND %s.entity_type_id = " .
  77. $attribute->getEntityTypeId() .
  78. " AND %s.attribute_id = " .
  79. $attribute->getId() .
  80. " AND %s.store_id = %d";
  81. $joinCondition = sprintf(
  82. $joinConditionTemplate,
  83. 'e',
  84. 't1',
  85. 't1',
  86. 't1',
  87. 't1',
  88. \Magento\Store\Model\Store::DEFAULT_STORE_ID
  89. );
  90. if ($attribute->getFlatAddChildData()) {
  91. $joinCondition .= ' AND e.child_id = t1.entity_id';
  92. }
  93. $valueExpr = $connection->getCheckSql('t2.value_id > 0', 't2.value', 't1.value');
  94. /** @var $select \Magento\Framework\DB\Select */
  95. $select = $connection->select()->joinLeft(
  96. ['t1' => $attributeTable],
  97. $joinCondition,
  98. []
  99. )->joinLeft(
  100. ['t2' => $attributeTable],
  101. sprintf($joinConditionTemplate, 't1', 't2', 't2', 't2', 't2', $store),
  102. [$attributeCode => $valueExpr]
  103. );
  104. if ($attribute->getFrontend()->getInputType() != 'multiselect' && $hasValueField) {
  105. $valueIdExpr = $connection->getCheckSql('to2.value_id > 0', 'to2.value', 'to1.value');
  106. $select->joinLeft(
  107. ['to1' => $this->getTable('eav_attribute_option_value')],
  108. "to1.option_id = {$valueExpr} AND to1.store_id = 0",
  109. []
  110. )->joinLeft(
  111. ['to2' => $this->getTable('eav_attribute_option_value')],
  112. $connection->quoteInto("to2.option_id = {$valueExpr} AND to2.store_id = ?", $store),
  113. [$attributeCode . '_value' => $valueIdExpr]
  114. );
  115. }
  116. if ($attribute->getFlatAddChildData()) {
  117. $select->where('e.is_child = 0');
  118. }
  119. return $select;
  120. }
  121. }