0

I've installed date module. the field data stored in field_data_field_date_start_rent. The field dont have multiple values

I want to select field_date_start_rent_value.

So, I'm testing SELECT it and UPDATE to other date field. It not works.

Below is the SELECT, but I'm using text field to UPDATE to see if it works:

$nid = $node->nid;
$startrent = db_query("SELECT field_date_start_rent_value FROM {field_data_field_date_start_rent} WHERE entity_id = $nid")->fetchField();
$endrentf = db_query("UPDATE {field_data_field_cubagan} SET field_cubagan_value = $startrent WHERE entity_id = $nid");
return $endrentf;

If using the same SELECT query on integer field, it works without problem. I think it must have something different in the SELECT query for date field.

1
  • 1
    You should never use SQL queries to get, and update the value of a field implemented through the field API, for the simple fact field data are not always saved in a database table. It depends from the implemented field storage API, and modules can implement a different field storage API that saves the data in a file, for example.
    – apaderno
    Commented Jun 30, 2012 at 18:23

1 Answer 1

1

It would be a lot easier to use a different set of API calls to do this, you can avoid having to manipulate the database directly:

// Extract the first field item
$date_items = field_get_items('node', $node, 'field_date_start_rent');
$date_item = array_shift($date_items);

// Get the field value
$start_rent = $date_item['value'];

// Update the other field
$node->field_cubagan[$node->language][0]['value'] = $start_rent;

// Save the node
node_save($node);

Just as an aside: you're not making the best use of the DB API at the moment, you should be using placeholders and provide arguments to db_query() rather than interpolating them directly into the query string. The equivalent of your SELECT statement written the recommended way would be:

$args = array(':nid' => $node->nid);
$sql = 'SELECT field_date_start_rent_value FROM {field_data_field_date_start_rent} WHERE entity_id = :nid';
$startrent = db_query($sql, $args)->fetchField();
2
  • In above example, Is $node->language mandatory even if the website don't have translation choice?
    – apasajja
    Commented Jul 1, 2012 at 1:52
  • 1
    Yes, it will be und for undetermined if you don't have a multilingual site; but you still need it in there
    – Clive
    Commented Jul 1, 2012 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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