public function setAttributeMetadataFromForm(AttributeForm $attributeForm) { assert('$attributeForm instanceof MultiSelectDropDownAttributeForm'); $modelClassName = get_class($this->model); $attributeName = $attributeForm->attributeName; $attributeLabels = $attributeForm->attributeLabels; $defaultValueOrder = $attributeForm->defaultValueOrder; $elementType = $attributeForm->getAttributeTypeName(); $partialTypeRule = $attributeForm->getModelAttributePartialRule(); $isRequired = (bool) $attributeForm->isRequired; $isAudited = (bool) $attributeForm->isAudited; $customFieldDataName = $attributeForm->customFieldDataName; if ($customFieldDataName == null) { $customFieldDataName = ucfirst(strtolower($attributeForm->attributeName)); //should we do something else instead? } $customFieldDataData = $attributeForm->customFieldDataData; $customFieldDataLabels = $attributeForm->customFieldDataLabels; $defaultValue = DropDownDefaultValueOrderUtil::getDefaultValueFromDefaultValueOrder($defaultValueOrder, $customFieldDataData); ModelMetadataUtil::addOrUpdateCustomFieldRelation($modelClassName, $attributeName, $attributeLabels, $defaultValue, $isRequired, $isAudited, $elementType, $customFieldDataName, $customFieldDataData, $customFieldDataLabels, 'OwnedMultipleValuesCustomField'); if ($attributeForm->getCustomFieldDataId() != null) { foreach ($attributeForm->customFieldDataData as $order => $newValue) { if (isset($attributeForm->customFieldDataDataExistingValues[$order]) && $attributeForm->customFieldDataDataExistingValues[$order] != $newValue) { MultipleValuesCustomField::updateValueByDataIdAndOldValueAndNewValue($attributeForm->getCustomFieldDataId(), $attributeForm->customFieldDataDataExistingValues[$order], $newValue); } } } $this->resolveDatabaseSchemaForModel($modelClassName); }
public function setAttributeMetadataFromForm(AttributeForm $attributeForm) { assert('$attributeForm instanceof DropDownAttributeForm'); $modelClassName = get_class($this->model); $attributeName = $attributeForm->attributeName; $attributeLabels = $attributeForm->attributeLabels; $defaultValueOrder = $attributeForm->defaultValueOrder; $elementType = $attributeForm->getAttributeTypeName(); $partialTypeRule = $attributeForm->getModelAttributePartialRule(); $isRequired = (bool) $attributeForm->isRequired; $isAudited = (bool) $attributeForm->isAudited; $customFieldDataName = $attributeForm->customFieldDataName; if ($customFieldDataName == null) { $customFieldDataName = ucfirst(strtolower($attributeForm->attributeName)); //should we do something else instead? } $customFieldDataData = $attributeForm->customFieldDataData; $customFieldDataLabels = $attributeForm->customFieldDataLabels; $defaultValue = DropDownDefaultValueOrderUtil::getDefaultValueFromDefaultValueOrder($defaultValueOrder, $customFieldDataData); ModelMetadataUtil::addOrUpdateCustomFieldRelation($modelClassName, $attributeName, $attributeLabels, $defaultValue, $isRequired, $isAudited, $elementType, $customFieldDataName, $customFieldDataData, $customFieldDataLabels); if ($attributeForm->getCustomFieldDataId() != null) { $oldAndNewValuePairs = array(); foreach ($attributeForm->customFieldDataData as $order => $newValue) { if (isset($attributeForm->customFieldDataDataExistingValues[$order]) && $attributeForm->customFieldDataDataExistingValues[$order] != $newValue) { CustomField::updateValueByDataIdAndOldValueAndNewValue($attributeForm->getCustomFieldDataId(), $attributeForm->customFieldDataDataExistingValues[$order], $newValue); $oldValue = $attributeForm->customFieldDataDataExistingValues[$order]; $oldAndNewValuePairs[$oldValue] = $newValue; } } if (count($oldAndNewValuePairs) > 0) { DropDownDependencyDerivedAttributeDesignerUtil::updateValueInMappingByOldAndNewValue($modelClassName, $attributeName, $oldAndNewValuePairs, $attributeForm->customFieldDataDataExistingValues[$order], $newValue); } DropDownDependencyDerivedAttributeDesignerUtil::resolveValuesInMappingWhenValueWasRemoved($modelClassName, $attributeName, $attributeForm->customFieldDataData); } $this->resolveDatabaseSchemaForModel($modelClassName); }
/** * @depends testAttributeLabelsMergeCorrectlyWithExistingData */ public function testSavingCustomFieldDataLabels() { $a = new A(); $this->assertTrue($a->isAttribute('fruitCstm')); unset($a); $appleCustomField = new CustomField(); $appleCustomField->value = 'apple'; $appleCustomField->data = CustomFieldData::getByName('Fruit'); $this->assertTrue($appleCustomField->save()); $attributeLabels = array('en' => 'fruit'); ModelMetadataUtil::addOrUpdateCustomFieldRelation('A', 'fruit', $attributeLabels, $appleCustomField, true, false, 'DropDown', 'Fruit', array('apple', 'grape', 'orange'), array('fr' => array('appleFr', 'grapeFr', 'orangeFr'), 'de' => array('', 'grape', '')), 'CustomField', false); $a = new A(); $a->a = 1; $this->assertTrue($a->isAttribute('fruitCstm')); $this->assertTrue($a->validate()); $this->assertEquals('apple', $a->fruitCstm->value); $compareData = array('fr' => array('appleFr', 'grapeFr', 'orangeFr'), 'de' => array('', 'grape', '')); $this->assertEquals($compareData, unserialize($a->fruitCstm->data->serializedLabels)); unset($a); ModelMetadataUtil::removeAttribute('A', 'fruitCstm'); }
/** * There was a bug if you had an existing model, then created a custom drop down, it would not show * any values. This was resolved by making sure cached models constructDerived. This test should pass now * that the fix is implemented. * @depends testGetRequiredDerivedLayoutAttributeTypes */ public function testExistingModelsShowCustomFieldDataCorrectlyWhenAttributeIsAddedAsDatabaseColumn() { $super = User::getByUsername('super'); Yii::app()->user->userModel = $super; //Create account $account = AccountTestHelper::createAccountByNameForOwner('test', $super); $accountId = $account->id; $account->forget(); $originalMetadata = Account::getMetadata(); $attributeLabels = array('en' => 'newRelation'); ModelMetadataUtil::addOrUpdateCustomFieldRelation('Account', 'newRelation', $attributeLabels, null, false, false, 'DropDown', 'Things', array('thing 1', 'thing 2'), array('fr' => array('thing 1 fr', 'thing 2 fr'))); $adapter = new ModelAttributesAdapter(new Account()); $adapter->resolveDatabaseSchemaForModel('Account'); $metadata = Account::getMetadata(); $this->assertNotEquals($originalMetadata, $metadata); $this->assertEquals($originalMetadata['Account']['rules'], $metadata['Account']['rules']); $newRelation = $metadata['Account']['relations']['newRelationCstm']; $this->assertEquals(array(RedBeanModel::HAS_ONE, 'OwnedCustomField', RedBeanModel::OWNED, RedBeanModel::LINK_TYPE_SPECIFIC, 'newRelationCstm'), $newRelation); $this->assertEquals('Things', $metadata['Account']['customFields']['newRelationCstm']); //on a new account, does the serialized data show correctly. $account = new Account(); $this->assertEquals(array('thing 1', 'thing 2'), unserialize($account->newRelationCstm->data->serializedData)); ForgetAllCacheUtil::forgetAllCaches(); //retrieve account and make sure the serialized data shows correctly. //This will not be cached. $account = Account::getById($accountId); $this->assertNotNull($account->industry->data->serializedData); $this->assertEquals(array('thing 1', 'thing 2'), unserialize($account->newRelationCstm->data->serializedData)); //This will pull from cached. Clear the php cache first, which simulates a new page request without destroying //the persistent cache. RedBeanModelsCache::forgetAll(true); $account = Account::getById($accountId); //Test pulling a different CustomField first. This simulates caching the customField $this->assertEquals(array('thing 1', 'thing 2'), unserialize($account->newRelationCstm->data->serializedData)); }