Delete删除操作
1、方法:delete()
调用方式:实例化
返回值:受影响记录数量
2、方法:destory(条件/闭包)
调用方式:静态
返回值:受影响记录数量
1、delete()方法不要传任何参数,它只删除当前模型对象对应的记录;
2、destory()中的删除条件,推荐采用闭包方式;
3、推荐用软删除替代该方法,即用更新的方式来实现删除操作。
举例:
实例化调用delete( )删除符合条件的数据。
<?php
namespace app\ index\controller;
use app\ index \model\Staff;
class Index
{
public function index( )
{
$result = Staff::get(['id'=>['>' ,1033]]);
$affected = $result -> delete( ) ;
dump($affected) ;
}
}
举例:
静态调用destory(条件/闭包),删除符合条件的数据。
<?php
namespace app\ index \ controller;
use app\ index\model\Staff;
class Index
{
public function index()
{
$where =function ($query){
$query->where('id','>',1030)
->where( 'age', '>' ,40)
->where0r( 'salary', '>' , 40000) ;
};
$value=Staff::destroy($where) ;
dump($value);
}
|