Save array to database

Hi I’m a newbie in yii

I want to ask, how to save data array into a database?

My case I want to save the data which is input from multiple row

but the problem that could have saved it from just one field that is only from array 0

My actionCreate

public function actionCreate($idar) {

$model = new TbSnSpare;

if (isset($_POST[‘TbSnSpare’])) {

$model->attributes = $_POST[‘TbSnSpare’];

$model->id_spare = $idar;

if (isset($_POST[‘serial_number’])) {

$total = count($_POST[‘serial_number’]);

for ($i = 0; $i < $total; $i++) {

if (isset($_POST[‘serial_number’][$i])) {

$model->serial_number = $_POST[‘serial_number’][$i];

$model->save();

}

}

$this->redirect(array(‘view’, ‘id’ => $model->id_sn));

}

}

$this->render(‘create’, array(

‘model’ => $model,

));

}

Please help me…

Maybe you should use implode or serialize to save string




$model-&gt;serial_number = implode(',', $_POST['serial_number']); // $model-&gt;serial_number = serialize($_POST['serial_number']);

$model-&gt;save();



Instead of storing array it is better to store it as json




$model->serial_number = json_encode($_POST['serial_number']);



while fetching just decode it.