/** * @param CEvent $event */ public function processFromContactSide(CEvent $event) { $model = $event->sender; if (isset($model->originalAttributeValues['account'])) { if ($model->originalAttributeValues['account'][1] > 0) { //lookup to see if there is a 'primary' affiliation for old acc/con pairing and unmark as primary $accountContactAffiliations = AccountContactAffiliation::getPrimaryByAccountIdAndContactId((int) $model->originalAttributeValues['account'][1], (int) $model->id); //shouldn't be more than one, but if there is unset all of them foreach ($accountContactAffiliations as $accountContactAffiliation) { $accountContactAffiliation->primary = false; $accountContactAffiliation->save(); } } //lookup and see if there is an affiliation for the new acc/con pairing if ($model->account->id > 0) { $accountContactAffiliations = AccountContactAffiliation::getByAccountAndContact($model->account, $model); //Shouldn't be more than one, but if there is, just mark the first primary. if (count($accountContactAffiliations) > 0) { //If so - mark primary. $accountContactAffiliations[0]->primary = true; $accountContactAffiliations[0]->save(); } else { //If not, create and mark primary. $accountContactAffiliation = new AccountContactAffiliation(); $accountContactAffiliation->primary = true; $accountContactAffiliation->contact = $model; $accountContactAffiliation->account = $model->account; if ($accountContactAffiliation->isAttributeRequired('role') && $accountContactAffiliation->role->value == null) { $accountContactAffiliation->role->value = $this->resolveRoleValue($accountContactAffiliation); } $accountContactAffiliation->save(); } } } }
/** * @depends testGetByAccountAndContact */ public function testGetPrimaryByAccountIdAndContactId() { $accountContactAffiliations = AccountContactAffiliation::getAll(); $this->assertEquals(1, count($accountContactAffiliations)); $accountContactAffiliations = AccountContactAffiliation::getPrimaryByAccountIdAndContactId($accountContactAffiliations[0]->account->id, $accountContactAffiliations[0]->contact->id); $this->assertEquals(0, count($accountContactAffiliations)); $accountContactAffiliations = AccountContactAffiliation::getAll(); $accountContactAffiliations[0]->primary = true; $this->assertTrue($accountContactAffiliations[0]->save()); $accountContactAffiliations = AccountContactAffiliation::getPrimaryByAccountIdAndContactId($accountContactAffiliations[0]->account->id, $accountContactAffiliations[0]->contact->id); $this->assertEquals(1, count($accountContactAffiliations)); }