j4mie / idiorm

A lightweight nearly-zero-configuration object-relational mapper and fluent query builder for PHP5.
http://j4mie.github.com/idiormandparis/
2.01k stars 369 forks source link

Is there a way to get table columns from new idiorm object? #262

Closed tssk closed 9 years ago

tssk commented 9 years ago

Hello,

I can use array_keys($existing->as_array()); over $existing = ORM::for_table('my_table')->find_one(); to get list of columns in my_table.

I expected that I could do the same for $new = ORM::for_table('my_table')->create(); but the new ORM object _data part is empty. I expected that it would contain empty but prepared structure from my_table.

Does anyone know if there is another way how to get column names eg. for empty table with idiorm?

Thanks,

Tomas

treffynnon commented 9 years ago

If you're using mysql you can use the information in the information_schema database. Otherwise this is simply not how Idiorm works so it might not be a good fit for your project. On 18 Jan 2015 04:09, "tssk" notifications@github.com wrote:

Hello,

I can use array_keys($existing->as_array()); over $existing = ORM::for_table('my_table')->find_one(); to get list of columns in my_table.

I expected that I could do the same for $new = ORM::for_table('my_table')->create(); but the new ORM object _data part is empty. I expected that it would contain empty but prepared structure from my_table.

Does anyone know if there is another way how to get column names eg. for empty table with idiorm?

Thanks,

Tomas

— Reply to this email directly or view it on GitHub https://github.com/j4mie/idiorm/issues/262.

tssk commented 9 years ago

@treffynnon,

Yes, but I am using idiorm also for its abstracting feature. Such code would not be database independent among supported databases.

As I am thinking about it I probably understand why the idiorm works this way. As it is complicated to load information about the columns without saving and selecting data for a record.

So far I have two ideas how to workaround it to get the functionality I need.

1) $record = ORM::for_table('my_table')->create(); ORM::get_db()->beginTransaction(); $record->save(); $record = ORM::for_table('my_table')->where('id', $record->id)->find_one(); $autoincrement = $record->id; $record->id = NULL; ORM::get_db()->rollBack(); ORM::get_db()->exec('ALTER TABLE my_table AUTO_INCREMENT = '.$autoincrement .';');

But it is not ideal as it expects that the table supports transactions. Also raises autoincrement (I don't understand why but it is how MYSQL InnoDB behaves) and lowering it may take along time on large table. So variant might be without transactions just saving and deleting the record. But still I see this as not a good approach on a table where many records maybe created at the same time.

2) $record = ORM::for_table('my_table')->find_one(); foreach(array_keys($record->as_array()) as $column) { $record->$column = NULL; } Much more elegant and safe from my point of view. But you need to be sure you have at least one record in the table.

Missing one feature or two does not make idiorm worse for me. As it is the best ORM DB PHP library I came across for its simplicity of use yet bringing lot of features and well-arranged code :)

Thanks,

Tomas