ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# 模型 >[info] 模型除了支持db方法外,还增加许多针对性功能,返回数据集,支持更多数据处理功能,比如结果过滤,增加新字段,数据自动完成,转换,多表关联等 1. 如表 test如下 | id|name | | --- | --- | | 1 | one | | 2 | two | | 3 | three | >[info] 模型名称 为*TestModel.php* 2. 过滤 >[info]找出等于1,3的记录 ``` TestModel::select()->filter(function($item){ return in_array($item['id'],[1,3]); }); //结果如下 array(2) { [0] => array(2) { ["id"] => int(1) ["name"] => string(3) "one" } [2] => array(2) { ["id"] => int(3) ["name"] => string(5) "three" } } ``` 3. 临时增加一个字段 >[info]增加字段 title ,等于id+name ``` TestModel::select()->append(['title'])->withAttr('title',function($val,$item){ return $item['id'].'-'.$item['name']; }); //结果如下 array(3) { [0] => array(3) { ["id"] => int(1) ["name"] => string(3) "one" ["title"] => string(5) "1-one" } [1] => array(3) { ["id"] => int(2) ["name"] => string(3) "two" ["title"] => string(5) "2-two" } [2] => array(3) { ["id"] => int(3) ["name"] => string(5) "three" ["title"] => string(7) "3-three" } } ```