|
@@ -41,20 +41,31 @@ class ValueController
|
|
|
* 保存/更新指定实体的属性值
|
|
* 保存/更新指定实体的属性值
|
|
|
* POST /eav/value
|
|
* POST /eav/value
|
|
|
* body: [
|
|
* body: [
|
|
|
- * { "entity_id": 123, "entity_type_id": 1, "attribute_id": 2, "value": "红色" }
|
|
|
|
|
|
|
+ * { "entity_id": 123, "entity_type_id": 1, "value": [{"attribute_id": 2, value:"红色"}] }
|
|
|
* ]
|
|
* ]
|
|
|
*/
|
|
*/
|
|
|
public function save(Request $request, EavValueEntity $entity): Response
|
|
public function save(Request $request, EavValueEntity $entity): Response
|
|
|
{
|
|
{
|
|
|
$data = $request->post();
|
|
$data = $request->post();
|
|
|
- if (empty($data) || !is_array($data)) {
|
|
|
|
|
|
|
+ if (empty($data)) {
|
|
|
return error_response('参数错误');
|
|
return error_response('参数错误');
|
|
|
}
|
|
}
|
|
|
- // 支持批量写入
|
|
|
|
|
- foreach ($data as $item) {
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // 期望的数据格式:{ entity_id, entity_type_id, values: [{ attribute_id, value }, ...] }
|
|
|
|
|
+ if (!isset($data['values']) || !is_array($data['values'])) {
|
|
|
|
|
+ return error_response('数据格式错误,需要 values 数组');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $entityId = $data['entity_id'] ?? 0;
|
|
|
|
|
+ $entityTypeId = $data['entity_type_id'] ?? 0;
|
|
|
|
|
+
|
|
|
|
|
+ if (!$entityId || !$entityTypeId) {
|
|
|
|
|
+ return error_response('缺少实体 ID 或实体类型 ID');
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 批量写入
|
|
|
|
|
+ foreach ($data['values'] as $item) {
|
|
|
if (
|
|
if (
|
|
|
- empty($item['entity_id']) ||
|
|
|
|
|
- empty($item['entity_type_id']) ||
|
|
|
|
|
empty($item['attribute_id']) ||
|
|
empty($item['attribute_id']) ||
|
|
|
!isset($item['value'])
|
|
!isset($item['value'])
|
|
|
) {
|
|
) {
|
|
@@ -66,8 +77,8 @@ class ValueController
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
$saveData = [
|
|
$saveData = [
|
|
|
- 'entity_id' => $item['entity_id'],
|
|
|
|
|
- 'entity_type_id' => $item['entity_type_id'],
|
|
|
|
|
|
|
+ 'entity_id' => $entityId,
|
|
|
|
|
+ 'entity_type_id' => $entityTypeId,
|
|
|
'attribute_id' => $item['attribute_id'],
|
|
'attribute_id' => $item['attribute_id'],
|
|
|
];
|
|
];
|
|
|
switch ($attr->backend_type) {
|
|
switch ($attr->backend_type) {
|
|
@@ -77,17 +88,23 @@ class ValueController
|
|
|
case 'decimal':
|
|
case 'decimal':
|
|
|
$saveData['value_decimal'] = floatval($item['value']);
|
|
$saveData['value_decimal'] = floatval($item['value']);
|
|
|
break;
|
|
break;
|
|
|
|
|
+ case 'boolean':
|
|
|
|
|
+ $saveData['value_int'] = intval($item['value']);
|
|
|
|
|
+ break;
|
|
|
case 'text':
|
|
case 'text':
|
|
|
$saveData['value_text'] = $item['value'];
|
|
$saveData['value_text'] = $item['value'];
|
|
|
break;
|
|
break;
|
|
|
|
|
+ case 'datetime':
|
|
|
|
|
+ $saveData['value_datetime'] = $item['value'];
|
|
|
|
|
+ break;
|
|
|
case 'varchar':
|
|
case 'varchar':
|
|
|
default:
|
|
default:
|
|
|
$saveData['value_varchar'] = $item['value'];
|
|
$saveData['value_varchar'] = $item['value'];
|
|
|
}
|
|
}
|
|
|
// 先查找是否已存在,存在则更新,否则新增
|
|
// 先查找是否已存在,存在则更新,否则新增
|
|
|
$exist = $entity->where([
|
|
$exist = $entity->where([
|
|
|
- 'entity_id' => $item['entity_id'],
|
|
|
|
|
- 'entity_type_id' => $item['entity_type_id'],
|
|
|
|
|
|
|
+ 'entity_id' => $entityId,
|
|
|
|
|
+ 'entity_type_id' => $entityTypeId,
|
|
|
'attribute_id' => $item['attribute_id']
|
|
'attribute_id' => $item['attribute_id']
|
|
|
])->find();
|
|
])->find();
|
|
|
if ($exist) {
|
|
if ($exist) {
|