user3496974
user3496974

Reputation: 145

Save data into different table : Yii

I am a newbie to yii and confused how to do this. I am working on creating a webpage.

I have a model in Yii(model1). At some instance I want to save few selected values to another table. Hence forth I have created a new model for new table (say model2). I have this code in my controller and does'nt seem to work

if(isset($_POST['model1']['mobile']))
{
foreach ($_POST['model1']['mobile'] as $id)
  {
   $model2->email = $this->email;
   $model2->save();
  }
}

What I am trying to do here is, in my model1 I have many attributes out of which I want to save only 'email' attribute to the second table(model2). This is giving me an error as "Model1Controller.email" is not defined. Am I missing something here?

Upvotes: 1

Views: 194

Answers (1)

Justinas
Justinas

Reputation: 43481

Your $this refers to class variable, not to foreach $id

if(isset($_POST['model1']['mobile']))
{
  foreach ($_POST['model1']['mobile'] as $id)
  {
   $model2->email = $id->email;
   $model2->save();
  }
}

Upvotes: 1

Related Questions