yii2学习笔记——基础使用操作

  1. yii2通过require(DIR…/models/xx.php)方式引入类的方式
      require_once('../models/phpqrcode.php');
      $qrcode=new \QRcode();
      $qrcode::png('http://xxxx/');

其中QRcode为我引入的php文件中定义的类,实例化使用时需要在类名前添加反斜杠,否则会报app/models/QRcode not found

此外查询资料发现一些比较好的引入文件的方法,但并未尝试

用namespace+autoload,看文档自动加载章节

或者

$this->render,因为不会有变量名冲突,不建议直接includerequire

2.yii2继承ActiveRecord的类,在处理数据库操作时,自带防SQL语句注入功能,因此不需要自增安全措施
如下是防JS语句注入功能

use yii\helpers\Html;
use yii\helpers\HtmlPurifier;

echo Html::encode($view_hello_str) //可以原样显示<script></script>代码  
echo HtmlPurifier::process($view_hello_str)  //可以过滤掉<script></script>代码 

3.yii2中一些比较常用的数据库操作

 use app\models\user

 User::find()->where(['property'=>$username])->one();取出一条

 User::find()->where(['property'=>$username])->all();取出所有

 User::find()->where(['property'=>$username])->asArray()->one();yii2取出数组格式的数据

 User::find()->where(['property'=>$username])->orderBy('property asc')->one();按升序取数据,默认降序

 User::find()->where(['property1'=>$username,'property2'=>$userid])->one();yii2联合查询

 User::find()->where(['property'=>$username])->andwhere('<>','property','$username')->one();不方便使用上一种方法时可以考虑的,此处<>意为不等于
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值