Gabesz
Gabesz

Reputation: 303

Yii2 ignore first database result

is there any way to ignore the first result record in Yii2 at a query? I have a list of numbers that represents a client. For designing purposes i had to query the first record separatly but now i have it duplicated. My questin is how can I query in Yii2 to ignore the first result?

Regards, Gábor

The second find is the query where i need to ignore the first result: public function actionGeneratePage() {

public function actionGeneratePage() {

    $behivott = Sorszam::find()->with('ablak')
        ->orderBy(['behivas_datum' => SORT_DESC])
        ->limit(1)
        ->all();
    $sorszamok = Sorszam::find()->with('ablak')
        ->orderBy(['behivas_datum' => SORT_DESC])
        ->limit(4)
        ->all();
    $reklam = Reklam::find()->all();

    return $this->render('generatePage', [
        'sorszamok' => $sorszamok,
        'reklam'    => $reklam,
        'behivott'  => $behivott,
    ]);
}    

Upvotes: 0

Views: 336

Answers (1)

topher
topher

Reputation: 14860

You use offset() to skip the first record:

$sorszamok = Sorszam::find()->with('ablak')
    ->orderBy(['behivas_datum' => SORT_DESC])
    ->limit(4)
    ->offset(1)
    ->all();

Also you can use a single query to get both $behivott and $sorszamok with array_shift:

$sorszamok = Sorszam::find()->with('ablak')
    ->orderBy(['behivas_datum' => SORT_DESC])
    ->limit(5)
    ->all();

$behivott = array_shift($sorszamok);

Upvotes: 2

Related Questions