1

Drupal (7) uses an abstact for handling DB-s.

I'd like to write dynamic queries where i can use drupals abstract.

There's entity wrapper for entities and I'm Looking for something similar to DB-s.

Is there anything like that?

  • Assuming you mean you want a way to simulate strongly typing the rows of a query result, like EMW does with fields...no, Drupal has nothing like that. The DB abstraction layer is a thin helper layer on top of PDO, it doesn't do a whole lot, certainly nothing that complicated – Clive Jun 10 '16 at 13:05
  • Wright. In that case what's a way figuring out how to build up a query? (How exactly are tables connected in DB.) – user3563097 Jun 12 '16 at 22:33
0

If by level of abstraction close to the one given by entity wrappers, you mean a module/library that would allow to write code similar to the following, no, there isn't anything like that.

$field_value = database_wrapper('table')->table_field;
database_wrapper('table')->set('table_field', $new_value)->save();

The level of abstraction that would offer, in the case of reading the database, would not be worth the code you would write to implement it, and it would just save a few method calls. In fact, $field_value = database_wrapper('table')->table_field; would be equivalent to the following Drupal 7 code.

$result = db_query("SELECT table_field FROM {table}");
foreach ($result as $record) {
  // Use $record->table_field;
}

Also for writing, the Drupal 7 code is not much complicated to require a wrapper.

db_insert('table')
  ->fields(array(
    'table_field' => $new_value,
    ))
  ->execute();

Real code would be more complicated than this, but a wrapper that should consider every case would be even more complicated.

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.