Yii Framework Forum: how to do $model->save at actionIndex - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

how to do $model->save at actionIndex Rate Topic: -----

#1 User is offline   thura747 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 10-July 12

Posted 08 August 2012 - 12:43 AM

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
0

#2 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 08 August 2012 - 01:04 AM

check ur model rules... i think there are required fields..
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#3 User is offline   riemann 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 31-August 11
  • Location:Algeria

Posted 08 August 2012 - 01:05 AM

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.
0

#4 User is online   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,114
  • Joined: 05-January 12
  • Location:Morocco

Posted 08 August 2012 - 01:09 AM

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

#5 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 08 August 2012 - 01:09 AM

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 Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#6 User is offline   thura747 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 10-July 12

Posted 08 August 2012 - 01:59 AM

@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
0

#7 User is online   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,114
  • Joined: 05-January 12
  • Location:Morocco

Posted 08 August 2012 - 02:14 AM

Quote

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.

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

#8 User is offline   thura747 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 10-July 12

Posted 08 August 2012 - 02:41 AM

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.
0

#9 User is offline   riemann 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 31-August 11
  • Location:Algeria

Posted 08 August 2012 - 03:25 AM

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';
0

#10 User is offline   thura747 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 10-July 12

Posted 08 August 2012 - 04:36 AM

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,
					));
				}
			}
		}

0

#11 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,527
  • Joined: 16-February 11
  • Location:Japan

Posted 08 August 2012 - 04:57 AM

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?

2) 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?
0

#12 User is offline   thura747 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 10-July 12

Posted 08 August 2012 - 05:11 AM

@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
0

#13 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,527
  • Joined: 16-February 11
  • Location:Japan

Posted 08 August 2012 - 05:23 AM

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.
0

#14 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,527
  • Joined: 16-February 11
  • Location:Japan

Posted 08 August 2012 - 05:32 AM

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.
0

#15 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 08 August 2012 - 06:19 AM

check ur model rules man..and print the $model and post values....
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#16 User is offline   Rajith R 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 550
  • Joined: 20-April 11
  • Location:Kochi , Kerala, India

Posted 08 August 2012 - 06:21 AM

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
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#17 User is offline   thura747 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 10-July 12

Posted 08 August 2012 - 10:33 PM

@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());
0

#18 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 1,527
  • Joined: 16-February 11
  • Location:Japan

Posted 08 August 2012 - 11:44 PM

Is your "active" attribute of the model a bool or an integer?
If it is the case, then:
$product->active = ($product->checkExpireDate());

0

#19 User is offline   thura747 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 37
  • Joined: 10-July 12

Posted 09 August 2012 - 12:17 AM

 softark, on 08 August 2012 - 11:44 PM, said:

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
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users