首页 > 其他分享 >yii2代码封装

yii2代码封装

时间:2024-07-28 15:06:24浏览次数:18  
标签:DutyTotalDays OvertimeMemo 封装 代码 values key sql array yii2

1、批量更新某个字段

/**
     * @throws CDbException
     * @throws CException
     * update xxxTable set column1 = case pk
     *        when whenData1 then caseData1
     *        ...
     * END
     * where id in (1,2,3...)
     */
    public function batchUpdateColumnByPK($pk, $params)
    {
        if (!$params || !$pk) {
            return false;
        }
        $sql = ' UPDATE ' . $this->tableName() . ' SET ';
        $count = 0;
        $pks = [];
        foreach ($params as $column => $whenCaseMaps) {
            $totalCount = count($whenCaseMaps);
            $sql .= ' `' . $column . '` ' . ' = CASE ' . $pk;
            foreach ($whenCaseMaps as $when => $then) {
                $sql .= ' WHEN \'' . $when . '\' THEN \'' . $then . '\' ';
                if (!in_array($when, $pks)) {
                    $pks[] = $when;
                }
                ++$count;
            }
            if ($count == $totalCount) {
                $sql .= ' END ';
            } else {
                $sql .= ' END, ';
            }
        }
        $sql .= ' WHERE ' . $pk . ' IN (' . implode(',', $pks) . ' );';
        return $this->getDbConnection()->createCommand($sql)->execute();
    }

 2、批量更新或插入

/**
     * @throws CDbException
     * @throws CException
     */
    public function saveAll($arrays){

        $arrayMap = [];
        $values = "";
        foreach ($arrays as $key => $array){
            if (!isset($array['OvertimeTotalDays'])) {
                $array['OvertimeTotalDays'] = 0;
            }
            if (!isset($array['DutyTotalDays'])) {
                $array['DutyTotalDays'] = 0;
            }
            if (!isset($array['ConvertedDays'])) {
                $array['ConvertedDays'] = 0;
            }
            if (!isset($array['OvertimeMemo'])) {
                $array['OvertimeMemo'] = '';
            }
            if (!isset($array['DutyMemo'])) {
                $array['DutyMemo'] = '';
            }
            foreach ($array as $k => $column){
                $arrayMap[$k.$key] = $column;
            }
            if ($key != sizeof($arrays)-1){
                $values.="(:StaffID".$key.",:Month".$key.",:Workdays".$key.",:LeaveDays".$key.",:LeaveInfo".$key.",:PaidLeave".$key.",:FullAttendancePrice".$key.",:OvertimePrice".$key.",:LateNumbers".$key.","
                    . ":LeaveEarlyNumbers".$key.",:NoSignNumbers".$key.",:AbsentNumbers".$key.",:NoWorkNumbers".$key.",:Memo".$key.",:Status".$key.",:Islatest".$key.",:TiaoxiuDays".$key.",:TiaoxiuInfo".$key.","
                    .":SickDays".$key.",:SickInfo".$key.", :PaidSick".$key.", :CommonSick".$key.", :LateTimes".$key.",:EarlyTimes".$key.",:Foodsubsidy".$key.",:Carsubsidy".$key.",:Suppersubsidy".$key.",:OvertimeTotalDays".$key.",:DutyTotalDays".$key.",:ConvertedDays".$key.",:OvertimeMemo".$key.",:DutyMemo".$key."),";
            }else{
                $values.="(:StaffID".$key.",:Month".$key.",:Workdays".$key.",:LeaveDays".$key.",:LeaveInfo".$key.",:PaidLeave".$key.",:FullAttendancePrice".$key.",:OvertimePrice".$key.",:LateNumbers".$key.","
                    . ":LeaveEarlyNumbers".$key.",:NoSignNumbers".$key.",:AbsentNumbers".$key.",:NoWorkNumbers".$key.",:Memo".$key.",:Status".$key.",:Islatest".$key.",:TiaoxiuDays".$key.",:TiaoxiuInfo".$key.","
                    .":SickDays".$key.",:SickInfo".$key.", :PaidSick".$key.", :CommonSick".$key.", :LateTimes".$key.",:EarlyTimes".$key.",:Foodsubsidy".$key.",:Carsubsidy".$key.",:Suppersubsidy".$key.",:OvertimeTotalDays".$key.",:DutyTotalDays".$key.",:ConvertedDays".$key.",:OvertimeMemo".$key.",:DutyMemo".$key.") ";
            }
        }
        $sql = "insert into V2_OADB.tblAttendanceMonthInfo(StaffID,Month,Workdays,LeaveDays,LeaveInfo,PaidLeave,FullAttendancePrice,OvertimePrice,"
            ."LateNumbers,LeaveEarlyNumbers,NoSignNumbers,AbsentNumbers,NoWorkNumbers,Memo,Status,Islatest,TiaoxiuDays,TiaoxiuInfo,SickDays,"
            ."SickInfo,PaidSick, CommonSick, LateTimes,EarlyTimes,Foodsubsidy,Carsubsidy,Suppersubsidy,OvertimeTotalDays,DutyTotalDays,ConvertedDays,OvertimeMemo,DutyMemo) "
            . "values".$values." on duplicate key update "
            . "Workdays=values(Workdays),LeaveDays=values(LeaveDays),LeaveInfo=values(LeaveInfo),PaidLeave=values(PaidLeave),FullAttendancePrice=values(FullAttendancePrice),"
            . "OvertimePrice=values(OvertimePrice),LateNumbers=values(LateNumbers),LeaveEarlyNumbers=values(LeaveEarlyNumbers),NoSignNumbers=values(NoSignNumbers),"
            . "AbsentNumbers=values(AbsentNumbers),NoWorkNumbers=values(NoWorkNumbers),Memo=values(Memo),Status=values(Status),Islatest=values(Islatest),"
            ."TiaoxiuDays=values(TiaoxiuDays),TiaoxiuInfo=values(TiaoxiuInfo),SickDays=values(SickDays),PaidSick=values(PaidSick), CommonSick=values(CommonSick), SickInfo=values(SickInfo),IsManualEdit=values(IsManualEdit),"
            ."LateTimes=values(LateTimes),EarlyTimes=values(EarlyTimes),Foodsubsidy=values(Foodsubsidy),Carsubsidy=values(Carsubsidy),OvertimeTotalDays=values(OvertimeTotalDays),
            DutyTotalDays=values(DutyTotalDays),ConvertedDays=values(ConvertedDays),OvertimeMemo=values(OvertimeMemo),DutyMemo=values(DutyMemo) ";
        return ModAttendanceMonthInfo::model()->getDbConnection()->createCommand($sql)->execute($arrayMap);
    }

 3、自动更新数据库字段

