public function actionAddContact($id) { $users = User::getNames(); unset($users['admin']); unset($users['']); foreach (Groups::model()->findAll() as $group) { $users[$group->id] = $group->name; } $contacts = Contacts::getAllNames(); unset($contacts['0']); $model = $this->loadModel($id); $contacts = Sales::editContactArray($contacts, $model); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['Sales'])) { $temp = $model->associatedContacts; $tempArr = $model->attributes; $model->attributes = $_POST['Sales']; $arr = $_POST['Sales']['associatedContacts']; foreach ($arr as $contactId) { $rel = new Relationships(); $rel->firstType = 'Contacts'; $rel->firstId = $contactId; $rel->secondType = 'Sales'; $rel->secondId = $model->id; $rel->save(); } $model->associatedContacts = Sales::parseContacts($arr); $temp .= " " . $model->associatedContacts; $model->associatedContacts = $temp; $changes = $this->calculateChanges($tempArr, $model->attributes); $model = $this->updateChangelog($model, $changes); if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('addContact', array('model' => $model, 'users' => $users, 'contacts' => $contacts, 'action' => 'Add')); }