YII2.0 database additions and deletions change

Source: Internet
Author: User
Tags findone yii

/*==================== Dkhbasemodel Database Adding and Deleting methods start ================================*///Add a data public function base        InsertData ($data) {if (!is_array ($data)) {return $this->seterror ([' errmsg ' = ' data Error ']);};        $db = \yii:: $app->db;        $ok = $db->createcommand ()->insert ($this->tablename (), $data)->execute ();        if ($ok) {//returns the self-increment ID return $db->getlastinsertid ();        }else{return $this->seterror ([' errmsg ' = ' Insert record failed ']); }}//The condition is queried for a record public function basegetone ($condition = [], $orderBy = ' id DESC ', $select = Array ()) {//$co        Ndition search conditions such as: $condition = [' and ']; $condition [] = [' = ', ' id ', 1]; $condition [] = [' like ', ' name ', ' Dkh ']; $select field Filter such as: [' id ', ' field1 ', ' Field2 '] return $this->find ()->select ($select)->where ($condition)->or    DerBy ($orderBy)->asarray ()->one (); }//Query an association model by condition record public Function basegetonerelation ($condition = [], $tables, $by = ", $select = Array ()) {return $this->find ()->joinwith ($tables)->select ($select)->where ($condi    tion)->orderby ($orderBy)->asarray ()->one (); }//Get all data public function basegetall ($condition = [], $orderBy = ' id DESC ', $select = Array ()) {return $this-&    Gt;find ()->select ($select)->where ($condition)->orderby ($orderBy)->asarray ()->all ();        The Public function basegetallrelation ($condition = [], $tables, $orderBy = ", $select = Array ()) is logged by the conditional query of multiple association models { return $this->find ()->joinwith ($tables)->select ($select)->where ($condition)->orderby ($orderBy)-    >asarray ()->all ();        }//delete a data by ID public Function Basedeletebyid ($id) {$this->id = $id;    return $this->findone ($id)->delete (); }//delete data for the specified condition public function basedeleteall ($condition = []) {if (empty ($condition)) {return $this->seterro        R ([' errmsg ' = ' deleteAll Error ');}; Return $this->deleteall ($condition);        }//Execute native SQL statements, commonly used for updates, bulk additions, delete data public function Baseexecutesql ($sql) {$db = \yii:: $app->db;    At the place where the call was made, the judgment of whether the execution was successful return $db->createcommand ($sql)->execute ();        }//Executes native SQL statements, typically used to query data return single line public function Basequeryonesql ($sql) {$db = \yii:: $app->db;    Return $db->createcommand ($sql)->queryone ();        }//Executes native SQL statements, typically used to query data to return multiple lines of public function Basequeryallsql ($sql) {$db = \yii:: $app->db;    Return $db->createcommand ($sql)->queryall ();        A record public function Basegetonebyid ($id) {$model = $this->findone ($id) is queried through the primary key;        if ($model) {return $model->attributes;        }else{return Array (); }}//update the Eligible data public function Baseupdatedata ($data, $condition = []) {if (empty ($condition)) {return $this        ->seterror ([' errmsg ' = ' condition Empty ');};        $res = $this->updateall ($data, $condition); if ($res = = = False) {           return $this->seterror ([' errmsg ' = ' update failed ']);    } return $res;        }//Get Paging data public function basepagedata ($condition = [], $page, $pageSize, $orderBy = ' id DESC ', $select = Array ()) { $page, $pageSize equals LIMIT $page, $pageSize//$select filter field [' Id ', ' field1 ', ' Field2 '] return $this->f IND ()->select ($select)->where ($condition)->offset ($page)->limit ($pageSize)->orderby ($orderBy)-    >asarray ()->all (); }//Gets paging data and the associated model records public function basepagedatarelation ($condition = [], $page, $pageSize, $tables, $orderBy = ', $sel         ECT = Array ()) {///$page, $pageSize equivalent to the SQL statement LIMIT $page, $pageSize//$select field filter such as: [' id ', ' field1 ', ' field2 '] $tables Association model table such as: [' table1 ', ' table2 '] return $this->find ()->joinwith ($tables)->select ($select)-&G    T;where ($condition)->orderby ($orderBy)->offset ($page)->limit ($pageSize)->asarray ()->all (); }//query record number of records by condition public function Basegetcount ($Condition = [], $select = Array ()) {return $this->find ()->select ($select)->where ($condition)->count (); }//Query the number of records by condition and associate model Public Function basegetcountrelation ($condition = [], $tables, $select = Array ()) {RET    Urn $this->find ()->joinwith ($tables)->select ($select)->where ($condition)->count (); }//Query a record by condition public function Basemodelgetone ($params) {if (Isset ($params [' condition ')) && Is_array ($        params[' condition ') &&!empty ($params [' condition ']) {$condition = $params [' condition '];        }else{$condition = [];            if (Isset ($params [' field ']) && is_array ($params [' field ']) &&!empty ($params [' field ']) {        $field = $params [' field '];        }else{$field = [];        } if (Isset ($params [' Order ')] &&!empty ($params [' Order ')]) {$order = $params [' Order '];        }else{$order = ' id DESC '; } return $this->find ()->select ($field)->where ($condition)->orderby ($order)->asarray ()->one (); The Public Function basemodelgetonerelation ($params) {if (Isset ($params [' condition ']) is logged by querying an association model with a condition &&amp ;        Is_array ($params [' condition ']) &&!empty ($params [' condition ']) {$condition = $params [' condition '];        }else{$condition = [];            if (Isset ($params [' field ']) && is_array ($params [' field ']) &&!empty ($params [' field ']) {        $field = $params [' field '];        }else{$field = [];        } if (Isset ($params [' Order ')] &&!empty ($params [' Order ')]) {$order = $params [' Order '];        }else{$order = ' id DESC '; if (Isset ($params [' join ']) && is_array ($params [' join ']) &&!empty ($params [' join ']) {$        join = $params [' Join '];        }else{$join = "; } return $this->find ()->joinwith ($join)->select ($field)->where ($condition)->orderby ($order)->asarray ()->one (); }//Get all data public function Basemodelgetall ($params) {if (Isset ($params [' condition ')) && Is_array ($para        ms[' condition ') &&!empty ($params [' condition ']) {$condition = $params [' condition '];        }else{$condition = [];            if (Isset ($params [' field ']) && is_array ($params [' field ']) &&!empty ($params [' field ']) {        $field = $params [' field '];        }else{$field = [];        } if (Isset ($params [' Order ')] &&!empty ($params [' Order ')]) {$order = $params [' Order '];        }else{$order = ' id DESC ';    } return $this->find ()->select ($field)->where ($condition)->orderby ($order)->asarray ()->all (); The Public Function basemodelgetallrelation ($params) {if (Isset ($params [' condition ']) is logged by querying multiple association models by condition &&AM P Is_array ($params [' Condition']) &&!empty ($params [' condition ']) {$condition = $params [' condition '];        }else{$condition = [];            if (Isset ($params [' field ']) && is_array ($params [' field ']) &&!empty ($params [' field ']) {        $field = $params [' field '];        }else{$field = [];        } if (Isset ($params [' Order ')] &&!empty ($params [' Order ')]) {$order = $params [' Order '];        }else{$order = ' id DESC '; if (Isset ($params [' join ']) && is_array ($params [' join ']) &&!empty ($params [' join ']) {$        join = $params [' Join '];        }else{$join = "; } return $this->find ()->joinwith ($join)->select ($field)->where ($condition)->orderby ($order),    Asarray ()->all (); }//Get Paging data public Function Basemodelpagedata ($params) {if (Isset ($params [' condition ']) && Is_array ($pa rams[' condition ') &&!empty ($params [' condition ']) {$condition = $params [' condition '];        }else{$condition = [];            if (Isset ($params [' field ']) && is_array ($params [' field ']) &&!empty ($params [' field ']) {        $field = $params [' field '];        }else{$field = [];        } if (Isset ($params [' Order ')] &&!empty ($params [' Order ')]) {$order = $params [' Order '];        }else{$order = ' id DESC ';        } if (Isset ($params [' page ')]) {$page = $params [' page '];        }else{$page = 0;        } if (Isset ($params [' pageSize '])) {$pageSize = $params [' pageSize '];        }else{$pageSize = 10; } return $this->find ()->select ($field)->where ($condition)->offset ($page)->limit ($pageSize),    ($order)->asarray ()->all (); }//Gets paging data and the associated model records public function basemodelpagedatarelation ($params) {if (Isset ($params [' condition ']) &Amp;& Is_array ($params [' condition ']) &&!empty ($params [' condition ']) {$condition = $params [' Conditi        On '];        }else{$condition = [];            if (Isset ($params [' field ']) && is_array ($params [' field ']) &&!empty ($params [' field ']) {        $field = $params [' field '];        }else{$field = [];        } if (Isset ($params [' Order ')] &&!empty ($params [' Order ')]) {$order = $params [' Order '];        }else{$order = ' id DESC '; if (Isset ($params [' join ']) && is_array ($params [' join ']) &&!empty ($params [' join ']) {$        join = $params [' Join '];        }else{$join = ";        } if (Isset ($params [' page ')]) {$page = $params [' page '];        }else{$page = 0;        } if (Isset ($params [' pageSize '])) {$pageSize = $params [' pageSize '];        }else{$pageSize = 10; } return $this->find ()->joinwith ($join)->select ($field)->where ($condition)->orderby ($order)->offset ($page)-    >limit ($pageSize)->asarray ()->all (); }//Condition query record number of public function Basemodelgetcount ($params) {if (Isset ($params [' condition ']) && Is_array        ($params [' condition ']) &&!empty ($params [' condition ']) {$condition = $params [' condition '];        }else{$condition = [];            if (Isset ($params [' field ']) && is_array ($params [' field ']) &&!empty ($params [' field ']) {        $field = $params [' field '];        }else{$field = [];    } return $this->find ()->select ($field)->where ($condition)->count (); }//Query the number of records by condition and associate model Public Function basemodelgetcountrelation ($params) {if (Isset ($params [' condition ']) &am p;& Is_array ($params [' condition ']) &&!empty ($params [' condition ']) {$condition = $params [' Conditio        N ']; }else{$cOndition = [];            if (Isset ($params [' field ']) && is_array ($params [' field ']) &&!empty ($params [' field ']) {        $field = $params [' field '];        }else{$field = []; if (Isset ($params [' join ']) && is_array ($params [' join ']) &&!empty ($params [' join ']) {$        join = $params [' Join '];        }else{$join = ";    } return $this->find ()->joinwith ($join)->select ($field)->where ($condition)->count (); }//delete data for the specified condition public function Basemodeldeleteall ($params) {if (Empty ($params [' condition '])) {return $this        SetError ([' errmsg ' = ' deleteAll Error ']);    return $this->deleteall ($params [' condition ']); }//Update the Eligible data public function Basemodelupdatedata ($params) {if (Empty ($params [' condition '])) {return $this-&gt        ; SetError ([' errmsg ' = ' condition ' is Empty ');};        $res = $this->updateall ($params [' Data '], $params [' condition ']); If$res = = = False) {return $this->seterror ([' errmsg ' = ' update failed ']);    } return $res; }

