dongmeng0317 2012-07-13 14:13
浏览 56

将Yii ActiveRecord模型插入mysql表时出错

I have the following kind of error that happens on my website from time to time.

2012/07/12 21:21:48 [error] [system.db.CDbCommand] Error in executing
SQL: INSERT INTO `seo_page` (`grouped`, `results_count`, `page`, `operation`, `subtype`, `type`, `state`, `zone`, `city`, `district`, `custom`, `property`, `date_add`, `date_upd`) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6, :yp7, :yp8, :yp9, :yp10, :yp11, :yp12, :yp13)

Basically the code works, and it saves the model correctly, I think I only get this kind of errors when it is trying to do two inserts at the same time with the same primary key, or maybe it doesn't ensure the uniqueness of the keyword field before inserting.

The script it is soposed to insert or update a keyword in the database, the code is like this:

static public function quickAdd($keyword) {
  if (strlen($keyword['name'])==0)
    return;
  $seo_keyword = SeoKeyword::model()->find("keyword=:keyword", array("keyword"=>$keyword['name']));
  if ($seo_keyword) {
//        return;
  } else {
    $seo_keyword=new SeoKeyword();
  }
  $seo_keyword->keyword=$keyword['name'];
  if (is_numeric($keyword['position'])) {
    $seo_keyword->position=$keyword['position'];
  }
  try {
    $seo_keyword->save();
  } catch (Exception $e) {

  }
  return $seo_keyword;
}

The mysql table looks like this:

CREATE TABLE IF NOT EXISTS `seo_keyword` (
`id_seo_keyword` int(11) NOT NULL AUTO_INCREMENT,
`keyword` varchar(255) NOT NULL,
`position` int(11) DEFAULT NULL,
`date_add` datetime NOT NULL,
`date_upd` datetime NOT NULL,
PRIMARY KEY (`id_seo_keyword`),
UNIQUE KEY `keyword` (`keyword`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=2 ;

I am also getting this kind of error with other tables, all of them are tables that have many rows and are mostly used for stathistical data.

The fact is that I can only see that it could not insert, but there's no error like: primary key problem, servery busy or anything.

  • 写回答

1条回答 默认 最新

  • dry18813 2012-07-16 14:10
    关注

    Try

    var_dump($seo_keyword->getErrors());
    

    after

    $seo_keyword->save();
    

    and lookout what is the actually problem so that insert query fails.

    评论

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制