模型访问:
<?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());
}
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号