简体   繁体   English

DBIx:Class - 找不到模型的来源

[英]DBIx:Class - cannot find source for model

I am trying to use DBIx:Class. 我正在尝试使用DBIx:Class。 I have successfully created the Schema class using DBIx:class::Schema::Loader. 我已经使用DBIx:class :: Schema :: Loader成功创建了Schema类。

I can also connect to the database. 我也可以连接到数据库。

#!/usr/bin/perl -w
use Test::More tests => 5;

use_ok('Models::ModelRole');
use_ok('Models::User');

my $model = Models::User->new();

cmp_ok($model->{ModelName}, 'eq', 'User', 'model name');

ok($model->connect(), "connect"); #works


ok($model->{schema}->resultset('User'));

The last test returns the error message: 最后一个测试返回错误消息:

DBIx::Class::Schema::source(): Can't find source for User at ./tests/ModelsTests.pl line 29

This is the structure of the generated class from DBIx:Class::Schema::Loader: 这是DBIx生成的类的结构:Class :: Schema :: Loader:

在此输入图像描述

This is the model user class: 这是模型用户类:

package Models::User;

use DB::Glued::Schema::Result::User;
use Models::ModelRole;
use Moose;


with 'Models::ModelRole';


sub BUILD {
    my $self = shift;

    $self->{schema} = Glued::Schema::Result::User->new();
    my @name = split('::', __PACKAGE__);
    $self->{ModelName} = $name[-1];
}
1;

I hope this is enough information. 我希望这是足够的信息。

Schemata/models have to be connected to a source. 架构/模型必须连接到源。 The DBIC code is only describing the data and its relationships. DBIC代码仅描述数据及其关系。 It's entirely agnostic about the source/connection. 它与源/连接完全无关。

So you must connect DB::Glued::Schema to be able to exercise the model. 所以你必须连接DB::Glued::Schema才能运用模型。 The best way for tests, I think, is to connect to an in :memory: SQLite DB. 我认为,测试的最佳方法是连接到in :memory: SQLite DB。 The DB will be empty of course. DB当然是空的。 There are a few options/approaches for populating it if you need fixtures. 如果你需要固定装置,有一些选项/方法可以填充它。 Search metacpan if you do. 如果你这样做,请搜索metacpan。

There is a nice package to make test connections simple for you: Test::DBIx::Class . 有一个很好的包可以让你简单地测试连接: Test :: DBIx :: Class

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

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