Additions and deletions to use the method:

                Select group                $condition = [' and '];                $condition [] = [' = ', ' member.shop_id ', $params [' shop_id ']];                $condition [] = [' in ', ' member.member_group_id ', $params [' Member_tag '] [' groups ']]; Indexed array                $wshMemberModel = new Wshmember ();                $memberList = $wshMemberModel->basegetallrelation ($condition, [' Wxuserinfos '], ' member.id DESC ', [' member.id ']);

$wshWxUsersModel = new Wshwxusers ();                $userList = $wshWxUsersModel->basegetall ([' shop_id ' + = $params [' shop_id '], ' user_id ' = ' $uid], ' ID DESC ', [' user_id ', ' open_id ']);

$insertData = [            ' material_id ' =>isset ($params [' material_id '])? $params [' material_id ']:0,            ' shop_id ' = $params [' shop_id '],            ' shop_sub_id ' =>$_session[' _staff ' [' shop_sub_id '],            ' send_time ' = $now,            ' Send_type ' = ' $params [' Send_type '],            ' text_content ' and ' = ' $params [' text_content '],            ' sort_content ' =>$ params[' sort_content '),        ];        $insertId = $wshWxMessageSendMutilModel->baseinsertdata ($insertData);

            $dkhWxMessageMemberMutilModel = new Dkhwxmessagemembermutil ();            $successValues = "; $successSql =";            Assemble the SQL statement            foreach ($successList as $item) {                $successValues. = "(' {$insertId} ', ' {$item [' user_id ']} ', ' 1 ', ' {$ Now} ', ' {$now} '), ';            }            if (!empty ($successValues)) {                $successSql = "INSERT into ' dkh_wx_message_member_mutil ' (' send_id ', ' user_id ', ' send _result ', ' created ', ' modified ') VALUES. substr ($successValues, 0,-1). '; ';                Insert Send success Data                $dkhWxMessageMemberMutilModel->baseexecutesql ($SUCCESSSQL);            }

            Consumption minus balance            $balance = $balanceData [' balance ']-$data [' balance '];            $map = [' and '];            $map [] = [' = ', ' member_id ', $orderData [' member_id ']];            $map [] = [' >= ', ' balance ', $data [' balance '];            $balanceOk = $dkhMemberExtendModel->baseupdatedata ([' balance ' = $balance], $map);

            First charge, can participate in the recharge            $condition = [' and '];            $condition [] = [' = ', ' dkh_recharge_activity.shop_id ', $data [' shop_id ']];            $condition [] = [' = ', ' Dkh_recharge_activity.is_del ', 0];            $condition [] = [' = ', ' Dkh_recharge_activity.status ', 1];            $condition [] = [' <= ', ' dkh_recharge_activity.start_time ', $now];            $condition [] = [' >= ', ' dkh_recharge_activity.end_time ', $now];            $dkhRechargeActivity = new Dkhrechargeactivity ();            $rechargeActivityData = $dkhRechargeActivity->basegetonerelation ($condition, [' dkhrechargeactivityrelation '], ' Dkh_recharge_activity.id DESC ');

YII2.0 database additions and deletions change

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.