この質問に対する答えは間違っています。それらの実装は機能するかもしれませんが、これを処理する適切な方法ではありません。これを行う正しい方法は、Magentosのサービスコントラクトとデータモデルを使用することです。
この場合、Magento\ConfigurableProduct\Api\LinkManagementInterface必要なサービス契約です。
コンソールコマンドで使用しているコードの小さな例:
<?php
namespace Vendor\Module\Console;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\ConfigurableProduct\Api\LinkManagementInterface;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\State;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
/**
 * Class UpdateChildProducts
 * @package Vendor\Module\Console
 */
class UpdateChildProducts extends Command
{
    /**
     * @var ProductRepositoryInterface
     */
    protected $productRepository;
    /**
     * @var SearchCriteriaBuilder
     */
    protected $searchCriteriaBuilder;
    /**
     * @var LinkManagementInterface
     */
    protected $linkManagement;
    /**
     * @var State
     */
    protected $state;
    /**
     * UpdateChildProducts constructor.
     * @param State $state
     * @param LinkManagementInterface $linkManagement
     * @param ProductRepositoryInterface $productRepository
     * @param SearchCriteriaBuilder $searchCriteriaBuilder
     * @param string $name
     */
    public function __construct(
        State $state,
        LinkManagementInterface $linkManagement,
        ProductRepositoryInterface $productRepository,
        SearchCriteriaBuilder $searchCriteriaBuilder,
        $name = 'update_child_products'
    ) {
        $this->state = $state;
        $this->linkManagement = $linkManagement;
        $this->productRepository = $productRepository;
        $this->searchCriteriaBuilder = $searchCriteriaBuilder;
        parent::__construct($name);
    }
    /**
     * Configure this command
     */
    protected function configure()
    {
        $this->setName('example:update_child_products');
        $this->setDescription('Iterate over all configurable products and show their children count.');
    }
    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        // Set area code
        try {
            $this->state->setAreaCode('adminhtml');
        } catch (\Exception $e) {
            // Fail silently ...
        }
        $searchCriteria = $this->searchCriteriaBuilder
            ->addFilter('type_id', 'configurable')
            ->create();
        $configurableProducts = $this->productRepository->getList($searchCriteria);
        $output->writeln(sprintf('Found %d configurable products ...', $configurableProducts->getTotalCount()));
        foreach ($configurableProducts->getItems() as $configurableProduct) {
            $childProducts = $this->linkManagement->getChildren($configurableProduct->getSku());
            $output->writeln(
                sprintf('Found %d children for %s', count($childProducts), $configurableProduct->getSku())
            );
        }
    }
}
Magento 2は、コードの大部分がMagento 1から移植されているため、独自のコードとあまり一致していません。そのため、継承ベースのモデルとそれらのメソッド(などgetTypeInstance())の残りが残っています。将来を見据えたMagento 2コードを作成する場合は、サービスコントラクトとデータモデルをできるだけ使用してください。