1

I've modified my schema so that I can store an array of items in one field:

"required_product" => array(
  "type" => "text",
  "length" => 'medium',
  "not null" => TRUE,
  "default" => "",
  "serialize" => true,
),

This works great when using drupal_write_record, as the array data is correctly serialized before putting it into insert/update query and executing.

However, I still have to manually unserialize when I extract the data:

$form_state["values"] = db_fetch_array(db_query("SELECT * FROM {amh_shop_discounts} WHERE discount_id=%d", $discount_id));
$form_state['values']['required_product'] = unserialize($form_state['values']['required_product']);

This is less useful - shouldn't it do both?

Currently running Drupal 6.22

1 Answer 1

5

This is just a guess, so correct me if I'm wrong...

I think that automatic unserialization is not possible because the schema is not loaded by db_query. While drupal_write_record() loads the schema definition before writing to the database, db_query simply executes the query string you put into it, without knowing which schema definition the query relates to.

2
  • Well, that does make sense - and I could probably write up a function that does access the schema in the same way (probably should be in the fetch_object/fetch_array methods), but is there already something that does this?
    – HorusKol
    Commented Oct 8, 2011 at 23:27
  • No, there isn't. Accessing the schema is slow, especially in Drupal 6, you don't want to do this when doing SELECT queries. You know your data, you probably want a _load() function anyway and then you can do the unserialize there.
    – Berdir
    Commented Jun 28, 2012 at 6:26

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.