Error on inserting Yii ActiveRecord model to mysql table

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.

Upvotes: 4

Views: 3500

Answers (1)

Onkar Janwa
Onkar Janwa

Reputation: 3950

Try

var_dump($seo_keyword->getErrors());

after

$seo_keyword->save();

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

Upvotes: 4

Related Questions