public function test()
{
//$res = $this->data->getMenu();
$model = new Data();
//新增多条数据
$res = $model->saveAll([
['node_name' => 'jjf', 'typeid' => 6],
['node_name' => 'jfj', 'typeid' => 19],
]);
//dump($res);
//遍历模型对象数组
foreach ($res as $val) {
dump($val->getData());
}
}
//新增单条数据
public function test()
{
//$res = $this->data->getMenu();
$model = new Data();
//create()静态方法,新增单条记录
$res = Data::create([
'node_name' => 'xinzeng',
'typeid' => 32,
]);
dump($res);
}
第二个参数为true:
public function test()
{
//$res = $this->data->getMenu();
$model = new Data();
//传入第二个参数为true时,仅添加表中存在的字段
$res=Data::create([
'node_name'=>'jsd',
'typeid'=>98,
'level'=>3 //表中没有level字段
],true);
//如果不加true参数,将报错并且拒绝执行
dump($res);
}
public function test()
{
//$res = $this->data->getMenu();
//获取模型对象
$model = new Data();
// 等价于
// $res=$model->save([
// 'node_name'=>'就发送',
// 'typeid'=>4
// ])
//给模型对象的node_name属性赋值
$model->node_name = '就发送';
//给模型对象的typeid属性赋值
$model->typeid = 4;
//将数据写入表中,返回受影响记录数
$res = $model->save();
dump($res);//输出int(1)
}
public function test()
{
//$res = $this->data->getMenu();
//获取模型对象
$model = new Data();
//allowField(true)过滤非表中字段
$res = $model->allowField(true)
->save([
'node_name' => 'fdf',
'age' => 32, //非表字段,自动过滤
]);
dump($res);
}
public function test()
{
//$res = $this->data->getMenu();
//获取模型对象
$model = new Data();
$res = $model->allowField(['node_name', 'typeid']) //允许新增的字段
->save([
'node_name' => 'jfss',
'age' => 4, //非表字段,自动过滤
'typeid' => 7,
'rule' => '/home', //不会新增
]);
dump($res);
}