سؤال

I need to save the field value several times by separating them by commas. I am getting the values,but i need to store each value in model in a loop.

$subModel->name = $_POST['Model']['keywords'];
$keyword = explode(",", $subModel->name);    
for ($i=0; $i<=sizeof($keyword)-1; $i++)
{
    //echo $keyword[$i]."<br/>";
    $subModel->name = $keyword[$i];
    $subModel->save();
}

It is storing only last value, can anyone tell me how to store into my model several times for every value one time in submodel in a loop.

Thanks

هل كانت مفيدة؟

المحلول

You need to set $subModel->isNewRecord = true in each iteration and also unset (or manually set to the value of your choice) the model's id field. It would look something like this:

for ($i=0; $i<=sizeof($keyword)-1; $i++)
{
    // if $subModel->name is not the primary key for that model, unset the PK here:
    // $subModel->id = null
    $subModel->name = $keyword[$i];
    $subModel->isNewRecord = true;
    $subModel->save();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top