Shopware 6更新数据库表时出现400错误:值需≤255字符
问题:Shopware 6更新clu_product_text表时触发长度限制错误
- 向Shopware 6的
clu_product_text数据库表提交数据时,收到400状态响应,错误提示:This value is too long. It should have 255 character or less. - 目标更新的
description字段已设置为longtext类型,但仍触发该错误
相关前端代码
const { Component} = Shopware; const { Criteria } = Shopware.Data; Component.register('clu-administration-page', { template, inject: [ 'repositoryFactory', 'syncService' ], metaInfo() { return { title: this.$createTitle() }; }, data: function () { return { description: "For writers, a random sentence can help them get their creative juices flowing. Since the topic of the sentence is completely unknown, it forces the writer to be creative when the sentence appears. There are a number of different ways a writer can use the random sentence for creativity. The most common way to use the sentence is to begin a story. Another option is to include it somewhere in the story. A much more difficult challenge is to use it to end a story. In any of these cases, it forces the writer to think creatively since they have no idea what sentence will appear from the tool.", id: "34eh9037492h2781", isLoading: true } }, computed: { cluProductTextRepository() { return this.repositoryFactory.create('clu_product_text'); }, }, methods: { updateCluProductText(){ this.cluProductTextRepository .get(this.id, Shopware.Context.api) .then((update) => { update.description = this.description; this.cluProductTextRepository.save(update, Shopware.Context.api); }); } }, created() { this.updateCluProductText(); } });
问题原因
Shopware 6的字段验证规则定义在实体Schema文件中,而非仅依赖数据库字段类型。哪怕数据库里description是longtext,只要实体定义中给该字段加了maxLength:255的验证约束,API就会拦截长文本并返回错误。
解决方法
- 找到插件中
clu_product_text实体的定义文件,路径一般是src/Core/Content/CluProductText/CluProductTextDefinition.php - 修改
description字段的定义,使用LongTextField并移除长度限制约束:
use Shopware\Core\Framework\DataAbstractionLayer\Field\LongTextField; // 在defineFields方法中更新字段: public function defineFields(): FieldCollection { $fields = parent::defineFields(); $fields->add( new LongTextField('description', 'description') // 删除原来的 ->addConstraint(new LengthConstraint(['max' => 255])) 这一行 ); return $fields; }
- 清除Shopware缓存并刷新插件:
bin/console cache:clear bin/console plugin:refresh bin/console plugin:activate YourPluginName # 替换成你的插件名称
- 前端代码不需要修改,实体验证规则更新后,API就能接受长文本内容了。
内容的提问来源于stack exchange,提问作者Des Tini
相关产品推荐
相关产品推荐

