yii2 小笔记

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_27229113/article/details/83818750
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'=> 'admin',
        'password'=> '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; ?>

        

猜你喜欢

转载自blog.csdn.net/qq_27229113/article/details/83818750
今日推荐