In my database table, the list of winners is stored in a serialized way. I know that after fetching the winners record from the database, I can unserialize the data and make an array of it. But for that, I have to use, “foreach” loop or have to do it manually. And I have successfully done that too.
But a thing came to my mind that is this possible to get the data in an unserialized way during the execution of query. Below is my Yii2 Active Query.
$winners = Winners::find()
->select('winners')
->where(['event_id'=> Yii::$app->session->get('event_id')])
->asArray()
->all();
This query is giving me what I want. Let me show you.
(
[0] => Array
(
[winners] => a:10:{i:0;i:0;i:1;i:4;i:2;i:8;i:3;i:13;i:4;i:15;i:5;i:19;i:6;i:20;i:7;i:23;i:8;i:25;i:9;i:26;}
)
But I want the winners list in unserialized form. And I tried doing the following.
$winners = Winners::find()
->select(serialize('winners'))
->where(['event_id'=> Yii::$app->session->get('event_id')])
->asArray()
->all();
But the above query generates an error. Is there any way to Unserialize the winners list during the query execution and get the winners list in array format
Try this :
$winners = Winners::find()
->select('winners')
->where(['event_id'=> Yii::$app->session->get('event_id')])
->asArray()
->all();
$result = unserialize($winners[0]['winners']);
Now use $result.