how to do $model->save at actionIndex

Hi there

How to save a model->data at actionIndex()?

This is my controller


public function actionIndex()

	{	

		$model=new CompanyLicense();

		if($model->checkExpireDate()){

			if(isset($_GET['CompanyLicense']))

				$model->attributes=$_GET['CompanyLicense'];

			$this->render('index',array(			

				'model'=>$model,

			));

		}else{

			echo 'debug2';

			$model->active = 'EXPIRED';

			if($model->save())

			{

				if(isset($_GET['CompanyLicense']))

					$model->attributes=$_GET['CompanyLicense'];

				$this->render('index',array(			

					'model'=>$model,

				));

			}

		}

	}

according my controller, my debugging "debug2" is display. but #model->save is not working. How should I do? or Do I need to do as MySQL stored producer?

Regards

T

check ur model rules… i think there are required fields…

Hi Thura747,

Perhapse the probleme come from validation, if $model->validate() return error messages then

$model->save() will not save your data, so try to debug with var_dump($model->validate()) in

the position of your debug message.

What are you trying exactly to save? You have a new instance of your model, and you save it? No attributes except ‘active’?

i didnt get what u r trying to do… the only value u r assigning is $model->active = ‘EXPIRED’; , then u r saving that.

@Rajith R and @bennouna,

to check one of the product is live or expired when the user view their products list. So, I’ve to validate before displaying the product list.

@riemann, thank you for your point. I got bool(false) msg. And I create $model->scenario at create and update action. And I set those $model->scenario at mode’s rule which is required as ‘on’=>‘create, update’. But I still getting same bool(false) how can I pass it?

Regards

T

But your $model has empty attributes when you do your checks. Normally you should have it instantiated first

sorry I was thinking wrong way. at index action, there are so many items. my checkexpired() is targeting for one items at a time. I’m sorry. is there any way to solved? I’m really sorry for my wrong logic.

hi,

Perhapse you have to populate model with before try to save with for example

$model->attributes=$_GET[‘CompanyLicense’]; just before doing $model->active = ‘EXPIRED’;

I change my code to this but not saving till now. is there anything wrong.




$records = CompanyLicense::model()->findAll();

		$list = CHtml::listData($records, 'id', 'expire_date');

		foreach($list as $k => $v)

		{

			$model = $this->loadModel($k);

			//echo $model->expire_date;

			if($model->checkExpireDate() == false){

				$model->active = 'EXPIRED';

				var_dump($model->validate($model->active));

				//echo 'debug' . $model->save();

				

				if($model->save())

				{

					if(isset($_GET['CompanyLicense']))

						$model->attributes=$_GET['CompanyLicense'];

					$this->render('index',array(			

						'model'=>$model,

					));

				}

			}

		}

Hi thura747,

  1. What is your view file for it?

Does the view show only one item?

Or, just like the gii-generated index page, does it show a list of items?

  1. What do you want a user to do in this index page?

Could you explain the business logic of the page from the end user’s point of view?

I mean, not from a programmer’s point of view.

In other word, you want to save a model, but I guess that’s not the real purpose.

You want to do something else and you are thinking that you have to save a model for it, aren’t you?

So, what’s the real purpose of your index page?

@softark, Thx alot for your quick reply.

  1. my view file is listing the products. No, my view is not to show only one item. It will show so many items by listing.

  2. there are products. each product has expire date. When I go to the products listing page, I want to know which product is live and which product was expired.

Regards

T

I see.

Then, I would do like the following in the partial view file for the product:




<?php echo ($data->checkExpireDate()) ? "ACTIVE" : "EXPIRED"; ?>



instead of




<?php echo $data->active; ?>



I mean you don’t have to change anything in gii-generated actionIndex method.

There’s no need to save “active” attribute, because it never keeps its state depending on the current date.

Or, if you really need to save(update) "active" attribute of the products:




// update "active" flags

$products = CompanyLicense::model()->findAll();

foreach($products as $product)

{

    $product->active = ($product->checkExpireDate()) ? "ACTIVE" : "EXPIRED";

    $product->save();

}


// show list

$model = new CompanyLicense();

if(isset($_GET['CompanyLicense']))

    $model->attributes=$_GET['CompanyLicense'];

$this->render('index',array(                    

   'model'=>$model,

));



Note that “$model” in actionIndex is an instance of CompanyLicense model that is meant for the container of search parameters. It doesn’t hold any real data(s) in the database.

check ur model rules man…and print the $model and post values…

and what is this


if(isset($_GET['CompanyLicense']))

                                                $model->attributes=$_GET['CompanyLicense'];

                                        $this->render('index',array(                    

                                                'model'=>$model,



after save u r again assigning values to model $model->attributes=$_GET[‘CompanyLicense’];… and passing it to index

@softark, Thx for your code. It is working half :P. Saving (updating) is not working. Still showing bool(false). when I check by var_dump($product->validate());

Is your "active" attribute of the model a bool or an integer?

If it is the case, then:




$product->active = ($product->checkExpireDate());



My "active" is enum. Now everything is fine. Working very well. Last code is


public function actionIndex()

	{	

		$companylicenses = CompanyLicense::model()->findAll();

		foreach($companylicenses as $companylicense)

		{

			$companylicense->active = ($companylicense->checkExpireDate()== true) ? "LIVE" : "EXPIRED";

			$companylicense->save();

		}

		

		$model=new CompanyLicense();		

		if(isset($_GET['CompanyLicense']))

			$model->attributes=$_GET['CompanyLicense'];

		$this->render('index',array(			

			'model'=>$model,

		));		

	}

Thank you so much, all of you who help me on this problem. :D :D

Regards

T