Cakephp 3 - Get only the first result from hasMany


#1

Hello, I have two models: Products and Images. A product hasMany images. How can I get only the first image of each product?
My current query is:
$products = $this->Products->find()->contain(['Images'])->where(['Products.published =' => 1])->all();


#2
$products = $this->Products->find('all')
->contain([
    'Images' => function ($q) {
        return $q->first();
    }
])
->where(['Products.published' => 1]);

other solution:
create a flag in the images to set for example main image that represents the product and filter in where