将 DBIx::Class::Result 转换为哈希 [英] Convert a DBIx::Class::Result into a hash

查看:57
本文介绍了将 DBIx::Class::Result 转换为哈希的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 DBIx::Class,我找到了解决问题的方法,谢天谢地.但我相信一定有更好的方法.

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"});

我如何将这个记录转换为一个简单的 hashref 而不必在之后立即进行调用.

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} 

代替

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

如果表格有更多列,这会让我发疯.

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

推荐答案

我猜你说的是 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 } };

这篇关于将 DBIx::Class::Result 转换为哈希的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