简体   繁体   English

当期望值时,DBIx :: class find函数返回哈希

[英]DBIx::class find function returns hash when value expected

Database schema: 数据库架构:

create table requests(
    rid integer primary key autoincrement,
    oid integer references orders (oid),
    command varchar(5),
    account varchar(50),
    txn_id varchar(12),
    md5 varchar(30),
    txn_date varchar(14),
    sum float,
    regdt timestamp default current_timestamp,
    constraint u1 unique (command,txn_id)
);
create table orders (
    oid integer primary key autoincrement,
    cid integer references customers (cid),
    pid integer references providers (pid),
    account varchar(50),
    amount float
);  

Mapped to code by DBIx::Class::Schema::Loader. 由DBIx :: Class :: Schema :: Loader映射到代码。

My controller code : 我的控制器代码:

 my $req= $schema->resultset('Request');
 my $order= $schema->resultset('Order');

      my $r= $req->find(
            {   command => 'check',
                txn_id  => $txn_id,
            },
            { key => 'command_txn_id_unique' }
        );
        my $oid=$r->oid;

        $req->create(
            {   command  => $command,
                account  => $account,
                txn_id   => $txn_id,
                md5      => $md5,
                txn_date => $txn_date,
                sum      => $sum,
                oid      => $oid
            }
        );
       my $o = $order->find($oid);

       $o->sum($sum);
       $o->update;

My tracert sqls with DBIC_TRACE=1 我的DBIC_TRACE = 1的tracert sqls

  SELECT me.rid, me.oid, me.command, me.account, me.txn_id, me.md5, me.txn_date, me.sum, me.regdt FROM requests me 
WHERE ( ( me.command = ? AND me.txn_id = ? ) ): 'check', '1358505665'
    SELECT me.oid, me.cid, me.pid, me.account, me.amount FROM orders me 
WHERE ( me.oid = ? ): '18'
    INSERT INTO requests ( account, command, md5, oid, sum, txn_date, txn_id) VALUES ( ?, ?, ?, ?, ?, ?, ? ): '1', 'pay', '44F4BC73D17E3FA906F658BB5916B7DC', '18', '500', '20130118104122', '1358505665'
    DBIx::Class::Storage::DBI::SQLite::_dbi_attrs_for_bind(): Non-integer value supplied for column 'me.oid' despite the integer datatype at /home/.../lib/TestPrj/Test.pm line 128
    SELECT me.oid, me.cid, me.pid, me.account, me.amount FROM orders me WHERE ( me.oid = ? ): 'TestPrj::Model::Result::Order=HASH(0x3dea448)'
    datatype mismatch: bind param (0) =HASH(0x3dea448) as integer at /usr/share/perl5/DBIx/Class/Storage/DBI.pm line 1765.

I don't understand : 我不明白:

Why in first select query $oid = 18 and its ok. 为什么首先选择查询$ oid = 18并确定。 Its really 18. 真的是18。

But in second select query this $oid is a HASH ? 但是在第二次选择查询中,此$ oid是HASH吗? Its not redefined anywhere in my code. 它没有在我的代码中的任何地方重新定义。

UPD: UPD:

Using Data::Dumper by advice @akawhy I saw that $oid is a blessed HASH. 通过咨询使用Data :: Dumper @akawhy为什么我看到$ oid是受祝福的HASH。

So, looks like there is different context 因此,看起来有不同的上下文

scalar in { ... oid => $oid .. .} {... oid => $ oid ..。}中的标量

and hash in find(...). 并在find(...)中进行哈希处理。

I don't know why it not a hash in first case. 我不知道为什么在第一种情况下它不是哈希。

But, when I changed to $oid=$r->get_column($oid) all works fine. 但是,当我更改为$ oid = $ r-> get_column($ oid)时,一切正常。

$resultset->find returns a Result object. $ resultset-> find返回一个Result对象。 You didn't paste your ResultSource classes but as you wrote that you generated them with Schema::Loader I assume the 'oid' column accessor method is the same as the 'oid' relationship accessor. 您没有粘贴ResultSource类,但是在编写使用Schema :: Loader生成它们时,我假设'oid'列访问器方法与'oid'关系访问器相同。 In that case it returns different things depending on scalar vs. list context. 在这种情况下,它会根据标量与列表上下文返回不同的结果。

I suggest to rename your relationship so you have two different accessors for the column value and its relationship. 我建议重命名您的关系,以便您对列值及其关系有两个不同的访问器。

I prefix all relationships with 'rel_' but you might prefer a different naming standard. 我为所有关系加上“ rel_”前缀,但您可能希望使用其他命名标准。

I think my $oid=$r->oid; 我认为my $oid=$r->oid; the $oid is a ref. $oid是一个参考。 you can use ref or Data::Dumper to see its type. 您可以使用refData::Dumper查看其类型。

Maybe you should use $oid->oid 也许你应该使用$oid->oid

And pay attention to this error 并注意这个错误

DBIx::Class::Storage::DBI::SQLite::_dbi_attrs_for_bind(): Non-......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM