I have an entity form that user can submit multiple times. What i would like to do is during validation get current values in the database and compare with the once that user just entered. I'm using

$form['#validate'][] = 'my_function';.

So really the questions is how do I get 'current values' that are already stored in the database to one array and 'about to be submitted values' (just updated by user) to the other array. The reason why I want those to be saved, temporary only, in the array is to do comparison.

  • Sounds reasonable - what's your question? Do you need to know how to query the database? How to store values in the database? How to set a form error? Please expand... – Clive May 22 '13 at 9:40
  • the questions is: how do I get current values to one array and about to be submitted values to the other array (questions updated) – sylwester May 22 '13 at 9:42
  • What are 'current' values, and what are 'about to be submitted values'? Please remember we can't see your code, we weren't involved in the spec meeting for this; you need to provide as much background as you can so your situation can be understood. Do you know how to save/retrieve values to/from the database? Adding your current code would be very helpful – Clive May 22 '13 at 9:43
  • current values would be the once that user has already submitted and are saved in the databases. what happens next is, after a while user comes again and submits the same form for the second time. now before those values ('about to be submitted') are saved to the database I would like to be able to compare them with the once that are already saved in the database. – sylwester May 22 '13 at 9:48
  • So breaking your question down to what you're actually asking - you need to know how to query data from the database, right? (bearing in mind comparing two arrays is a language issue and has nothing to do with Drupal) – Clive May 22 '13 at 9:50

If you're looking for a non-SQL way to get data out of the database the db_select() function (which returns an SelectQuery) is the way to go. For obvious reasons there's no implicit API implemented for your custom data tables (you'd need to create that yourself).

Without knowing your schema this can't be totally accurate, but the basics are:

$query = db_select('my_table', 't')
  ->fields('t', array('field1', 'field2'))
  ->condition('some_column', 'some_value');

$results = $query->execute();
foreach ($results as $result) {
  // $result is an object, use it...

SelectQuery has a bunch of helper methods to get specifically formatted data out of the database, so chances are you won't need to loop over the data. You can find a list of those helper functions (and info on the alternative, SQL based approach that still uses the API) here

| improve this answer | |
  • thanks for the answer Clive. is there an other way of doing that if I were to tell you, and should have, that I am using entity forms – sylwester May 23 '13 at 9:18

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.