I'm having problems using weight enabled node with dynamic query. From my module code:

// Fetch the data from the database, ordered by weight ascending.
  $select = db_select('node', 'n');
  // Join tables
  $select->join('weight_settings', 'ws', 'n.type = ws.type');
  $select->join('weight_weights', 'w', 'n.nid = w.entity_id');
  $select->fields('n', array('title', 'nid', 'type'));
  $select->fields('w', array('weight'));
  $select
    ->condition('n.status', '1')
    ->orderBy('weight', 'ASC');
  $select->range(0, 50);

If I create a new node with the standard node edit form and accept a default weight = 0, then this new node will not be included in the results of this query. Looking in the database I discovered that at submit a record for the node was not being saved to {weight_weights}.

My work-around was to adjust the content type's default weight to equal '-1'. This saved a value to {weight_weights} and consequently returned the new node as part of the result.

I'm filing a bug, but realize that I also may not be constructing the query correctly. If the latter, recommendations are most welcome.

Also, I noticed that the revised default weight value was not being captured by Features. See http://drupal.org/node/1589948

Cheers!

Comments

Status:Active» Fixed

This has been fixed and committed to 7.x-2.x.

Status:Fixed» Needs work

Since the fix I'm getting a PDOException upon saving nodes, because the third field in the weight_weights table is empty but not null. I have temporarily and dirty fixed it like this. It works, but the problem maybe lies elsewhere...

@@ -421,6 +421,9 @@
  * Set the weight of a node.
  */
