Skip to content

Support validate custom fields and save custom field values #4

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 9 commits into from
Jun 1, 2023

Conversation

moon-brillar
Copy link
Contributor

No description provided.

@moon-brillar
Copy link
Contributor Author

$attributes = ['zip_code' => '67890'];
$this->user->fill($attributes);
$this->user->save();
$customFieldValue = $this->user->customFieldValues()->where('custom_field_id', $this->customField->id)->first();
Copy link
Contributor

@chenghung chenghung May 31, 2023

Choose a reason for hiding this comment

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

❓ 這個package有機會支援類似下面的語法嗎?

// syntax
$model->getCustomAttribute('attr-name');

// examples: if lead has "zip_code" custom field 
$lead->getCustomAttribute('zip_code'); // return "67890"
$lead->getCustomAttribute('no_exist_attr'); // return null

讓他用起來比較不像是在寫sql query.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

有機會會看時程補上!!感謝建議!!

Copy link
Contributor

@chenghung chenghung left a comment

Choose a reason for hiding this comment

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

@moon-brillar r+ with comments.

Copy link
Contributor

@chenghung chenghung left a comment

Choose a reason for hiding this comment

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

@moon-brillar r+ with comments.

@moon-brillar moon-brillar force-pushed the 3-setup-model-observer-traits-sti branch 2 times, most recently from 669b9c1 to 2762062 Compare June 1, 2023 07:13
@moon-brillar moon-brillar force-pushed the 3-setup-model-observer-traits-sti branch from e0495d2 to f665dec Compare June 1, 2023 09:40
@moon-brillar moon-brillar merged commit fb50229 into main Jun 1, 2023
@moon-brillar moon-brillar deleted the 3-setup-model-observer-traits-sti branch June 1, 2023 09:41
Copy link

@ericHao22 ericHao22 left a comment

Choose a reason for hiding this comment

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

r+ with comments, thanks!!

public function getValidationRule(): array
{
return [
$this->key => ['date'],

Choose a reason for hiding this comment

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

💬 comment

在想這個會不會太寬鬆,之後可以討論一下

];

protected static function newFactory(): Factory
{
return CustomFieldValueFactory::new();
}

public function customField(): BelongsTo

Choose a reason for hiding this comment

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

💬 comment

可能也可以叫做field就好?

Comment on lines +65 to +75
$query = CustomField::query();
if (is_null($context)) {
$customFields = $query->get();
} else {
$customFields = $query
->where('contextable_type', get_class($context))
->where('contextable_id', $context->id)
->get();
}

return $customFields;
Copy link

@ericHao22 ericHao22 Jun 1, 2023

Choose a reason for hiding this comment

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

❗ change request

這邊應該會有一個共同條件是modal class才對?不管context有沒有存在都需要的條件,組成query應該會如下所示:

$query = CustomField::query();
$query->where('model_class', get_class($this));

if ($context) {
    $query->where('contextable_type', get_class($context))
    $query->where('contextable_id', $context->id);
}

return $query->get();

Choose a reason for hiding this comment

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

💬 comment

在拿多型關聯的class name時會建議用modal身上的getMorphClass去拿會比較好,因為多型關聯用的class name是可以客製的

$query->where('contextable_type', $context->getMorphClass());

https://laravel.com/docs/8.x/eloquent-relationships#custom-polymorphic-types

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed, thanks!!

$value = $validatedCustomFieldValues[$customField->key];
$constraints = [
'custom_field_id' => $customField->id,
'customizable_type' => get_class($this),

Choose a reason for hiding this comment

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

💬 comment

多型關聯的class name如同上面提到的

}
$customFields = $this->getCustomFields();
$customFields->each(function ($customField) use ($validatedCustomFieldValues) {
if (array_key_exists($customField->key, $validatedCustomFieldValues)) {

Choose a reason for hiding this comment

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

💬 comment

可以考慮用guard pattern?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

可以!

$values = ['value' => $value];
CustomFieldValue::updateOrCreate($constraints, $values);
}
});

Choose a reason for hiding this comment

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

💬 comment

這裡更新完後要把$this->validatedCustomFieldValues清空嗎?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

這邊應該是不用 (?

/**
* @test
*/
public function custom_field_values_relationship_should_work(): void

Choose a reason for hiding this comment

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

💬 comment

感覺拿custom field value上面的測試多少有涵蓋到了,或許這裡可以著重測試搭配context拿custom fields的部份?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants