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 serialize when I extract the data - example:

$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

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.

|improve this answer|||||
  • 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 Oct 8 '11 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 Jun 28 '12 at 6:26

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.