Skip to content

Fix custom fields conflict with standard columns #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,18 @@ class Lead extends Model
After setting up the above configurations, you can proceed with the following steps:

1. Define custom fields using the `CustomField` model.
2. Create leads as usual, but with additional custom fields as you defined in the first step.
2. Create leads as you normally would, but include additional custom fields under the custom key, as defined in the previous step.
```php
[
'phone' => '12345678',
'custom' => [
'zip' => '123'
]
]

```
3. If the custom field value passes validation, it will be stored in the `CustomFieldValue` table.
4. Retrieve custom field values just like any other attribute of the Lead model. For example, you can access a custom field value using `$lead->customField`.
4. Retrieve custom field values just like any other attribute of the Lead model but with the prefix `custom`. For example, you can access a custom field value using `$lead->custom_zip`.

By following these steps, you can seamlessly work with custom fields for the Lead model and access their values as if they were regular attributes.

Expand Down
13 changes: 7 additions & 6 deletions src/Concerns/Customizable.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ public function validateCustomFields(): void
if ($customFields->isEmpty()) {
return;
}
$modelAttributeKeys = $modelAttributes->keys();
$customFieldColumns = $modelAttributeKeys->diff($tableColumns);
$unvalidatedCustomFields = $modelAttributes->get('custom');
if (empty($unvalidatedCustomFields)) {
return;
}
$customFieldsRules = $customFields->flatMap(function (CustomField $field) {
return $field->getValidationRule();
})->all();
$customFieldValues = $modelAttributes->only($customFieldColumns)->toArray();
$validator = Validator::make($customFieldValues, $customFieldsRules);
$validator = Validator::make($unvalidatedCustomFields, $customFieldsRules);
$this->validatedCustomFieldValues = $validator->validate();
}

Expand Down Expand Up @@ -115,12 +116,12 @@ public function saveCustomFieldValues(): void
*/
public function loadCustomFieldValues(): void
{
if (! $this->customFieldValues) {
if ($this->customFieldValues->isEmpty()) {
return;
}

$this->customFieldValues->each(function (CustomFieldValue $customFieldValue) {
$attribute = $customFieldValue->customField->key;
$attribute = "custom_{$customFieldValue->customField->key}";
$this->setAttribute($attribute, $customFieldValue->customField->parseValue($customFieldValue->value));
});
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Feature/Concerns/CustomizableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function setUp(): void
'contextable_type' => get_class($this->account)
];
$this->customField = CustomField::factory()->create($attributes);
$this->user = User::factory()->create(['account_id' => $this->account->id, 'zip_code' => '12345']);
$this->user = User::factory()->create(['account_id' => $this->account->id, 'custom' => ['zip_code' => '12345']]);
}

/**
Expand All @@ -43,7 +43,7 @@ public function create_model_should_save_custom_field_value(): void
*/
public function update_model_should_update_custom_field_values(): void
{
$attributes = ['zip_code' => '67890'];
$attributes = ['custom' => ['zip_code' => '67890']];
$this->user->fill($attributes);
$this->user->save();
$customFieldValue = $this->user->customFieldValues()->where('custom_field_id', $this->customField->id)->first();
Expand All @@ -61,6 +61,6 @@ public function create_user_with_invalid_custom_field_value_should_throw_excepti
{
$this->expectException(ValidationException::class);
$this->expectExceptionMessage('The given data was invalid.');
User::factory()->create(['account_id' => $this->account->id, 'zip_code' => 123]);
User::factory()->create(['account_id' => $this->account->id, 'custom' => ['zip_code' => 123]]);
}
}
9 changes: 4 additions & 5 deletions tests/Unit/Concerns/CustomizableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected function setUp(): void
'contextable_type' => $this->account->getMorphClass()
];
$this->customField = CustomField::factory()->create($attributes);
$this->user = User::factory()->create(['account_id' => $this->account->id, 'zip_code' => '12345']);
$this->user = User::factory()->create(['account_id' => $this->account->id, 'custom' => ['zip_code' => '12345']]);
}

/**
Expand All @@ -43,8 +43,7 @@ public function custom_field_values_relationship_should_work(): void
public function validate_custom_fields_should_work()
{
$this->expectException(ValidationException::class);
$this->user->zip_code = 123;
$this->user->validateCustomFields();
$user = User::factory()->create(['account_id' => $this->account->id, 'custom' => ['zip_code' => 123]]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 comment

這邊呼叫create的時候就會拋錯了,下面原本要測試的validateCustomFields方法還會呼叫到嗎?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks! fixed!!!

}

/**
Expand Down Expand Up @@ -73,9 +72,9 @@ public function custom_load_custom_field_values_should_work($type, $value, $expe
];
}
$customField = CustomField::factory()->create($attributes);
$user = User::factory()->create(['account_id' => $this->account->id, 'field' => $value]);
$user = User::factory()->create(['account_id' => $this->account->id, 'custom' => ['field' => $value]]);
$user->loadCustomFieldValues();
$this->assertEquals($expected, $user->field);
$this->assertEquals($expected, $user->custom_field);
}

public function customFieldDataProvider(): array
Expand Down