Drupal 7 开发内部培训资料, 分头诗人

关于rules的action设置问题

赞成!
0
否决!

我在一个rules(after saving new content)下的action里设置了用php code来定义。

代码如下:

$author_uid = $node->field_zhenduan_patient['und'][0]['uid'];(换成一个数字也会出现错误,所以这个不是原因)
$node->uid = $author_uid;
node_save($node);

出现这样的错误:

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '291' for key 'PRIMARY': INSERT INTO {node} (nid, vid, type, language, title, uid, status, created, changed, comment, promote, sticky, tnid, translate) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13); Array ( [:db_insert_placeholder_0] => 291 [:db_insert_placeholder_1] => 294 [:db_insert_placeholder_2] => zhenduan_records [:db_insert_placeholder_3] => zh-hans [:db_insert_placeholder_4] => 初诊-09/25/2012 - 08:00 [:db_insert_placeholder_5] => 185 [:db_insert_placeholder_6] => 1 [:db_insert_placeholder_7] => 1348556639 [:db_insert_placeholder_8] => 1348556639 [:db_insert_placeholder_9] => 0 [:db_insert_placeholder_10] => 1 [:db_insert_placeholder_11] => 0 [:db_insert_placeholder_12] => 0 [:db_insert_placeholder_13] => 0 ) in drupal_write_record() (line 7036 of F:\wamp\www\yayi\includes\common.inc).

如果我用db_query直接去改就可以了,所以我想可能是这里用node_save是有问题的,不知是因为什么呢?

1 个回答

赞成!
0
否决!

从错误看,是 nid = 291 已经存在,而你是insert插入数据,nid 也是291,有冲突了。

node_save 按道理是会自动识别insert 还是 update,这个错误比较离奇,你可以安装devel 模块,然后

1
node_save($node); 

改为

1
dpm($node); 

看看 node的nid 是什么?为什么不会自动变成update?