function _weight_set_weight($node) {
+  if(empty($node->weight_weight)) {
+    $node->weight_weight = 0;
+  }
   db_merge('weight_weights')
     ->key(array('entity_id' => $node->nid))

Status:Needs work» Active

I'm unable to reproduce the error. Could you provide a little more info about your configuration? One thing to check is that the default weight is being applied to nodes when creating them.

Thanks for your response. Actually it was a node of a content type where I haven't activated weight at all.
What would you like to know about my configuration? It's a Drupal 7.14 installation on Linux with Apache 2, PHP 5 and MySQL 5.

Status:Active» Fixed

That's all I needed, thanks! This has been fixed and committed.

Marked #1602692: Undefined property weight_weight in weight_node_insert() (line 342 of weight.module) as duplicate of this issue.

And adding this error message, which is fixed by this commit for the sake of people searching on error messages:

Notice: Undefined property: stdClass::$weight_weight in weight_node_insert() (line 342 of /var/www/mysite/sites/all/modules/contrib/weight/weight.module).

[removed duplicate post]

Status:Fixed» Active

This is not fixed at all. I downloaded the latest version and still get the errors:
SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'weight' at row 1

#0 /home/adrupal/www/includes/database/database.inc(2136): PDOStatement->execute(Array)
#1 /home/adrupal/www/includes/database/database.inc(664): DatabaseStatementBase->execute(Array, Array)
#2 /home/adrupal/www/includes/database/mysql/query.inc(36): DatabaseConnection->query('INSERT INTO {we...', Array, Array)
#3 /home/adrupal/www/includes/database/query.inc(1614): InsertQuery_mysql->execute()
#4 /home/adrupal/www/sites/all/modules/weight/weight.module(434): MergeQuery->execute()
#5 /home/adrupal/www/sites/all/modules/weight/weight.module(352): _weight_set_weight(Object(stdClass))
#6 [internal function]: weight_node_update(Object(stdClass))
#7 /home/adrupal/www/includes/module.inc(823): call_user_func_array('weight_node_upd...', Array)
#8 /home/adrupal/www/modules/node/node.module(1134): module_invoke_all('node_update', Object(stdClass))
#9 /home/adrupal/www/modules/node/node.pages.inc(406): node_save(Object(stdClass))
#10 /home/adrupal/www/includes/form.inc(1443): node_form_submit(Array, Array)
#11 /home/adrupal/www/includes/form.inc(854): form_execute_handlers('submit', Array, Array)
#12 /home/adrupal/www/includes/form.inc(374): drupal_process_form('webform_node_fo...', Array, Array)
#13 /home/adrupal/www/includes/form.inc(131): drupal_build_form('webform_node_fo...', Array)
#14 /home/adrupal/www/modules/node/node.pages.inc(15): drupal_get_form('webform_node_fo...', Object(stdClass))
#15 [internal function]: node_page_edit(Object(stdClass))
#16 /home/adrupal/www/includes/menu.inc(516): call_user_func_array('node_page_edit', Array)
#17 /home/adrupal/www/index.php(21): menu_execute_active_handler()
#18 {main}

I regularly import nodes and am getting errors all over the place due to the weight module. It's simply unacceptable.

Status:Active» Fixed

P.S. it seems to be fixed in the dev version. Using that one now. I hope that it will be merged quickly to the release version!

Regards,
Bas

dev version fixed my issue

Status:Fixed» Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Version:7.x-2.0-rc2» 7.x-2.1

I updated to weight 2.1 but encountered this issue again. It only appears when you save a node programmatically which doesn't have a weight explicitly assigned to it, or has an empty value assigned to it. I thought this was fixed in 2.1, but I reviews the code and saw no changes in the _weight_set_weight function. Imho, in this function, there should be a check; either refusing to save the value to correct it to the default when it is incorrect. Right now, I get the following error on saving a general page node (which does not has weight enabled):

1 exception 'PDOException' with message 'SQLSTATE[HY000]: General error: 1366 Incorrect integer value: '' for column 'weight' at row 1' in /home/ms2/www/includes/database/database.inc:2136
2 Stack trace:
3 #0 /home/ms2/www/includes/database/database.inc(2136): PDOStatement->execute(Array)
4 #1 /home/ms2/www/includes/database/database.inc(664): DatabaseStatementBase->execute(Array, Array)
5 #2 /home/ms2/www/includes/database/mysql/query.inc(36): DatabaseConnection->query('INSERT INTO {we...', Array, Array)
6 #3 /home/ms2/www/includes/database/query.inc(1614): InsertQuery_mysql->execute()
7 #4 /home/ms2/www/sites/all/modules/weight/weight.module(434): MergeQuery->execute()
8 #5 /home/ms2/www/sites/all/modules/weight/weight.module(352): _weight_set_weight(Object(stdClass))
9 #6 [internal function]: weight_node_update(Object(stdClass))
10 #7 /home/ms2/www/includes/module.inc(823): call_user_func_array('weight_node_upd...', Array)
11 #8 /home/ms2/www/modules/node/node.module(1134): module_invoke_all('node_update', Object(stdClass))
12 #9 /home/ms2/www/modules/node/node.pages.inc(406): node_save(Object(stdClass))
13 #10 /home/ms2/www/includes/form.inc(1443): node_form_submit(Array, Array)
14 #11 /home/ms2/www/includes/form.inc(854): form_execute_handlers('submit', Array, Array)
15 #12 /home/ms2/www/includes/form.inc(374): drupal_process_form('page_node_form', Array, Array)
16 #13 /home/ms2/www/sites/all/modules/compare/operations/compare_operation_submit_form.php(57): drupal_build_form('page_node_form', Array)
17 #14 /home/ms2/www/sites/all/modules/compare/operations/compare_operation_type_edit_node.php(84): compare_operation_submit_form->execute()
18 #15 /home/ms2/www/sites/all/modules/compare/operations/compare_operation_type.php(112): compare_operation_edit_node->execute()
19 #16 /home/ms2/www/sites/all/modules/compare/compare.module(531): compare_operation->get_result()
20 #17 /home/ms2/www/sites/all/modules/compare/drush/compare.drush.inc(107): _compare_get_results(NULL, Array, Array)
21 #18 [internal function]: drush_compare_export('/home/ms2/temp/...')
22 #19 /usr/share/drush/includes/command.inc(120): call_user_func_array('drush_compare_e...', Array)
23 #20 /usr/share/drush/includes/command.inc(58): drush_invoke_args('compare-export', Array)
24 #21 [internal function]: drush_invoke('compare-export', '/home/ms2/temp/...')
25 #22 /usr/share/drush/includes/command.inc(721): call_user_func_array('drush_invoke', Array)
26 #23 [internal function]: drush_command('/home/ms2/temp/...')
27 #24 /usr/share/drush/includes/command.inc(204): call_user_func_array('drush_command', Array)
28 #25 /usr/share/drush/drush.php(101): drush_dispatch(Array)
29 #26 /usr/share/drush/drush.php(41): drush_main()
30 #27 {main}

How about the following solution?

/**
* Set the weight of a node.
*/
function _weight_set_weight($node) {
  if (empty($node->weight_weight)) {
    $node->weight_weight = 0;
  }
  db_merge('weight_weights')
    ->key(array('entity_id' => $node->nid))
    ->fields(array(
      'entity_id' => $node->nid,
      'entity_type' => 'node',
      'weight' => $node->weight_weight,
    ))
    ->updateFields(array(
      'weight' => $node->weight_weight,
    ))
    ->execute();
}

I hope you can fix this soon. I've been having this issue with the weight module some time now and have reverted to using 'old style' integer fields which is a shame as the idea behind your module is great.

Thanks!
Bas

Status:Closed (fixed)» Active

Changed the status to active, as this bug wasn't fixed.

Could you give an example of the code you're using that is causing the error?

Status:Active» Closed (cannot reproduce)

I'm unable to reproduce this.

Issue summary:View changes

updated with link to related issue