H
H
hollanditkzn2018-03-27 12:06:58
Yii
hollanditkzn, 2018-03-27 12:06:58

How to use indexBy to shorten array code?

I made something like this array building an array, something like this

$shops = $request->get('ShiftsSearch')['shop_id'] == null ? Shop::find()->all() : Shop::find($request->get('ShiftsSearch')['shop_id'])->all();
        foreach ($shops as $shop){
            $arrShop[$shop->name] = Shifts::find()->with('shop')->andWhere(['between', 'date', $date_start->format('Y-m-d'), $date_end->format('Y-m-d')])->andWhere(['shop_id' => $shop->id])->all();
        }

query => [
  'Сибирский тракт' => [
      [ 
             'id' => int 34
              'user_id' => int 5
              'shop_id' => int 1
              'date' => string '2018-03-20' (length=10)
              'start_time' => string '08:10:00' (length=8)
              'end_time' => string '18:15:00' (length=8)
              'created_at' => string '2018-03-23 10:25:13' (length=19)
              'updated_at' => string '2018-03-23 10:25:13' (length=19)
     ],
      [ 
             'id' => int 35
              'user_id' => int 6
              'shop_id' => int 1
              'date' => string '2018-03-20' (length=10)
              'start_time' => string '08:10:00' (length=8)
              'end_time' => string '18:15:00' (length=8)
              'created_at' => string '2018-03-23 10:25:13' (length=19)
              'updated_at' => string '2018-03-23 10:25:13' (length=19)
     ],
      [ 
             'id' => int 36
              'user_id' => int 8
              'shop_id' => int 1
              'date' => string '2018-03-20' (length=10)
              'start_time' => string '08:10:00' (length=8)
              'end_time' => string '18:15:00' (length=8)
              'created_at' => string '2018-03-23 10:25:13' (length=19)
              'updated_at' => string '2018-03-23 10:25:13' (length=19)
     ],
//и тд
  ]
]

But when I try to shorten this code through indexBy
$query = Shifts::find()->with('shop')->andWhere(['between', 'date', $start, $end]);
 $query = $request->get('ShiftsSearch')['shop_id'] == null ? $query->indexBy('shop.name')->all() : $query->andWhere(['shop_id' => $request->get('ShiftsSearch')['shop_id']])->indexBy('shop.name')->all();

Then I get something like this
public 'query' => 
    array (size=1)
      'Сибирский тракст' => 
        object(app\models\Shifts)[153]
          private '_attributes' (yii\db\BaseActiveRecord) => 
            array (size=8)
              'id' => int 34
              'user_id' => int 5
              'shop_id' => int 1
              'date' => string '2018-03-20' (length=10)
              'start_time' => string '08:10:00' (length=8)
              'end_time' => string '18:15:00' (length=8)
              'created_at' => string '2018-03-23 10:25:13' (length=19)
              'updated_at' => string '2018-03-23 10:25:13' (length=19)

That is, they are grouped themselves, but I need the index to be converted and everything that is there is displayed

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Kim, 2018-03-28
@kimono

Try:asArray()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question