Link.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\GroupedProduct\Model\ResourceModel\Product;
  7. use Magento\Catalog\Api\Data\ProductInterface;
  8. use Magento\Catalog\Model\ResourceModel\Product\Relation;
  9. use Magento\Framework\EntityManager\MetadataPool;
  10. /**
  11. * @api
  12. * @since 100.0.2
  13. */
  14. class Link extends \Magento\Catalog\Model\ResourceModel\Product\Link
  15. {
  16. const LINK_TYPE_GROUPED = 3;
  17. /**
  18. * @var MetadataPool
  19. * @since 100.1.0
  20. */
  21. protected $metadataPool;
  22. /**
  23. * Link constructor.
  24. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  25. * @param Relation $catalogProductRelation
  26. * @param MetadataPool $metadataPool
  27. * @param string|null $connectionName
  28. */
  29. public function __construct(
  30. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  31. Relation $catalogProductRelation,
  32. MetadataPool $metadataPool,
  33. $connectionName = null
  34. ) {
  35. $this->metadataPool = $metadataPool;
  36. parent::__construct(
  37. $context,
  38. $catalogProductRelation,
  39. $connectionName
  40. );
  41. }
  42. /**
  43. * Retrieve Required children ids
  44. * Return grouped array, ex array(
  45. * group => array(ids)
  46. * )
  47. *
  48. * @param int $parentId
  49. * @param int $typeId
  50. * @return array
  51. */
  52. public function getChildrenIds($parentId, $typeId)
  53. {
  54. $connection = $this->getConnection();
  55. $childrenIds = [];
  56. $bind = [':product_id' => (int)$parentId, ':link_type_id' => (int)$typeId];
  57. $select = $connection->select()->from(
  58. ['l' => $this->getMainTable()],
  59. ['linked_product_id']
  60. )->join(
  61. ['cpe' => $this->getTable('catalog_product_entity')],
  62. sprintf(
  63. 'cpe.%s = l.product_id',
  64. $this->metadataPool->getMetadata(ProductInterface::class)->getLinkField()
  65. )
  66. )->where(
  67. 'cpe.entity_id = :product_id'
  68. )->where(
  69. 'link_type_id = :link_type_id'
  70. );
  71. $select->join(
  72. ['e' => $this->getTable('catalog_product_entity')],
  73. 'e.entity_id = l.linked_product_id AND e.required_options = 0',
  74. []
  75. );
  76. $childrenIds[$typeId] = [];
  77. $result = $connection->fetchAll($select, $bind);
  78. foreach ($result as $row) {
  79. $childrenIds[$typeId][$row['linked_product_id']] = $row['linked_product_id'];
  80. }
  81. return $childrenIds;
  82. }
  83. }