Answer the question
In order to leave comments, you need to log in
How can you build a query with an index?
It would be nice for me if the index fixes the store, but it turns out that when you do indexby in the query, then only one entry for this store gets there and this is the last one, but how to make it different and all the data from this store gets there?
Here is the query that I built
$query = Shifts::find()->with('shop')->andWhere(['between', 'date', date('Y-m-d', strtotime('last Monday')), date('Y-m-d', strtotime('Sunday'))])->indexBy(['shop.name'])->all();
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)
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)
],
//и тд
]
]
Answer the question
In order to leave comments, you need to log in
From the indexBy application sources:
/**
* Converts the raw query results into the format as specified by this query.
* This method is internally used to convert the data fetched from database
* into the format as required by this query.
* @param array $rows the raw query result from database
* @return array the converted query result
*/
public function populate($rows)
{
if ($this->indexBy === null) {
return $rows;
}
$result = [];
foreach ($rows as $row) {
$result[ArrayHelper::getValue($row, $this->indexBy)] = $row;
}
return $result;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question