PHP custom model addition, deletion, modification and query

不言
Release: 2023-03-23 19:08:02
Original
1483 people have browsed it

The content of this article is about adding, deleting, modifying and checking PHP custom models. Now I share it with everyone. Friends in need can refer to it

<?php
namespace app\index\controller;

use app\common\model\User;
use app\common\model\UserInfo;

class Index
{
    public function index()
    {
        /*
        // 静态访问
        $user = User::get(1);
        // 获取一条记录的数据
        dump($user->getData());
        // 获取记录里面的某个属性值
        dump($user->name);

        // 助手方法
        dump(model(&#39;user&#39;)->find(1)->getData());

        // 直接实例化
        $user = new User();
        dump($user->find(1)->getData());
        */
        $user = UserInfo::get(1);
        dump($user->getData());

        $user = UserInfo::get(1);
        dump($user->getData());

        return "thinkphp5";
    }

    // 新增/更新 数据的方法
    public function saveData()
    {
        // 新增一条数据
//        $data = [
//            &#39;name&#39; => &#39;jikexueyuan&#39;,
//            &#39;password&#39; => md5(123456),
//            &#39;age&#39; => 18,
//            &#39;sex&#39; => "男",
//            &#39;status&#39; => 1,
//            &#39;create_time&#39; => time(),
//            &#39;update_time&#39; => 0,
//        ];

        $user = new User();
//        echo $user->save($data);

        // 新增多条数据
        for($i = 0 ; $i < 10 ; $i ++)
        {
            $data[$i] = [
                &#39;name&#39; => &#39;jikexueyuan&#39; . $i,
                &#39;password&#39; => md5(123456),
                &#39;age&#39; => $i,
                &#39;sex&#39; => "男",
                &#39;status&#39; => 1,
                &#39;create_time&#39; => time(),
                &#39;update_time&#39; => 0,
            ];
        }
        $user->saveAll($data);
    }

    // 数据更新
    public function updateData()
    {
        // 查询后进行更新操作
        $user = User::get(16);
        $user->name = &#39;wangjialin&#39;;
        echo $user->save();

        // 直接使用数据库类的更新方法update
        $user = new User();
        echo "return:".$user->where(&#39;id=18&#39;)->update([&#39;name&#39;=>&#39;wjl&#39;]);

        // saveAll方法可以用于批量的更新
        // [id=>1]
    }

    // 数据查询
    public function getDataList()
    {
        // 无条件查询所有的数据
//        $list = User::all();
//        foreach ($list as $key=>$val)
//        {
//            dump("id:".$val->id.",name:".$val->name . ",age:".$val->age);
//        }

        // 条件列表查询
        $list = User::all(&#39;18,22&#39;);
        foreach ($list as $key=>$val)
        {
            dump("id:".$val->id.",name:".$val->name . ",age:".$val->age);
        }

    }

    // 数据删除
    public function delData()
    {
        echo User::destroy(18);
        echo model(&#39;user&#39;)->where(&#39;id=16&#39;)->delete();
    }



}
Copy after login

Related recommendations:

PHP custom two-dimensional array sorting function array

Explanation on PHP custom functions and internal functions


The above is the detailed content of PHP custom model addition, deletion, modification and query. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template