如何使用 DBIx 来使用 mysql 位

发布于 2024-08-09 23:40:43 字数 857 浏览 2 评论 0原文

我正在尝试访问我的 Catalyst 应用程序中的 mysql 位字段。这是表信息:

SQL:

create table foo{
 ...
 mybitField bit(1) NOT NULL default b'0'
}

我的映射表:

...

mybitField
{
    data_type => "BIT",
    default_value => "b'0'",
    is_nullable => 0,
    size => undef,
  }
...

现在在我的控制器中,我做了一个简单的操作

$c->stash->{foos}=[$c->model('DB::foo')->all];

,在我的模板中,我尝试了以下操作:

[% FOREACH foo IN foos -%]
  <tr>
      [%- IF int(foo.mybitField) %]
                <td>The field is True</td>
        [%- ELSE %]
                <td>The field is False</td>
        [% END %]
  </tr>
[% END -%]

我也尝试了,

[%- IF foo.mybitField %]

但也不起作用。除了更改数据库字段类型本身之外还有其他方法吗?

I am trying to access a mysql bit field in my catalyst app. Here is the table info:

SQL:

create table foo{
 ...
 mybitField bit(1) NOT NULL default b'0'
}

My mapped table:

...

mybitField
{
    data_type => "BIT",
    default_value => "b'0'",
    is_nullable => 0,
    size => undef,
  }
...

Now in my controller, I do a simple

$c->stash->{foos}=[$c->model('DB::foo')->all];

and in my template, I have tried the following:

[% FOREACH foo IN foos -%]
  <tr>
      [%- IF int(foo.mybitField) %]
                <td>The field is True</td>
        [%- ELSE %]
                <td>The field is False</td>
        [% END %]
  </tr>
[% END -%]

I also tried just

[%- IF foo.mybitField %]

but that did not work either. Any other way apart from changing the database field type itself?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀子花开つ 2024-08-16 23:40:43

我刚刚对此进行了测试,MySQL BIT 字段以“原始”二进制值的形式返回,正如预期的那样。
所以看来您遇到的是模板工具包问题。

我不知道你的意思:

[%- IF int(foo.mybitField) %]

我不认为 TT 有 int() 函数。 Perl 的 int() 函数也不会执行您想要的操作。我的建议是在 Perl 中编写一个函数,将打包值转换为常规整数,例如:

my $int = unpack( 'c', $bit_field );

另一种方法是将一些列膨胀列添加到您的模式类中。

__PACKAGE__->inflate_column('mybitField', {
    inflate => sub { unpack( 'c*', shift ) },
});

但是,更新仍然会失败,而且我不知道一个简单解决方案。我自己从未在 MySQL 上使用过 BIT 数据类型 - 我通常使用 CHAR(1) 列。

此外,如果您在 DBIC 邮件列表上提问,您可能会得到更好的答案:[email protected] 或访问 irc.perl.org 上的 #dbix-class 频道。

I just tested this and MySQL BIT fields come back as the "raw" binary value, as expected.
So it seems what you have is a Template Toolkit issue.

I don't know what you mean by:

[%- IF int(foo.mybitField) %]

I don't think TT has an int() function. And Perl's int() function wouldn't do what you want, either. My suggestion would be to either write a function in Perl which converts the packed value into a regular integer, for instance:

my $int = unpack( 'c', $bit_field );

An alternative would to add some column inflation column to your schema classes.

__PACKAGE__->inflate_column('mybitField', {
    inflate => sub { unpack( 'c*', shift ) },
});

However, this still would still fail for updates and I don't know a simple solution for that. I never used the BIT data type myself on MySQL - I usually use a CHAR(1) column.

Also, you might get better answers if you ask on the DBIC mailing list at [email protected] or at the #dbix-class channel on irc.perl.org.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文