public function beforeSave($insert){
        if(parent::beforeSave($insert)){
            if($this->isNewRecord){
                //判断是更新还是插入
                $this->AddTime = TimeUtil::time();
                $this ->AddBy = 'xxxdsasx';
                $this->EditTime = TimeUtil::time();
                $this->EditBy = 'xxasdxx';
            }else{
                $this->EditTime = TimeUtil::time();
                $this->EditBy = 'sdasdasd';
            }
            return true;
        }else{
            return false;
        }
    }

 

标签:DutyTotalDays,OvertimeMemo,封装,代码,values,key,sql,array,yii2
From: https://www.cnblogs.com/Adam-Ye/p/15321438.html

相关文章

  • 代码随想录算法训练营第49天 | 动态规划总结
    代码随想录算法训练营第天|647.回文子串https://leetcode.cn/problems/palindromic-substrings/description/代码随想录https://programmercarl.com/0647.回文子串.html#思路516.最长回文子序列https://leetcode.cn/problems/longest-palindromic-subsequence/代码随想录......
  • 程序员的代码规范需求
    程序员的代码规范是指在编写代码过程中应遵守的一系列约定和规则,以提高代码的质量、可读性和可维护性。以下是一些常见的代码规范需求:1.命名规范:变量、函数、类等命名应具有描述性,使用有意义的名称,遵循一定的命名约定,如驼峰命名法等。2.缩进和空格:使用一致的缩进风格,建议使用......
  • 【linux】【设备树】具有 GPIO 控制器和连接器的硬件配置的备树(Device Tree)代码讲解
    具有GPIO控制器和连接器的硬件配置的备树(DeviceTree)代码讲解背景-学习Linux设备树代码soc{soc_gpio1:gpio-controller1{#gpio-cells=<2>;};soc_gpio2:gpio-controller2{#gpio-cells=<2>;};};connector:connect......
  • 当 Visual Studio 检测到代码没有问题时,无法弄清楚为什么它返回“语法错误”
    fromkivy.appimportAppfromkivy.uix.gridlayoutimportGridLayoutfromkivy.uix.labelimportLabelclasstest(App):defbuild(self):self.window=GridLayout()self.label=Label(text="hello")self.window.add_widge......
  • stable diffusion中的UNet2DConditionModel代码解读
    UNet2DConditionModel总体结构图片来自于https://zhuanlan.zhihu.com/p/635204519stablediffusion运行unet部分的代码。noise_pred=self.unet(sample=latent_model_input,#(2,4,64,64)生成的latenttimestep=t,#时刻tencoder_hidden_states=pro......
  • 掌握 IPython %%time 魔法命令:高效测量代码块执行时间
    引言在编程和数据分析中,了解代码的执行时间是优化性能的关键步骤。IPython,作为一个强大的交互式计算环境,提供了多种工具来帮助用户测量和优化代码。其中,%%time魔法命令是IPython中用来测量代码块执行时间的便捷工具。本文将详细介绍%%time魔法命令的使用方法,并通过一......
  • 探索 IPython 中的 %%javascript 魔法命令:运行 JavaScript 代码的秘籍
    引言IPython是一个强大的交互式计算环境,它不仅支持Python语言,还通过各种魔法命令扩展了其功能。其中,%%javascript魔法命令是IPython扩展中一个非常有趣的特性,它允许用户在IPython环境中直接运行JavaScript代码。这对于需要在数据科学和科学计算中使用JavaScript......
  • 代码随想录二刷——数组
     ......
  • Redis Java客户端(带示例代码)
    目录概述Jedis--快速入门Jedis简介创建项目和测试1.引入依赖2.建立连接3.测试4.释放资源Jedis连接池JedisPool简介创建连接池使用连接池概述在Redis官网中提供了各种语言的客户端网站:ConnectwithRedisclients标记为❤的就是推荐使用的java客......
  • 毕业设计:基于Springboot的在线小说阅读平台【代码+论文+PPT】
    全文内容包括:1、采用技术;2、系统功能;3、系统截图;4、配套内容。索取方式见文末微信号,欢迎关注收藏!一、采用技术语言:Java1.8框架:SpringBoot数据库:MySQL5.7、8.0开发工具:IntelliJIDEA旗舰版其他:Maven3.8以上二、系统功能会员管理:负责用户注册、登录、会员等级划分及用户信......