5

I have a problem, may you guys can help me :)

I have content type called book, and it has field called stock. Instead of using node_save() I want to update value of book stock using manual query. I have this query, if I run from MySQL client (heidisql ex) the value of book stock has changed already, but if I open the node of book nothing happen. Please help me guys..

node_save() method:

$node = node_load($id); // $id -> node id (nid)
$node->field_stock_value[$node->language][0]['value'] = "whatever";
node_save($node);

my query:

db_query("update field_data_field_stock b left join field_revision_field_stock rb on b.revision_id = rb.revision_id set b.field_stock_value = '{$stock}', rb.field_stock_value = '{$stock}' where b.entity_id = {$nid}");

I want to use query instead node_save() because I want to perform batch set for updating node, and you know, node_save() is really weight.

thank you before, happy new year guys.. :D

  • can you tell me the function you do NODE_SAVE on it? please exactly write it. – Yuseferi Dec 31 '13 at 5:43
6

Why not you use field API field_attach_update() instead, it can save the node_save() process.

For example take this.

  $node = new stdClass();
  $node->id = $id_value;          // node id
  $node->type = $bundle_value;  // aka content type

  $node->field_fieldname[LANGUAGE_NONE][0]['value'] = 'some value';
  field_attach_update('node', $node);

Reference: http://www.urbaninsight.com/2011/10/24/saving-nodes-fields-without-saving-node-itself

| improve this answer | |
  • I got following error: EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7663 of /media/www/mywebsite/includes/common.inc). – Rijalul fikri Dec 31 '13 at 7:29
  • you have to provide the content type name instead of $bundle_value for $node->bundle = $bundle_value; so if its page then $node->bundle = "page"; – arpitr Dec 31 '13 at 7:35
  • it's supposed tobe $node->type, not $node->bundle. i tried success with $node->type. thanks :) – Rijalul fikri Dec 31 '13 at 7:36
  • 1
    This is not a public API, use at your own risk. If you have a lot of nodes to process, use Drupal's batch API. It will be a lot slower, but by doing this, you are not calling the relevant hooks, so other modules that might want to do something if those values change aren't notified, if you're using entitycache.module, the entity cace will not be cleared and so on. – Berdir Jan 1 '14 at 9:02
  • Thanks @Berdir I was using this API, but I was not aware of this fact.Thanks again :) . – arpitr Jan 2 '14 at 6:27
0

I've captured mysql query log (as mentioned here: https://stackoverflow.com/questions/6479107/how-to-enable-mysql-query-log ) after editing a node( of type job vacancy). I want to mention how a cck field like "designation" is getting updated in the tables(I've not created new revision though):

-------------------------------------------
DELETE FROM field_data_field_designation 
WHERE  (entity_type = 'node') AND (entity_id = '599') AND (language IN  ('und'))


-------------------------------------------
DELETE FROM field_revision_field_designation 
WHERE  (entity_type = 'node') AND (entity_id = '599') AND (revision_id = '636') AND (language IN  ('und'))

-------------------------------------------
INSERT INTO field_data_field_designation (entity_type, entity_id, revision_id, bundle, delta, language, field_designation_tid) VALUES ('node', '599', '636', 'job_vacancy', '0', 'und', '4641'), ('node', '599', '636', 'job_vacancy', '1', 'und', '4642'), ('node', '599', '636', 'job_vacancy', '2', 'und', '4643'), ('node', '599', '636', 'job_vacancy', '3', 'und', '4644'), ('node', '599', '636', 'job_vacancy', '4', 'und', '4645'), ('node', '599', '636', 'job_vacancy', '5', 'und', '4646'), ('node', '599', '636', 'job_vacancy', '6', 'und', '4647'), ('node', '599', '636', 'job_vacancy', '7', 'und', '4648'), ('node', '599', '636', 'job_vacancy', '8', 'und', '4649'), ('node', '599', '636', 'job_vacancy', '9', 'und', '4650')


-------------------------------------------
INSERT INTO field_revision_field_designation (entity_type, entity_id, revision_id, bundle, delta, language, field_designation_tid) VALUES ('node', '599', '636', 'job_vacancy', '0', 'und', '4641'), ('node', '599', '636', 'job_vacancy', '1', 'und', '4642'), ('node', '599', '636', 'job_vacancy', '2', 'und', '4643'), ('node', '599', '636', 'job_vacancy', '3', 'und', '4644'), ('node', '599', '636', 'job_vacancy', '4', 'und', '4645'), ('node', '599', '636', 'job_vacancy', '5', 'und', '4646'), ('node', '599', '636', 'job_vacancy', '6', 'und', '4647'), ('node', '599', '636', 'job_vacancy', '7', 'und', '4648'), ('node', '599', '636', 'job_vacancy', '8', 'und', '4649'), ('node', '599', '636', 'job_vacancy', '9', 'und', '4650')

We can see that it is deleting the latest revision and inserting back into with new values with the same revision id(I'm not sure about the logic though).

So you need to update both field_data and the field_revision tables with the same values on specific(latest) revision on given nid. I'm not sure how it'd update if I had created new revision while saving the node.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.