- 模型
- orderby的使用:
->orderBy(['addtime'=>SORT_DESC, 'sort'=>SORT_ASC])->all() - 在使用find()查询的时候, 指定查询字段:
find()->select('id, title, content') 指定查询的字段 - 块赋值, 使用attributes, 比如 $psychological->attributes = $input; 把数组一次性赋值给attributes 属性, 但是要注意, 要确保模型类中的rules方法, 已经包含了要赋值的字段. 否则attributes 属性接收不到值. 就不能保存成功
- where 作为查询条件单独拿出来的时候, 想使用 < > >&#61; <&#61; <> 进行范围查询的时候, 要怎么写?
$where &#61; [&#39;and&#39;,[&#39;<&#39;, &#39;minscore&#39;, $score],[&#39;>&#39;, &#39;maxscore&#39;, $score],];
//查询满足minscore<$score并且maxscore>$score 的记录 - yii2中同时连接两个或以上数据库:(如果在本地开发完,传到线上服务器, 需要把配置的数据库的用户名和密码改成线上数据库的
)- 在config目录下web.php文件中的components数组里配置
&#39;db2&#39; &#61;> [&#39;class&#39; &#61;> &#39;yii\db\Connection&#39;,&#39;dsn&#39; &#61;> &#39;mysql:host&#61;localhost;dbname&#61;quickapp&#39;,&#39;username&#39; &#61;> &#39;root&#39;,&#39;password&#39; &#61;> &#39;root&#39;,&#39;charset&#39; &#61;> &#39;utf8&#39;,],
-
在继承ActiveRecord的模型中设置表名,rules等,必须要注意一点, yii默认连接的是components里面的db设置的数据库, 所以当连接其他数据库的时候, 就必须要重写 getDb() 方法, 很简单
public static function getDb(){return \Yii::$app->db2; //db2就是components里的db2下标}
OK, 可以使用了.
- 在config目录下web.php文件中的components数组里配置
- yii打印SQL语句:
echo RecycleModel::find()->alias(&#39;r&#39;)->select(&#39;r.name as rubbish, c.id, c.name, c.code, c.inc, c.des,c.req&#39;)->leftJoin([&#39;c&#39;&#61;>RecycleCateModel::tableName()], &#39;r.category_id&#61;c.id&#39;)->where($where)->orderBy([&#39;modified&#39;&#61;>SORT_DESC])->limit(&#39;10&#39;)->asArray()->createCommand()->getRawSql();exit;
-
添加数据()
$usercode &#61; \Yii::$app->db->createCommand()->batchInsert(UserVoucher::tableName(), [&#39;code&#39;, &#39;cat_id&#39;,&#39;userId&#39;, &#39;gettime&#39;, &#39;expire&#39;], [[$code[&#39;code&#39;], $id, $userId, time(), $code[&#39;expire&#39;]],])->execute(); // \Yii::$app->db 这里的db, 如果换成db2, 就是往db2数据库里插入数据.
- 连表查询分页
$count &#61; VoucherCode::find()
->alias(&#39;v&#39;)
->leftJoin([&#39;c&#39;&#61;>RubbishCate::tableName()], &#39;v.cat_id&#61;c.id and v.is_delete&#61;0&#39;)
->where($where)
->count(); //查询符合连表数据总数
$p &#61; new Pagination([&#39;totalCount&#39;&#61;>$count, &#39;pageSize&#39;&#61;>15]);
$code &#61; VoucherCode::find()
->alias(&#39;v&#39;)
->leftJoin([&#39;c&#39;&#61;>RubbishCate::tableName()], &#39;v.cat_id&#61;c.id and v.is_delete&#61;0&#39;)
->select(&#39;v.*, c.name&#39;)
->where($where)
->offset($p->offset)
->limit($p->limit)
->asArray()
->all(); //查询数据
return $this->render(&#39;/rubbish-voucher/list&#39;, [&#39;code&#39;&#61;>$code, &#39;pagination&#39;&#61;>$p]);
//views视图调用&#39;pagination&#39; &#61;> $pagination,
&#39;prevPageLabel&#39; &#61;> &#39;上一页&#39;,
&#39;nextPageLabel&#39; &#61;> &#39;下一页&#39;,
&#39;firstPageLabel&#39; &#61;> &#39;首页&#39;,
&#39;lastPageLabel&#39; &#61;> &#39;尾页&#39;,
&#39;maxButtonCount&#39; &#61;> 5,
&#39;options&#39; &#61;> [
&#39;class&#39; &#61;> &#39;pagination&#39;,
],
&#39;prevPageCssClass&#39; &#61;> &#39;page-item&#39;,
&#39;pageCssClass&#39; &#61;> "page-item",
&#39;nextPageCssClass&#39; &#61;> &#39;page-item&#39;,
&#39;firstPageCssClass&#39; &#61;> &#39;page-item&#39;,
&#39;lastPageCssClass&#39; &#61;> &#39;page-item&#39;,
&#39;linkOptions&#39; &#61;> [
&#39;class&#39; &#61;> &#39;page-link&#39;,
],
&#39;disabledListItemSubTagOptions&#39; &#61;> [&#39;tag&#39; &#61;> &#39;a&#39;, &#39;class&#39; &#61;> &#39;page-link&#39;],
])
?> -
$status &#61; \Yii::$app->request->get(&#39;status&#39;, &#39;&#39;); //获取用户传入的条件
$where &#61; [];
if($status !&#61; &#39;&#39;){ $where[&#39;status&#39;] &#61; $status;
}
$where[&#39;is_delete&#39;] &#61; 0; -
//课程表和用户表多对多关联, 课程course模型里获取用户表字段, 中间表为 user_course, 中间表写在viaTable()里
public function getUser(){return $this->hasMany(User::className(), [&#39;id&#39;&#61;>&#39;uid&#39;])->viaTable(&#39;user_course&#39;, [&#39;course_id&#39;&#61;>&#39;id&#39;]);}
//或者使用via()
//获取关联表的属性
$user &#61; Course::findOne($v[&#39;id&#39;])->user;
//统计有多少人
$num &#61; count(Course::findOne($v[&#39;id&#39;])->user); -
$count &#61; Course::find()->where($where)->count(); //数据总数
$p &#61; new Pagination([&#39;totalCount&#39;&#61;>$count, &#39;pageSize&#39;&#61;>$pagesize]); //实例化分页类
$course &#61; Course::find()->select(&#39;id, title, pic_url, sections, teacher, fee, free&#39;)->where($where)->offset(($page-1)*$pagesize)->limit($p->limit)->all(); //前端传入第几页$page和每页显示多少条$pagesize, 主要是offset方法里的参数怎么写 -
- orderby的使用: