P
P
Petr Fronin2016-10-26 11:20:17
Yii
Petr Fronin, 2016-10-26 11:20:17

How to add limit to hasmany?

There is a function:

public function getVideos()
    {
        return $this->hasMany(Video::className(), ['user_id' => 'id'])->orderBy('created_at desc');
    }

In the code we write:
$user = $this->findModel($id);             
$user->getVideos();

Everything works fine and we get all the user's videos, but we only need to get 4!
How can this be limited?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2016-10-26
@qonand

$user = $this->findModel($id);             
$user->getVideos()->limit(4);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question