Mi Ka
Mi Ka

Reputation: 135

perl DBIx::Class::Schema

I have a problem with DBIx::Class::Schema, I have created db and then tables: User,Post, ... . Then I done this in terminal

dbicdump -o dump_directory=./lib App::Schema 'dbi:mysql:my_db:localhost:3306' root password

Command made lib::App::Schema with Schema.pm and with Result path in it. (With table classes).

This was generated by Schema.pm

use utf8;
package App::Schema;
use strict;
use warnings;
use base 'DBIx::Class::Schema';

__PACKAGE__->load_namespaces;
1;

And I use schema so:

use lib::App::Schema;
my $schema = App::Schema->connect("dbi:mysql:dbname=my_db", "root", "password");
my @m = $schema->resultset('User')->all;

In this file a get error

DBIx::Class::Schema::source(): Can't find source for User at lib/Local/ThisFile.pm line 26

Upvotes: 1

Views: 398

Answers (1)

Borodin
Borodin

Reputation: 126732

Lets assume that you want to create a package Habr::Schema for the schema of your database. You would use the command

dbicdump -o dump_directory=./lib Habr::Schema 'dbi:mysql:my_db:localhost:3306' root password

and thereafter you can access that module using this

use lib './lib';
use Habr::Schema;

although you must be careful with relative paths in use lib and it is always better to use an absolute path so that you may run your code from any working directory

Your full program might look like this. It prints the id column from every row in the User table

use strict;
use warnings 'all';
use feature 'say';

use Habr::Schema;

my $schema = App::Schema->connect(qw/ dbi:mysql:dbname=my_db  root  password /);

for my $user ( $schema->resultset('User')->all ) {
    say $user->id;
}

Upvotes: 1

Related Questions