Return n first items

cakephp 4.x

I am trying to get the 5 latest loaded images (in a plugin called medias).
So I tried (in src->Model->Table :

$this->hasMany('LatestFiles', [
            'className' => 'Medias.Fichiers',
            'foreignKey' => 'dossier_id',
            'strategy' => 'select',
            'queryBuilder' => function ($q) use($imageTypes) {
                return $q->where(['LatestFiles.filetype IN' => $imageTypes])->order(['LatestFiles.modified' => 'DESC'])->limit(5);
            }
        ]);

and in the controller :

->contain([ 'LatestFiles'])

but the limit does not work. It returns all images.

I am sure that I am very close but where ?

The queryBuilder option is for use when changing what you’re containing on the fly. What I think you’re looking for in association setup is the finder option.

Hello darkness my old friend…

Thank you for the link (and for responding too :slight_smile: )