-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
$attributes = ['zip_code' => '67890']; | ||
$this->user->fill($attributes); | ||
$this->user->save(); | ||
$customFieldValue = $this->user->customFieldValues()->where('custom_field_id', $this->customField->id)->first(); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
有機會會看時程補上!!感謝建議!!
There was a problem hiding this 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.
There was a problem hiding this 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.
669b9c1
to
2762062
Compare
…e custom field values
e0495d2
to
f665dec
Compare
There was a problem hiding this 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'], |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 comment
可能也可以叫做field就好?
$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; |
There was a problem hiding this comment.
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();
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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), |
There was a problem hiding this comment.
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)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 comment
可以考慮用guard pattern?
There was a problem hiding this comment.
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); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💬 comment
這裡更新完後要把$this->validatedCustomFieldValues
清空嗎?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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的部份?
No description provided.