yii框架循环添加只能加入最后一条的解决办法
2016-09-04 15:38
357 查看
foreach循环插入数据时,表中只插入了循环的最后一条数据,而其它数据没有真实添加,追踪发现,内存地址中循环时新一条数据会覆盖前一条数据,解决办法是另起一个对象;
第一种方法:
$model = new User();
foreach($data as $attributes)
{
$_model = clone $model; //克隆对象
$_model->setAttributes($attributes);
$_model->save();
}
第二种方法:
$model = new User();
foreach($data as $attributes)
{
$model->isNewRecord = true;
$model->setAttributes($attributes);
$model->save() && $model->id=0;
}
来自:http://blog.csdn.net/huojiahui22/article/details/46380493
第一种方法:
$model = new User();
foreach($data as $attributes)
{
$_model = clone $model; //克隆对象
$_model->setAttributes($attributes);
$_model->save();
}
第二种方法:
$model = new User();
foreach($data as $attributes)
{
$model->isNewRecord = true;
$model->setAttributes($attributes);
$model->save() && $model->id=0;
}
来自:http://blog.csdn.net/huojiahui22/article/details/46380493
相关文章推荐
- YII Framework框架教程之安全方案详解
- Yii PHP Framework实用入门教程(详细介绍)
- PHP的Yii框架中Model模型的学习教程
- 详解PHP的Yii框架中自带的前端资源包的使用
- Yii操作数据库的3种方法
- 在yii中新增一个用户验证的方法详解
- 详解PHP的Yii框架中的Controller控制器
- PHP的Yii框架中过滤器相关的使用总结
- Yii框架在页面输出执行sql语句以方便调试的实现方法
- YII动态模型(动态表名)支持分析
- Yii视图操作之自定义分页实现方法
- yii的入口文件index.php中为什么会有这两句
- 浅析Yii中使用RBAC的完全指南(用户角色权限控制)
- 详解在PHP的Yii框架中使用行为Behaviors的方法
- Yii框架连接mongodb数据库的代码
- Yii 连接、修改 MySQL 数据库及phpunit 测试连接
- Yii2中DropDownList简单用法示例
- 深入讲解PHP的Yii框架中的属性(Property)
- yii实现使用CUploadedFile上传文件的方法
- Yii实现的多级联动下拉菜单