首页 > 代码库 > YII2 常用操作记录

YII2 常用操作记录

$statusdb->updateAll([‘status‘=>2], ‘earnestId=:earnestId and type=:type‘,[‘earnestId‘=>$infoArray[1],‘type‘=>4]);//改变状态

修改2个where条件的值


一次修改多个data值

$field = array();
$field[‘depId‘] = $data[‘depId‘];
$field[‘areaId‘] = $data[‘areaId‘];
$field[‘storeName‘] = $data[‘storeName‘];
$field[‘lat‘] = $data[‘lat‘];
        $field[‘lng‘] = $data[‘lng‘];
        $field[‘address‘] = $data[‘address‘];
        $field[‘phone‘] = $data[‘phone‘];
$model->updateAll($field,‘storeId=:storeId‘,array(‘:storeId‘=>$data[‘storeId‘]));


2个二维数组数据插入(使用之前先clone否则值被覆盖)

foreach($data[‘role_parma‘] as $k=> $v1) {
   if (!empty($v1)){
       $_usermodel = clone $usermodel;
       $_usermodel->storeId = $data[‘storeId‘];
       $_usermodel->depId = $data[‘depId‘];
       $_usermodel->role_parma = $v1;
       $_usermodel->userId = $data[‘userId‘][$k];
       $_usermodel->whoCreate = $login[‘userId‘];
       $_usermodel->isDel = 0;
       $_usermodel->save(false);
   }
}

执行原生sql

$connection = Yii::$app->getDb();
$ziduan=explode("=", $result[‘infoAddress‘]);

$sql = "update " . $result[‘tableName‘] . " set " .$result[‘field‘]."=".$result[‘status‘]. " where " .$ziduan[0] ."=" .$ziduan[1]; //修改对方表为同意状态为2
$command = $connection->createCommand($sql);
$command->execute();

join表

public static function getBindInfo($shopId){
   if(empty($shopId)) return array();

   $query = self::find();
   $query->asArray();
   $query->where([‘mn_store_user.depId‘=>$shopId]);
   $query->select("mn_store_user.role_parma,user.*");
   $query->leftJoin(‘mn_user as user‘,‘mn_store_user.userId=user.mnUserId‘);
   $data = $query->all();
   $return = array();
   if($data){
       foreach($data as $val){
           $return[$val[‘role_parma‘]] = $val;
       }
   }
   return $return;

}

//打印sql

 echo $query->createCommand()->getRawSql();exit;

 打印错误

$db->error();

YII2 常用操作记录