Convert a DBIx::Class::Result into a hash

吃可爱长大的小学妹 提交于 2019-12-02 06:30:56

问题


Using DBIx::Class, I found a solution to my issue, thankfully. But I'm sure there has to be a nicer way.

my $record = $schema->resultset("food")->create({name=>"bacon"});

How would I turn this record into a simple hashref instead of having to make this call right after.

my record = $schema->resultset("food")->search({name=>"bacon"})->hashref_array();

Ideally I want to be able to write a code snippet as simple as

 {record=> $record} 

instead of

{record => {name => $record->name, $record->food_id, ...}}

This would drive me insane with a table that has alot more columns.


回答1:


I assume you're talking about DBIx::Class?

my $record = $schema->resultset("food")->create({name=>"bacon"});
my %record_columns = $record->get_columns;

# or, to get a HashRef directly
my $cols = { $record->get_columns };

# or, as you've asked for
my $foo = { record => { $record->get_columns } };



回答2:


What you're looking for is included in DBIx::Class as DBIx::Class::ResultClass::HashRefInflator.



来源:https://stackoverflow.com/questions/32513447/convert-a-dbixclassresult-into-a-hash

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!