swoft
swoft copied to clipboard
swoft模型getter带逗号分隔符的字段或json字段,但在更新或插入时报错
Q | A |
---|---|
Bug report? | no |
Feature request? | yes |
Swoft version | 2.0.9 (by php bin/swoft -V ) |
Swoole version | 4.4.12 (by php --ri swoole ) |
PHP version | 7.3.11 (by php -v ) |
Runtime environment | Ubuntu 18.04 |
Describe the bug 我有个商品表,其中attr_lists保存了json字符串,images保存了 英文逗号分隔的字符串;使用php bin/swoft entity:create 生成Goods.php实体之后,手动修改了这两个字段的getter:
/**
* @return string
*/
public function getImages(): string
{
return $this->images;
}
/**
* @return string|null
*/
public function getAttrLists(): ?string
{
return $this->attrLists;
}
以下是修改后的
/**
* @return array
*/
public function getImages(): array
{
return $this->images ? explode(',', $this->images) : [];
}
/**
* @return array
*/
public function getAttrLists(): ?array
{
return $this->attrLists ? json_decode($this->attrLists, 320) : [];
}
这样在使用Goods模型读取数据没有问题,但在update 或 insert的时候报错如下:
我注意到文档示例有一个字段标注了array,但尝试之后对我没有帮助:
就是想实现TP5和lavael的获取器(转换字段格式)功能,但不影响写入操作,是我的用法不对吗?抱歉我不确定是这个是bug,也没找到咱们swoft的计论区,所以请团队帮助解答疑惑,不胜感激。
实体保存会 调用 getter 看看执行的 sql 语句 估计是因为转义字符的问题
getAttrLists 和 getImages 更新 插入框架会自动调用
需要和数据库存储保持一致需要是 string 业务需要 改个名字不要叫这个名字就不会受影响了
array 类型 只会对 json 有效果