Run Forloop In Yii Model Action Create And Save Every Record

I need to run forloop in yii model action create or Addipblock() and save every record although forloop end and then redirect it to view admin,

My first record take start from 1 and end 50, So in database should be 50 records added.

My code is like this:


public function actionAddipblock(){        

    $model=new IpManager('addipblock'); 

    if(isset($_POST['IpManager'])){

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

        $starting_ip = $_POST['IpManager']['starting_ip'];

        $ending_ip   = $_POST['IpManager']['ending_ip'];            

        if($model->validate('addipblock')){             

        for($ip = $starting_ip; $ip <= $ending_ip; $ip++){

            $model->ip = $ip;

            $model->server = $_POST['IpManager']['server'];

            $model->client = $_POST['IpManager']['client'];

            $model->status = $_POST['IpManager']['status'];

            $model->creation_date = date("Y-m-d H:i:s");

        }           

        if($model->save()){ 

               $this->redirect(array('admin'));     

         }

          else{

                echo 'Error:';

            }               

        }

    }

    $this->render('addipblock',array('model'=>$model));

}

But when I run this code it save only one record not save all recorde until forloop end. Guys what will you suggest me for this? what should I can do for this?

hi friend this is because you are calling $model->save() only once after the loop try putting $model->save() inside the loop. donot over use active record instead use query builder…

hi friend your logic is wrong because you are creating a single ar instance and you cannot loop it…

Thanks very much,I got the solution but now I hvae problem to valideate uniq ip address in forloop.

My validation rules,




              public function rules(){

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('ip, server', 'required', 'on' => 'create,update'),			

			array('ip', 'unique', 'on' => 'create'),

			

			//Validation rules for addipblock scenario

			array('starting_ip,ending_ip,server', 'required', 'on' => 'addipblock'),

			array('ip', 'unique', 'on' => 'addipblock'),

	       }

              

My Controller Action to add Ip Block.




          public function actionAddipblock(){

		

    	$model=new IpManager('addipblock');


	    // uncomment the following code to enable ajax-based validation

	   /*   

	    if(isset($_POST['ajax']) && $_POST['ajax']==='ip-manager-addipblock-form')

	    {

	        echo CActiveForm::validate($model);

	        Yii::app()->end();

	    }*/

	

	    if(isset($_POST['IpManager']))

	    {

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

	        

	        $starting_ip = $_POST['IpManager']['starting_ip'];

	        $ending_ip 	 = $_POST['IpManager']['ending_ip'];

	        

	        

	        		

	        for($ip = $starting_ip; $ip <= $ending_ip; $ip++){

	        	$model->id = null;

	        	$model->ip = $ip;

	        	$model->server = $_POST['IpManager']['server'];

	        	$model->client = $_POST['IpManager']['client'];

	        	$model->status = $_POST['IpManager']['status'];

	        	$model->creation_date = date("Y-m-d H:i:s");	        	

	        	$model->isNewRecord = true;

	        	

	        	if($model->validate('addipblock')){	        	

	        		$model->save();

	        	}

	        }	        

	        if($model->save()){

					$this->redirect(array('admin'));		

	            }

	            else{

	            	echo 'Error:';

	            }        

	    }

	    $this->render('addipblock',array('model'=>$model));

	}

         

But I want to apply validate rule at every ip that’s built in forloop,and validation rule must be check uniqe ip,but not working.it is validat only last ip of forloop returns.

I dont know y u r looping a single model at all?

look you r created a single ar model no need to loop it because all values are automatically assinged…

try this you may add the flash mesassge


$errors = $model->getErrors();

            if($model->save())

            {

                Yii::app()->user->setFlash('error',Yii::t("messages",$errors));

                $this->redirect('index');

                Yii::app()->end();

}