yii2 小笔记

yii笔记
linux监测日志
tail -f runtime/logs/app.log
error_log("*******host*******\n".print_r($host, 1)."\n********************\n",3, 'aass.log');

congfig配置
        
'mqtt' => [
        'address' => 'tcp://47.106.178.36',
        'port'    => '61613',
        'username'=> '',
        'password'=> '',
    ],
调用
$jpush = \Yii::$app->params['mqtt']['address'];


域名
\Yii::$app->request->hostInfo

自定义路径
defined('ROOT_DIR') or define('ROOT_DIR', __DIR__);
直接使用ROOT_DIR."/sss/sss"即可

获取配置
param.php 
"equ_model" =>[
       'model_1' => "ME638",
    ],
$weixin=\Yii::$app->params['equ_model']['model_1'];  //获取配置里面的系列号

打印最后一条数据
$query = Salesorder::find()  
  ->where(['order_id'=>[1,2,3,4]])  
  ->select(['order_id'])  
;  

sql调试
$commandQuery = clone $query;  
echo $commandQuery->createCommand()->getRawSql();  
exit;

 $ReSwitchTimer = \app\models\SwitchTimer::find()->andWhere(['in','switch_id',array_column

($switch_ids,'id')]);
        $temp = clone $ReSwitchTimer;
        var_dump($temp->createCommand()->getRawSql());


 $sid = \Yii::$app->db->getLastInsertID();//获取自增id

  $SelfExamination = new \app\models\SelfExamination();
                $SelfExamination->self_id = $utime;
                $SelfExamination->equipment_id = $ids;
                $SelfExamination->box_name = $equ_name;
                $SelfExamination->status = $mon['status'];
                $SelfExamination->self_time = time();
                $SelfExamination->save();
                var_dump($SelfExamination->id);die;//取自增id

调试
新增
  //插入自检单条记录
                $SelfExamination = new \app\models\SelfExamination();
                $SelfExamination->self_id = "$SelfAll->id";
                $SelfExamination->equipment_id = $ids;
                $SelfExamination->box_name = $equ_name;
                $SelfExamination->status = $mon['status'];
                $SelfExamination->self_time = time();
                if (!$SelfExamination->validate()) {
                    var_dump( $SelfExamination->firstErrors);

                } else {
                    $SelfExamination->save();
                }
循环新增多条数据
$model = new User();
foreach($data as $attributes)
{
     $_model = clone $model;
     $_model->setAttributes($attributes);
     $_model->save();
}


判断字段中存在0的是否为空
$l['type'] !== null ? $l['type'] : "";

mongodb 调试
{created:{$gte: new Date('2018-07-05 19:20:00')}}

mongodb 查询存在loadmax字段的数据
 'payload.desired.arrays.0.status.loadmax' => ['$exists' => true]

联表查询
  $query = new \yii\db\Query();
            $query = $query->select("t1.*,t2.*,t3.group_name,t3.is_del")
                ->from(['t1' => 'ss_equipment_group_link'])
                ->leftJoin(['t2' => 'ss_equipment'], "t2.id = t1.equipment_id")
                ->leftJoin(['t3' => 'ss_user_group'], "t3.id = t1.group_id")
                ->where(1);

            if ($name != null) {
                $query = $query->andWhere(['like','t1.equipment_id',$name])->orFilterWhere(['like','t2.serial_number',

$name])->orFilterWhere(['like','t3.group_name',$name]);
            }
            $total = $query->count();

            $result = $query->offset(($page - 1) * $rows)->limit($rows)->all();

$lists = \app\services\EquipmentSwitch::find()->andWhere(['ss_equipment_switch.id'=>$switchId])-

>joinWith('equipment')
->asArray()->all();

 public function getEquipment(){
        // 第一个参数为要关联的子表模型类名,
        // 第二个参数指定 通过子表的equipment_id,关联主表的id字段
        return $this->hasOne(\app\services\Equipment::className(), ['id' => 'equipment_id'])->select

('*');
    }

// 关联goods表
public function getGoods()
{
    // 一关联一 hasOne
    return $this->hasOne(Status::className(), ['id' => 'jihuo']);
}

// 关联user表 并传值 进一步判断
public function getUser($status = 1)
{
    // 一关联多 hasMany ; p_id 被关联表字段, id 关联表字段(当前表字段)
    return $this->hasMany(User::className(), ['p_id' => 'id'])
                ->where('status = :status', [':status' => $status]);
}

