如何使用 DBIx 来使用 mysql 位
我正在尝试访问我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我刚刚对此进行了测试,MySQL BIT 字段以“原始”二进制值的形式返回,正如预期的那样。
所以看来您遇到的是模板工具包问题。
我不知道你的意思:
我不认为 TT 有 int() 函数。 Perl 的 int() 函数也不会执行您想要的操作。我的建议是在 Perl 中编写一个函数,将打包值转换为常规整数,例如:
另一种方法是将一些列膨胀列添加到您的模式类中。
但是,更新仍然会失败,而且我不知道一个简单解决方案。我自己从未在 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:
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:
An alternative would to add some column inflation column to your schema classes.
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.