yii insert 插入数据,报ar 不是新的,无法被新增到数据库

mrh111 2014-11-20 01:35:32
执行到 这行 DealerImgs::model()->insert( array( 'key' => 'name', 'id' => 'id') );

报如下错误:
[error][exception.CDbException] exception 'CDbException' with message 'active record 由于不是新的, 无法被新增到数据库.' in F:\wwwroot\sd\trunk\yii\framework\db\ar\CActiveRecord.php:1070

求解
...全文
1675 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
mahaixing 2014-11-23
  • 打赏
  • 举报
回复
有点类似,不知道能不能帮上忙
艾派德迷你2 2014-11-20
  • 打赏
  • 举报
回复
这个表的主键必须是自动增加并且这个对象为新的. $model = ABC::model()->findByPK('123456'); $model ->setIsNewRecord(); $model->insert(array('xxx'=>'a1234','bbb'=>'bbbbaaa')); 不过一般都用这种办法,此种办法需要手动验证数据. insert() 方法 public boolean insert(array $attributes=NULL) $attributes array 需要保存的属性列表。默认为null,这意味着从数据库中加载的所有属性将被保存。 {return} boolean 属性是否有效,并将记录插入成功。 源码: framework/db/ar/CActiveRecord.php#1004 (显示) public function insert($attributes=null) { if(!$this->getIsNewRecord()) throw new CDbException(Yii::t('yii','The active record cannot be inserted to database because it is not new.')); if($this->beforeSave()) { Yii::trace(get_class($this).'.insert()','system.db.ar.CActiveRecord'); $builder=$this->getCommandBuilder(); $table=$this->getMetaData()->tableSchema; $command=$builder->createInsertCommand($table,$this->getAttributes($attributes)); if($command->execute()) { $primaryKey=$table->primaryKey; if($table->sequenceName!==null) { if(is_string($primaryKey) && $this->$primaryKey===null) $this->$primaryKey=$builder->getLastInsertID($table); else if(is_array($primaryKey)) { foreach($primaryKey as $pk) { if($this->$pk===null) { $this->$pk=$builder->getLastInsertID($table); break; } } } } $this->_pk=$this->getPrimaryKey(); $this->afterSave(); $this->setIsNewRecord(false); $this->setScenario('update'); return true; } } return false; } 插入一行到基于这个ActiveRecord的属性表。如果表的主键是自动增量和插入前为null,将插入后的实际值填充。注意,验证不执行此方法。你可以调用validate执行验证。记录成功插入到数据库后,它的isNewRecord属性将设置为false,和它的scenario属性将设置为‘update’。
xiaoxin125 2014-11-20
  • 打赏
  • 举报
回复
new 一个模型试试 $model = new DealerImgs();
mrh111 2014-11-20
  • 打赏
  • 举报
回复
引用 3 楼 xuzuning 的回复:
你最好给出原始的错误信息

可能你的表还有其他的唯一索引吧




xuzuning 2014-11-20
  • 打赏
  • 举报
回复
你最好给出原始的错误信息 可能你的表还有其他的唯一索引吧
mrh111 2014-11-20
  • 打赏
  • 举报
回复
引用 1 楼 xuzuning 的回复:
主键不唯一了吧?
DealerImgs::model()->insert( array( 'aid' => 0, 'dealer_id' => $this->_dealer['dealer_id'], 'imgurl' => $_move->message['imgurl'], 'inputtime' => TIME, 'path' => $_move->message['imgurl'], 'source_type' => $source_type ) ); 源代码是这样的,主键id 是唯一的
xuzuning 2014-11-20
  • 打赏
  • 举报
回复
主键不唯一了吧?

4,251

社区成员

发帖
与我相关
我的任务
社区描述
国内外优秀PHP框架讨论学习
社区管理员
  • Framework
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