Blogger Information
Blog 14
fans 0
comment 0
visits 16077
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
模型访问 添加更新
依然很m丶的博客
Original
973 people have browsed it

模型访问:

<?php
namespace app\index\controller;
use \think\Db;
use \app\index\model\Shop_name;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        dump(
          Shop_name::get('25')->getData()
        );
    }
}

实例化对象添加:

<?php
namespace app\index\controller;
use \app\index\model\Shop_name;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        //实例化模型,创建模型对象
        $shop_name = new Shop_name();
        //创建模型,采用对象方式
        $shop_name-> name ='c++';
        $shop_name-> price = 12;
        $result = $shop_name->save();
        return $result ? '成功添加'.$result.'条信息' : '添加失败';
    }
}

批量添加

<?php
namespace app\index\controller;
use \app\index\model\Shop_name;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        //实例化模型,创建模型对象
        $shop_name = new Shop_name();
        //创建模型,采用对象方式
        $date = [
            ['name'=>'java1','price'=>20],
            ['name'=>'java2','price'=>25],
            ['name'=>'java3','price'=>30],
        ];
        $result = $shop_name->saveAll($date);
        dump($result);
    }
}

cerat添加

<?php
namespace app\index\controller;
use \app\index\model\Shop_name;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
        //实例化模型,创建模型对象
        $shop_name = new Shop_name();
        //创建模型,采用对象方式
        $result = $shop_name::create([
            'name'=>'php1','price'=>22
            ]
        );
        dump($result->getData());
    }
}

update

<?php
namespace app\index\controller;
use \app\index\model\Shop_name;
class Index
{
    public function index()
    {
        return '正在学习中...';
    }
    public function demo()
    {
      $date = [
        'name'=>'php','price'=>15,'type'=>'php'
      ];
      $where = ['id'=>32];
      $field =['name','price'];
      //update(更新内容,更新条件,更新字段)
      $result = Shop_name::update($date,$where,$field);
      dump($result->getData());
    }
}


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post