PHP Custom model Additions and deletions

Source: Internet
Author: User
This article introduces the content of the PHP custom model additions and deletions, and now share to everyone, the need for friends can refer to

<?phpnamespace app\index\controller;use app\common\model\user;use app\common\model\userinfo;class Index{Public        function index () {/*//static Access $user = User::get (1);        Gets the data for a record dump ($user->getdata ());        Gets the value of a property in the record dump ($user->name);        Helper method Dump (model (' user ')->find (1)->getdata ());        Direct instantiation of $user = new User ();        Dump ($user->find (1)->getdata ());        */$user = Userinfo::get (1);        Dump ($user->getdata ());        $user = Userinfo::get (1);        Dump ($user->getdata ());    return "THINKPHP5"; }//How to add/Update data public Function SaveData () {//Add a new data//$data = [//' name ' = ' = ' Jike            Xueyuan ',//' password ' + MD5 (123456),//' age ' + 18,//' sex ' = ' male ',//        ' Status ' = 1,//' create_time ' + Time (),//' update_time ' + 0,//]; $user = New User ();//Echo $user->save ($data); Added multiple data for ($i = 0; $i < $i + +) {$data [$i] = [' name ' = ' = ' Jikexueyu An '.                $i, ' Password ' and MD5 (123456), ' age ' and $i, ' sex ' and ' male ',        ' Status ' = 1, ' Create_time ' + Time (), ' update_time ' = 0,];    } $user->saveall ($data);        }//Data update public Function UpdateData () {///query after the update operation $user = User::get (16);        $user->name = ' Wangjialin ';        echo $user->save ();        Update method for directly using the database class update $user = New User ();        echo "return:". $user->where (' id=18 ')->update ([' name ' = ' WJL ']);        The SaveAll method can be used for batch updates//[id=>1]}//Data query Public function getdatalist () {//unconditionally querying all data// $list = User::all ();//foreach ($list as $key = + $val)//{//Dump ("ID:". $val->id. ", Name:". $val->name. ", Age:". $val->age)//}//Condition list Query $list = User::all (' 18        , 22 '); foreach ($list as $key = = $val) {dump ("ID:". $val->id. ", Name:". $val->name. "," Age: ". $val->age)        ;        }}//Data delete public function Deldata () {echo User::d Estroy (18);    echo model (' user ')->where (' id=16 ')->delete (); }}

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.