8.5 Database model Operations

Source: Internet
Author: User
Tags modifier

Model Layer

<?php namespace App\demo\model; UseThink\model; classStudentextendsmodel{//The capture device Public functionGetssexattr ($val){ Switch($val){ Case1:returnMale; Break; Case2:returnFemale; Break; default:returnUnknown; Break; } } //modifier Public functionSetclassattr ($val){ return MD5($val); } //setting, if there is no assignment, the fields that need to be completed automatically Public $auto= [' Sno ', ' Ssex ']; Public functionsetsnoattr () {return18; } Public functionsetssexattr () {return1111; } }?>

Control layer

<?php namespace App\demo\controller;  UseThink\controller;  UseApp\demo\model\student;//Introducing Models          classDemoextendscontroller{ Public functionTest () {//get the data for ID 4 in the student table             $student=NewStudent; //Dump ($student->get (4)->toarray ());             Query operation//1 Condition query//dump ($student->get (["Sno" =>101])->toarray ()); 2 Closed packet Query             /*$data = $student->get (function ($query) {$query->where (' Sname ', ' Sun Quan ');            }); Dump ($data->toarray ());*/            //3 Find Method//$data = $student->where (' Sname ', ' Liu Bei ')->find (); 4 querying more than one piece of data            /*$data = Student::all (' All-in-all ');            foreach ($data as $key = = $stu) {dump ($stu->toarray ()); }*/            //5 inserting data//inserting a bar            /*$student->data ([' id ' = ' = ', ' Sno ' = ' + ', ' Sname ' = ' haha ', ' Ssex ' and ' = ' male ', ' sbirthday ' and ' = ', ' Class            "=" 96080 "]);            $student->save (); Insert multiple $list = [[' id ' = ' = ', ' Sno ' = ' + ', ' Sname ' = ' haha ', ' Ssex ' and ' man ', ' sbirthday ' and ' = ' man ', ' Class "+" 96080 "], [' id ' = = ', ' Sno ' = ' + ', ' Sname ' and ' haha ', ' Ssex ' and ' man ', ' sbirthday ' and ' man ', ' Class ' =            > "96080"];        Dump ($user->saveall ($list, false)); */                //Update            /*$student->save ([' Sname ' = ' Zhang Liao ', ' Sno ' + 201], [' id ' = 1]);*/            //Delete//student According to primary key::d Estroy ([+]);            Conditional Delete: Delete data with id 0//student::d estroy ([' id ' = 0]); The capture device            /*$data = $student->get ([' id ' = ' 7 ']); Dump ($data->toarray ());*/            //modifier            /*$data = $student->save ([' Class ' = ' 345 '],[' id ' = ' 9 ']); Dump ($data);*/            //Auto-Finish//There is no value for age and sex here            /*$data = $student->save ([' Class ' = ' 345 '],[' id ' = ' 8 ']); Dump ($data);*/         }              }    //time Stamp    /*the system supports automatically writing timestamp fields created and updated the first way is to add global settings to the database configuration file://Turn on the auto-write timestamp field ' Auto_timestamp ' + true, and the second is to set directly in a separate model class    Reset: Protected $autoWriteTimestamp = true; If these two places are set to True, the default is recognized as int type, and if your time field is not of type int, for example, using the datetime type, you can set://Turn on the auto-write timestamp field ' auto_timestamp ' = ' datet IME ', or protected $autoWriteTimestamp = ' datetime ';*/?>

8.5 Database model Operations

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.