Magento – Magento 2: Collection getSelect does not load the results

magento2PHP

I have this method

  public function getCarBrands() {
    return $this->_objectManager->create('Scandimedia\Wheels\Model\CarModels')->getCollection()->getSelect()->group('brand');
}

But when I execute it and try to foreach the results with:

foreach($this->getCarBrands() as $model) {
echo $model->getData();

}

It does not do anything.
But when I echo it returns:

SELECT `main_table`.* FROM `scandimedia_wheels_car_models` AS `main_table` GROUP BY `brand`

If I do get_class, it gives me this result: Magento\Framework\DB\Select

How can I actually load the results from the group and get the data without running the string as a query?

Best Answer

You need to add addFieldToSelect('*') to your collection.

You need to modify:

public function getCarBrands() {

    $collection =  $this->_objectManager->create('Scandimedia\Wheels\Model\CarModels')->getCollection();
    $collection->addFieldToSelect('*')
    $collection->getSelect()->group('main_table.brand');
    return $collection;
}