1. 程式人生 > >yii2 小筆記

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'=> '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; ?>