$datum) { if ($model = SpecValue::findOne(['id' => $datum, 'spec_id' => $spec_id])) { $model->title = $data['update']['title'][$key]; $model->sort = (int)$data['update']['sort'][$key]; $model->save(); $allIds[] = $model->id; } } } // 创建的内容 if (isset($data['create'])) { $rows = []; foreach ($data['create']['title'] as $key => $datum) { $rows[] = [$merchant_id, $spec_id, $datum, (int)$data['create']['sort'][$key], time(), time()]; } $field = ['merchant_id', 'spec_id', 'title', 'sort', 'created_at', 'updated_at']; !empty($rows) && Yii::$app->db->createCommand()->batchInsert(SpecValue::tableName(), $field, $rows)->execute(); } // 删除不存在的内容 $deleteIds = []; foreach ($oldValues as $value) { !in_array($value['id'], $allIds) && $deleteIds[] = $value['id']; } !empty($deleteIds) && SpecValue::deleteAll(['in', 'id', $deleteIds]); } /** * @param array $ids * @return array|\yii\db\ActiveRecord[] */ public function findByIds(array $ids) { return SpecValue::find() ->andWhere(['in', 'id', $ids]) ->andFilterWhere(['merchant_id' => $this->getMerchantId()]) ->asArray() ->all(); } }