关联查询
$list = static::find()
                    ->leftJoin('book',
                        'book.RID = prices.RID')->select('prices.*,book.WCONTENT,book.WIMGURL,book.WURL')
                    ->andFilterWhere(['prices.WMODEL'=> $where_condition['WMODEL']])
                    ->andFilterWhere(['prices.WTYPE'=> $where_condition['WTYPE']])
                    ->offset($params['offset'])
                    ->orderBy(['prices.WCREATETIME' => SORT_DESC])->limit($params['limit'])->asArray()->all();


讲数组作为条件查询
   $switch_ids = \app\models\EquipmentSwitch::find()->select('id')->where(['equipment_id' => 

$equipment_id])->asArray()->all();
        $ReSwitchTimer = \app\models\SwitchTimer::find()->andWhere(['in','switch_id',array_column

($switch_ids,'id')]);


删除
try{
        \app\models\EquipmentGroupLink::deleteAll(['group_id' => $group_id]);
        \app\models\UserGroup::findOne(['id' => $group_id])->delete();
        return $this->returnData('', 1, '删除成功');
       }catch (\Exception $e) {
           print $e->getMessage();
           exit();
       }

修改

 $switch_id = $timer->switch_id;
        \Yii::$app->db->createCommand()->update('ss_switch_timer', ['status' => 0], "switch_id = 
$switch_id and status = 1")->execute();        
        
        // 修改定时状态
        $timer->status = $status;
        $timer->save();


讲校验修改为非必传参数

 // 判断是否要修改密码
            if ($attributes['password']) {
                $attributes['password'] = \Yii::$app->security->generatePasswordHash($attributes['password']);
            } else {
                unset($attributes['password']);
            }

定时任务参数
windows
 D:\wamp\www\smartSPACE>yii crontab/copy-db,你需要配置你的php环境变量

linux
*/1 * * * * /usr/local/php/bin/php /home/wwwroot/smartSPACE/yii crontab/copy-db

/usr/local/php/bin/php /home/wwwroot/smartSPACE/yii crontab/copy-db

mongodb 新增
$collection = \Yii::$app->mongodb->getCollection('timed_task');
        $collection->insert(['end_time' => $end_time]);
查询
$last_result = $query->select(['end_time'])->from('timed_task')->orderBy('_id desc')->limit(1)->one();


前端部分
if判断
<?php if ($manager->adminid != 1): ?>
                <a href="<?php echo yii\helpers\Url::to(['manage/del', 'adminid' => 

$manager->adminid]) ?>">删除</a>
             <?php endif; ?>

orderby排序type
  //查询新的表
            $query = new \yii\db\Query();
            $query = $query->select("t1.*,t2.alias,t3.alias as equ_alias")
            ->from(['t1' => 'ss_fault_message'])
            ->leftJoin(['t2' => 'ss_alias'], "t2.extend_id = t1.switch_alias")
            ->leftJoin(['t3' => 'ss_alias'], "t3.extend_id = t1.equipment_id")
            ->where(1)
            ->andWhere(['t1.user_id' => $this->uid])
            ->groupBy('t1.id')
            ->orderBy(["FIELD(`status`,0,1)"=>true, 'id'=>'desc']);
            $total= $query->count();
            $default = $query->all();

 $list = \app\models\SystemMessage::find()
            ->select('*')
            ->where(['or',['user_id'=>$this->uid],'user_id=0'])
            ->offset(($page - 1) * $rows)
            ->limit($rows)
            ->orderBy(
                [
                    'is_read' => SORT_ASC, //是否报满正序
                    'id' => SORT_DESC, //编号排序倒序
                ]
            )
            ->asArray()->all();

根据类型type统计
$fault_message = \app\models\FaultMessage::find();
        $activeRecord = $fault_message->select(['count(event) as event','sum(event=1) as event_1', 'sum(event=2) as 

event_2',
            'sum(event=3) as event_3', 'sum(event=4) as event_4', 'sum(event=5) as event_5','sum(event=6) as event_6',
            'sum(event=7) as event_7','sum(event=8) as event_8','sum(event=9) as event_9'])
            ->andWhere(['between', 'create_time', $ts_data, $te_data])->andWhere(['>', 'event', 0])->asArray()->one();

        

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可以吧可以吧

打赏可以获得大长腿妹子微信

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值