sql:
model:
<?php
namespace app\model;
use think\Model;
class Aaa extends Model
{
}
controller:
<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use app\model\Aaa;
class Index extends BaseController
{
public function hello()
{
$user=new Aaa();
$data=['name'=>"vvvv"];
$user->allowField(['name'])->save($data);//只能name插入数据
}
}
修改:
<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use app\model\Aaa;
class Index extends BaseController
{
public function hello()
{
$user=Aaa::where('name',"vvvv")->find();//找到name为vvvv的字段
$user->name="liwen";//修改其name值
$user->save();
}
}
修改的第二种方式:
<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use app\model\Aaa;
class Index extends BaseController
{
public function hello()
{
Aaa::update(['name'=>"fff",'id'=>3]);
}
}
删除:
<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use app\model\Aaa;
class Index extends BaseController
{
public function hello()
{
$user=Aaa::find(2);
$user->delete();
}
}
查询:
<?php
namespace app\index\controller;
use app\BaseController;
use think\facade\Db;
use app\model\Aaa;
class Index extends BaseController
{
public function hello()
{
$aaa=Aaa::select([3,4]);
halt($aaa);
}
}