简体   繁体   中英

get limited numbers of data with yii2

I want to get limited numbers of data from database using Yİİ2. I fetched all the record by writing this:

$departures = ArrayHelper::map(
    TourDeparture::find()->all(),
    'id',
    'tour_id'
);

I tried to use limit(5) , so that I can get only 5 rows. But I could not. Still, I get the all the rows in the table. How can I achieve that?

Updated: Here is my tourdeparture model

class TourDeparture extends \yii\db\ActiveRecord

{

public static function tableName()
{
    return 'tour_departure';
}

/**
 * {@inheritdoc}
 */
public function rules()
{
    return [
        [['tour_id', 'start_date', 'end_date', 'price_1adult', 'price_2adult', 'price_3adult', 'price_child', 'price_baby', 'min_guests', 'max_guests', 'status', 'required_min_guest'], 'required'],
        [['tour_id', 'min_guests', 'max_guests', 'status', 'required_min_guest'], 'integer'],
        [['start_date', 'end_date'], 'safe'],
        [['price_1adult', 'price_2adult', 'price_3adult', 'price_child', 'price_baby'], 'number'],
        [['tour_id'], 'exist', 'skipOnError' => true, 'targetClass' => Tour::className(), 'targetAttribute' => ['tour_id' => 'id']],
    ];
}

/**
 * {@inheritdoc}
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'tour_id' => 'Tour ID',
        'start_date' => 'Start Date',
        'end_date' => 'End Date',
        'price_1adult' => 'Price 1adult',
        'price_2adult' => 'Price 2adult',
        'price_3adult' => 'Price 3adult',
        'price_child' => 'Price Child',
        'price_baby' => 'Price Baby',
        'min_guests' => 'Min Guests',
        'max_guests' => 'Max Guests',
        'status' => 'Status',
        'required_min_guest' => 'Required Min Guest',
    ];
}

/**
 * Gets query for [[Tour]].
 *
 * @return \yii\db\ActiveQuery
 */
public function getTour()
{
    return $this->hasOne(Tour::className(), ['id' => 'tour_id']);
}

/**
 * Gets query for [[TourReservations]].
 *
 * @return \yii\db\ActiveQuery
 */
public function getTourReservations()
{
    return $this->hasMany(TourReservation::className(), ['tour_departure_id' => 'id']);
}

}

for retrieve the first 5 rows should be

TourDeparture::find()->orderBy("id")->limit(5)->all() 
$departures = ArrayHelper::map( TourDeparture::find()->limit(5), 'id', 'tour_id' );

I wrote it like above. but ı got all the record

I'm surprised your code worked at all! You are passing the Query class into the map function. ArrayHelper::map is expecting an array and needs the query to be executed using the ->all() . ->limit(5) just adds a new term to the SQL query.

$departures = ArrayHelper::map( TourDeparture::find()->limit(5)->all(), 'id', 'tour_id' );

The code you shared didn't do the limit(5). Anyway, I believe it is caused by the array map, you may try to simulate the changes as below

$q = TourDeparture::find()->select(['id', 'tour_id'])->limit(5)->asArray()->all();

\yii\helpers\VarDumper::dump($q, $depth = 10, $highlight = true);

For the above, records selected in array form, before array map.

After array map, from here you can compare the changes.

$departures = ArrayHelper::map(
                 $q,
                 'id',
                 'tour_id'
              );

\yii\helpers\VarDumper::dump($departures, $depth = 10, $highlight = true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM