insert multiple records from one input text field


I am newly use YII. I want to insert multiple records, like 222-290.






one input field

view file

<div class="simple">

<?php echo CHtml::activeLabelEx($model,‘record’); ?>

<?php echo CHtml::activeTextField($model,‘record’); ?>


can anyone help me how to save the records

Do you want the same field to be repeated dynamically on the page? Or do you want to generate 8 new records for every 1 field entry?

More information would be very helpful

I want to generate 1-100 or more records for 1 field entry. Suppose I have one input text field and user input 1-200 and submit, then controller will insert 1 to 200 records in to the database. I don’t understand how to write this in my controller

To create multiple records, you could create a while or for loop, inside the loop set $model = new Model();

Then just say, $model->whatever = value; then $model->save();

A better way would be to do a mass insert, dunno if ActiveRecord supports that, but it should be doable with DAO

can you send me an example, I m newly using Yii, so I don’t understand that. I really appreciate it.


Please give more details, exactly what text users should enter into this one single input field.

User input numeric value in to the text field ( 1523-4343) and submit, then will insert to the database like

1523 one row

1524 2nd row

Probably you’ll need to validate user input, but here I assume it is correct:

$model->attributes = $_POST['ModelName'];

list($start, $end) = explode('-', $model->record);

for ($i = $start; $i <= $end; $i++)


    $_model = new ModelName;

    $_model->record = $i;



BTW nemo, I would recommend using 2 text fields if you’re allowing a range, so your users don’t have to adhere to a specific format. Also then you wouldn’t have to worry about splitting/exploding the string based on the hyphen.

can you send me an example with controller? It will really help me.


This is untested, but try a form like:

<div class="form">

<?php echo CHtml::beginForm(); ?>


    <div class="row">

        <?php echo CHtml::label('Input Range', false); ?>

        <?php echo CHtml::textField('field1') ?>

    &nbsp; to &nbsp;

        <?php echo CHtml::textField('field2') ?>



    <div class="row submit">

        <?php echo CHtml::submitButton('Save'); ?>



<?php echo CHtml::endForm(); ?>

</div><!-- form -->

Then in your controller, check for $_POST[‘field1’], $_POST[‘field2’], something like this:

if(!empty($_POST)) {

$start = (int) $_POST['field1'];

$end = (int) $_POST['field2'];

if(empty($end)) {

$end = $start;


for ($i = $start; $i <= $end; $i++)


    $_model = new ModelName;

    $_model->record = $i;




WOW. Great. Thank you very much.