Yii2给数据库表添加字段后对应模型无法识别到该属性的原因和解决办法
应为数据库表的结构被缓存了。删除runtime文件夹或者执行
//清理指定表结构缓存数据
Yii::$app->db->getSchema()->refreshTableSchema('{{%post}}');//这里post是出去表前缀的表名
//清理所有表结构缓存数据
Yii::$app->db->getSchema()->refresh();
在通过PowerDesigner将数据库的物理设计图转化为实际的数据库时,有时候导入数据库成功后,打开一些表查看时,会发现一些属性名称被系统自动地添加上了中括号“[ ]”。
这是因为这些属性的命名刚好是相关数据库中的关键字,这样虽然也能成功建库,但是有可能在使用代码(如运用Java代码在Hibernate框架下访问数据库时)访问数据库时会发生错误,同样,如果数据库名刚好是叫“User”、刚好是关键字(保留字)时,也会发生错误。
我们要解决这些问题的方法其实也很简单,就是对相关出现中括号的属性进行改名,可以通过添加前缀以及下划线进行命名和区分保留字。
同样,数据库中相关数据库的库名、表名都最好不要是关键字(保留字)。
如何为商品表添加上属性,属性值?属性对于商品而言肯定是一对多的,属性值对于属性也是一对多的。那么在不知道这个商品有哪些属性,属性下又有哪些属性值得情况下,如何来设计这个呢?商品的属性在不同的属性值下价格、库存、图片等都是不一样的eg:一个手机128G 版本的肯定和16G版本的价格不一样吧。
对于这个问题,我认为既然一件商品的属性在不同属性值下相当于另外一件商品。那么在这个商品的基础上应该拥有他的子商品集,关系也是一对多。即:我们要给商品加上属性这一元素,需要添加三张表,属性表、属性值表、子商品表。下面给出表的结构:
-- -- 表的结构 `goods` 商品表 -- CREATE TABLE `goods` ( `GoodsId` int(11) NOT NULL COMMENT '商品id', `GoodsName` varchar(50) NOT NULL COMMENT '商品名称', `Price` float NOT NULL COMMENT '价格', `MarketPrice` float NOT NULL COMMENT '市场价', `SalVolume` int(10) NOT NULL COMMENT '销量', `GoodsImages` varchar(300) NOT NULL COMMENT '商品图片', `GoodsClass` varchar(20) NOT NULL COMMENT '商品类型', `GoodsDetails` text NOT NULL COMMENT '商品描述', `goodstag` varchar(50) NOT NULL COMMENT '商品标签' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- 表的结构 `goodsitem` 子商品表 -- CREATE TABLE `goodsitem` ( `goodsitemid` int(11) NOT NULL COMMENT '子商品id', `goodsid` int(11) NOT NULL COMMENT '商品id', `itemprice` float NOT NULL COMMENT '子商品价格', `iteminventory` int(11) NOT NULL COMMENT '库存量', `valuesids` varchar(50) NOT NULL COMMENT '属性值id', `title` varchar(50) NOT NULL COMMENT '商品标题', `pics` varchar(200) NOT NULL COMMENT '商品图片' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- 表的结构 `goodsitemkey` 商品属性表 -- CREATE TABLE `goodsitemkey` ( `keyid` int(11) NOT NULL COMMENT '主键Id', `goodsid` int(11) NOT NULL COMMENT '商品Id', `keyname` varchar(50) NOT NULL COMMENT '属性名' ) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- -- 表的结构 `goodsitemvalues` 商品属性值表 -- CREATE TABLE `goodsitemvalues` ( `valuesid` int(11) NOT NULL COMMENT '属性值id', `keyid` int(11) NOT NULL COMMENT '属性id', `valuesname` varchar(50) NOT NULL COMMENT '属性名' ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
需要注意的是在给子商品表添加数据时需要保证数据完整性,即他应该是属性名和属性值交叉得出的结果,valuesids必须描述完所有的属性值eg:属性名有 (keyid = 1) 颜色 (keyid = 2)大小
属性值有 (valuesid = 1 )白色 (valuesid = 2 )黑色 (valuesid =3) 红色
(valuesid = 4 )小 (valuesid = 5 )中 (valuesid = 6) 大
那么对于子商品valuesids的描述应该有 valuesids= 1,4 ; valuesids=1,5;valuesids=1,6;valuesids= 2,4 ; valuesids=2,5;valuesids=2,6;valuesids= 3,4 ; valuesids=3,5;valuesids=3,6。
最后,这只是我的最开始时的思路,可能不完美,但是这个思考过程是很nice的
{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里云数据库专家保驾护航,为用户的数据库应用系统进行性能和风险评估,参与配合进行数据压测演练,提供数据库优化方面专业建议,在业务高峰期与用户共同保障数据库系统平稳运行。","link1":"https://www.aliyun.com/service/optimization/database","link":"https://www.aliyun.com/service/chiefexpert/database","icon":"https://img.alicdn.com/tfs/TB1a5ZfonnI8KJjy0FfXXcdoVXa-100-100.png","btn2":"数据库紧急救援服务","tip":"还有更多专家帮助您解决云上业务问题:立即查看","btn1":"云上数据库优化服务","link2":"https://www.aliyun.com/service/databaserescue","title":"数据库专家服务"}],"search":[{"txt":"安全首席专家","link":"https://www.aliyun.com/service/chiefexpert/security"},{"txt":"中间件首席专家","link":"https://www.aliyun.com/service/chiefexpert/middleware"},{"txt":"系统与平台首席专家","link":"https://www.aliyun.com/service/chiefexpert/platform"},{"txt":"存储首席专家","link":"https://www.aliyun.com/service/chiefexpert/storage"}],"countinfo":{"search":{"length_pc":0,"length":0},"card":{"length_pc":0,"length":0}},"simplifiedDisplay":"newEdition","newCard":[{"link":"https://www.aliyun.com/product/rds/mysql","icon":"rds_mysql1","contentLink":"https://www.aliyun.com/product/rds/mysql","title":"云数据库RDS MySQL版","des":"云数据库RDS MySQL版 MySQL 是全球最受欢迎的开源数据库,阿里云MySQL版 通过深度的内核优化和独享实例提供稳定极致的数据库性能,同时灵活的部署架构及产品形态,可满足不同场景下的数据库需求。","btn1":"了解详情","link1":"https://www.aliyun.com/product/rds/mysql","btn2":"管理控制台","link2":"https://rdsnext.console.aliyun.com/#/rdsList/cn-hangzhou/basic/","btn3":"在线咨询","link3":"https://www.aliyun.com/core/online-consult?spm=5176.8789780.7y9jhqsfz.115.47ea55caeL2Mfu&from=OVtXEBmlH8","infoGroup":[{"infoName":"最新活动","infoContent":{"firstContentName":"MySQL 1年仅19.9元","firstContentLink":"https://www.aliyun.com/database/dbfirstbuy#J_9036464270","lastContentName":"ECS+MySQL 0.7元/日","lastContentLink":"https://www.aliyun.com/1111/enterprise"}},{"infoName":"快速入门","infoContent":{"firstContentName":"选型指南","firstContentLink":"https://help.aliyun.com/document_detail/164594.html?spm=a2c4g.11186623.6.551.56b67384ispvvk","lastContentName":"上手指南","lastContentLink":"https://help.aliyun.com/document_detail/26124.html?spm=a2c4g.11174283.2.16.51795b83pbkzav"}},{"infoName":"最佳实践","infoContent":{"firstContentName":"X-Engine的应用场景","firstContentLink":"https://help.aliyun.com/document_detail/148402.html?spm=a2c4g.11186623.2.14.3f592685zjuUxu#concept-2364901","lastContentName":"读写分离功能","lastContentLink":"https://help.aliyun.com/document_detail/96073.html?spm=a2c4g.11174283.2.53.42db5b83VvZvMJ"}},{"infoName":"产品推荐","infoContent":{"firstContentName":"云数据库PolarDB","lastContentName":"云原生数据仓库MySQL","firstContentLink":"https://www.aliyun.com/product/polardb","lastContentLink":"https://www.aliyun.com/product/ads"}}]}],"visual":{"textColor":"dark","topbg":""}}
{"$env":{"JSON":{}},"$page":{"env":"production"},"$context":{"moduleinfo":{"card_count":[{"count_phone":1,"count":1}],"search_count":[{"count_phone":4,"count":4}]},"card":[{"des":"阿里云数据库专家保驾护航,为用户的数据库应用系统进行性能和风险评估,参与配合进行数据压测演练,提供数据库优化方面专业建议,在业务高峰期与用户共同保障数据库系统平稳运行。","link1":"https://www.aliyun.com/service/optimization/database","link":"https://www.aliyun.com/service/chiefexpert/database","icon":"https://img.alicdn.com/tfs/TB1a5ZfonnI8KJjy0FfXXcdoVXa-100-100.png","btn2":"数据库紧急救援服务","tip":"还有更多专家帮助您解决云上业务问题:立即查看","btn1":"云上数据库优化服务","link2":"https://www.aliyun.com/service/databaserescue","title":"数据库专家服务"}],"search":[{"txt":"安全首席专家","link":"https://www.aliyun.com/service/chiefexpert/security"},{"txt":"中间件首席专家","link":"https://www.aliyun.com/service/chiefexpert/middleware"},{"txt":"系统与平台首席专家","link":"https://www.aliyun.com/service/chiefexpert/platform"},{"txt":"存储首席专家","link":"https://www.aliyun.com/service/chiefexpert/storage"}],"countinfo":{"search":{"length_pc":0,"length":0},"card":{"length_pc":0,"length":0}},"simplifiedDisplay":"newEdition","newCard":[{"link":"https://www.aliyun.com/product/rds/mysql","icon":"rds_mysql1","contentLink":"https://www.aliyun.com/product/rds/mysql","title":"云数据库RDS MySQL版","des":"云数据库RDS MySQL版 MySQL 是全球最受欢迎的开源数据库,阿里云MySQL版 通过深度的内核优化和独享实例提供稳定极致的数据库性能,同时灵活的部署架构及产品形态,可满足不同场景下的数据库需求。","btn1":"了解详情","link1":"https://www.aliyun.com/product/rds/mysql","btn2":"管理控制台","link2":"https://rdsnext.console.aliyun.com/#/rdsList/cn-hangzhou/basic/","btn3":"在线咨询","link3":"https://www.aliyun.com/core/online-consult?spm=5176.8789780.7y9jhqsfz.115.47ea55caeL2Mfu&from=OVtXEBmlH8","infoGroup":[{"infoName":"最新活动","infoContent":{"firstContentName":"MySQL 1年仅19.9元","firstContentLink":"https://www.aliyun.com/database/dbfirstbuy#J_9036464270","lastContentName":"ECS+MySQL 0.7元/日","lastContentLink":"https://www.aliyun.com/1111/enterprise"}},{"infoName":"快速入门","infoContent":{"firstContentName":"选型指南","firstContentLink":"https://help.aliyun.com/document_detail/164594.html?spm=a2c4g.11186623.6.551.56b67384ispvvk","lastContentName":"上手指南","lastContentLink":"https://help.aliyun.com/document_detail/26124.html?spm=a2c4g.11174283.2.16.51795b83pbkzav"}},{"infoName":"最佳实践","infoContent":{"firstContentName":"X-Engine的应用场景","firstContentLink":"https://help.aliyun.com/document_detail/148402.html?spm=a2c4g.11186623.2.14.3f592685zjuUxu#concept-2364901","lastContentName":"读写分离功能","lastContentLink":"https://help.aliyun.com/document_detail/96073.html?spm=a2c4g.11174283.2.53.42db5b83VvZvMJ"}},{"infoName":"产品推荐","infoContent":{"firstContentName":"云数据库PolarDB","lastContentName":"云原生数据仓库MySQL","firstContentLink":"https://www.aliyun.com/product/polardb","lastContentLink":"https://www.aliyun.com/product/ads"}}]}],"visual":{"textColor":"dark","topbg":""}}}
Yii2给数据库表添加字段后对应模型无法识别到该属性的原因和解决办法
应为数据库表的结构被缓存了。删除runtime文件夹或者执行
//清理指定表结构缓存数据
Yii::$app->db->getSchema()->refreshTableSchema('{{%post}}');//这里post是出去表前缀的表名
//清理所有表结构缓存数据
Yii::$app->db->getSchema()->refresh();
转载于:https://www.cnblogs.com/achengmu/p/9844909.html