Jeter
- Jeter 回答了问题 请问 Yii2 limit 连贯查询怎么用?
$limit = 10;
$count = Products::find()->count();//总记录数
$pagination = new Pagination(['totalCount' => $count, 'pageSize' => $limit]);
Products::find()->where($where) ->offset($pagination->offset) ->limit($pagination->limit) ->orderBy('`sort`') ->all();
这就是分页
not NUll
避免全表扫描- Jeter 回答了问题 Yii SQL 语句会出现转义符 `
仅供参考:
WeixinCourse::find()
->where(['LIKE', 'name
', $xxxx.'%', false])//%这样放,可以使name索引(设置了索引的话)生效
->andWhere(['status'=>1])
->orWhere(['LIKE', 'description
', $xxxxX.'%', false])//%这样放,可以使description索引(设置了索引的话)生效
->orderBy("if(instr(name,'{$w}') >0,1,0) desc,id desc")
->all(); 配置组件,如cache,redis,feehi配置,微信、支付宝配置等等,都可以在里面配置
10000 个人?太小看redis,100000个都没问题,注意设置过期时间,不然永久保存,到时真的内存搞崩了
如main.php里面:
'components' => ['cache' => [ //'class' => yii\caching\DummyCache::className(),//虚拟缓存 'class'=> 'yii\redis\Cache',//redis缓存 'keyPrefix' => 'api', // 唯一键前缀 ],
];
获取方法: yii::$app->cacheparams.php里面就是:yii::$app->params['cache']
- Jeter 回答了问题 请问一下如何把这个查询结果变成数组?
$keyword_name_search = KeywordsModel::find()->where(['keyword_name'=>$model->keyword_name])->isArray()->all();
common 下的 config 文件夹内
main-local.php
文件内配置全部 redis:'redis' => [ 'class' => 'yii\redis\Connection', 'hostname' => 'localhost', 'port' => 6379, 'database' => 0, //'password' => '', ],
项目内任何 php 文件调用 redis:
一、存储字符串$redis = Yii::$app->redis
$redis->exists($key);
=> 是否存在$redis->set($data);
=> 添加数据到redis$redis->expire($key, $expire);
=> 设置生存周期(秒)$redis->get($key);
=> 获取redis数据二、获取、存储hash数据
1、Yii::$app->redis->hget($key, $hashKey);
=> 获取redis hash数据
2、Yii::$app->redis->hset($key, $hashKey, $data);
=> 设置redis hash数据- Jeter 回答了问题 Yii2 查询构造器问题
这样也行(User是model模型):
User::find()->select('id', 'email')->where(['last_name' => 'Smith'])->limit(10)->all()
本地没有设置密码。不让通过。难道一定要设置密码