| 1 | package Catalyst::TraitFor::Model::DBIC::Schema::Result; | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use Moose::Role; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use Scalar::Util; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.006'; | |||||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | after '_install_rs_models', sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.5
| |||||||||||||||||||||||||||||||||||||||||||||||
| 9 | 1 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 10 | 1 | my $class = $self->_original_class_name; | |||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | my @sources = $self->schema->sources; | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | 1 | ▶ | die "No sources for your schema" unless @sources; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||||
| 16 | 1 | foreach my $moniker (@sources) { | |||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | my $classname = "${class}::${moniker}::Result"; | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | ▶ | *{"${classname}::ACCEPT_CONTEXT"} = sub { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=18 SCAR=32.6
| |||||||||||||||||||||||||||||||||||||||||||||||
| 19 | 10 | my ($result_self, $c, @passed_args) = @_; | |||||||||||||||||||||||||||||||||||||||||||||
| 20 | 10 | my $id = '__' . ref($result_self); | |||||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||||||
| 22 | # Allow one to 'reset' the current IF there's @passed_args. | ||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||
| 24 | 10 | ▶ | delete $c->stash->{$id} if exists($c->stash->{$id}) && scalar(@passed_args); | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
exists $c->stash->{$id} and scalar @passed_args
MC/DC100%1/1
exists $c->stash->{$id} and scalar @passed_args
exists $c->stash->{$id}
Truth table100%2/2
A
A= exists $c->stash->{$id}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 10 | ▶ | return $c->stash->{$id} ||= do { | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$c->stash->{$id} ||= do {
my(@args) = @{$c->request->args;};
my(@arg) = @{$c->request->args;};
if (my $template = $c->action->attributes->{"ResultModelFrom"}) {
@args = eval " {$template->[0]}";
};
my $rs = $c->model($self->model_name)->resultset($moniker);
if (builtin::blessed($passed_args[0] || "") and $passed_args[0]->isa("DBIx::Class::ResultSet") and $passed_args[0]->result_source->source_name eq $moniker) {
$c->log->info("Getting Result from existing ResultSet") if $c->debug;
$rs = shift @passed_args;
};
my(@find) = scalar @passed_args ? @passed_args : @args;
my $find;
if ($c->debug) {
require JSON::MaybeXS;
my $json_with_args = "JSON::MaybeXS"->new("utf8", 1, "allow_nonref", 1);
$find = scalar @find ? $json_with_args->encode(@find) : "[NEW RESULT]";
};
my $return;
if (scalar @find) {
if ($c->debug) {
$c->log->info("Finding model via ${\$self->model_name;}->$moniker" . "::find($find)");
};
$return = $rs->find(@find);
if ($c->debug and not $return) {
$c->log->info("No records for ${\$self->model_name;}->$moniker" . "::find($find)");
};
}
else {
if ($c->debug) {
$c->log->info("No request arguments, returning new_result");
};
$return = $rs->new_result({});
};
return $return
}
MC/DC0%0/2
$c->stash->{$id} ||= do {
my(@args) = @{$c->request->args;};
my(@arg) = @{$c->request->args;};
if (my $template = $c->action->attributes->{"ResultModelFrom"}) {
@args = eval " {$template->[0]}";
};
my $rs = $c->model($self->model_name)->resultset($moniker);
if (builtin::blessed($passed_args[0] || "") and $passed_args[0]->isa("DBIx::Class::ResultSet") and $passed_args[0]->result_source->source_name eq $moniker) {
$c->log->info("Getting Result from existing ResultSet") if $c->debug;
$rs = shift @passed_args;
};
my(@find) = scalar @passed_args ? @passed_args : @args;
my $find;
if ($c->debug) {
require JSON::MaybeXS;
my $json_with_args = "JSON::MaybeXS"->new("utf8", 1, "allow_nonref", 1);
$find = scalar @find ? $json_with_args->encode(@find) : "[NEW RESULT]";
};
my $return;
if (scalar @find) {
if ($c->debug) {
$c->log->info("Finding model via ${\$self->model_name;}->$moniker" . "::find($find)");
};
$return = $rs->find(@find);
if ($c->debug and not $return) {
$c->log->info("No records for ${\$self->model_name;}->$moniker" . "::find($find)");
};
}
else {
if ($c->debug) {
$c->log->info("No request arguments, returning new_result");
};
$return = $rs->new_result({});
};
return $return
}
$c->stash->{$id}
do {
my(@args) = @{$c->request->args;};
my(@arg) = @{$c->request->args;};
if (my $template = $c->action->attributes->{"ResultModelFrom"}) {
@args = eval " {$template->[0]}";
};
my $rs = $c->model($self->model_name)->resultset($moniker);
if (builtin::blessed($passed_args[0] || "") and $passed_args[0]->isa("DBIx::Class::ResultSet") and $passed_args[0]->result_source->source_name eq $moniker) {
$c->log->info("Getting Result from existing ResultSet") if $c->debug;
$rs = shift @passed_args;
};
my(@find) = scalar @passed_args ? @passed_args : @args;
my $find;
if ($c->debug) {
require JSON::MaybeXS;
my $json_with_args = "JSON::MaybeXS"->new("utf8", 1, "allow_nonref", 1);
$find = scalar @find ? $json_with_args->encode(@find) : "[NEW RESULT]";
};
my $return;
if (scalar @find) {
if ($c->debug) {
$c->log->info("Finding model via ${\$self->model_name;}->$moniker" . "::find($find)");
};
$return = $rs->find(@find);
if ($c->debug and not $return) {
$c->log->info("No records for ${\$self->model_name;}->$moniker" . "::find($find)");
};
}
else {
if ($c->debug) {
$c->log->info("No request arguments, returning new_result");
};
$return = $rs->new_result({});
};
return $return
}
Truth table33%1/3
A ||= B
A= $c->stash->{$id}B= do {
my(@args) = @{$c->request->args;};
my(@arg) = @{$c->request->args;};
if (my $template = $c->action->attributes->{"ResultModelFrom"}) {
@args = eval " {$template->[0]}";
};
my $rs = $c->model($self->model_name)->resultset($moniker);
if (builtin::blessed($passed_args[0] || "") and $passed_args[0]->isa("DBIx::Class::ResultSet") and $passed_args[0]->result_source->source_name eq $moniker) {
$c->log->info("Getting Result from existing ResultSet") if $c->debug;
$rs = shift @passed_args;
};
my(@find) = scalar @passed_args ? @passed_args : @args;
my $find;
if ($c->debug) {
require JSON::MaybeXS;
my $json_with_args = "JSON::MaybeXS"->new("utf8", 1, "allow_nonref", 1);
$find = scalar @find ? $json_with_args->encode(@find) : "[NEW RESULT]";
};
my $return;
if (scalar @find) {
if ($c->debug) {
$c->log->info("Finding model via ${\$self->model_name;}->$moniker" . "::find($find)");
};
$return = $rs->find(@find);
if ($c->debug and not $return) {
$c->log->info("No records for ${\$self->model_name;}->$moniker" . "::find($find)");
};
}
else {
if ($c->debug) {
$c->log->info("No request arguments, returning new_result");
};
$return = $rs->new_result({});
};
return $return
}
| |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 10 | my @args = @{$c->request->args}; | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | 10 | my @arg = @{$c->request->args}; # common typo. | |||||||||||||||||||||||||||||||||||||||||||||
| 29 | 10 | ▶ | if(my $template = $c->action->attributes->{ResultModelFrom}) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 1 | @args = (eval " {$template->[0]}"); | |||||||||||||||||||||||||||||||||||||||||||||
| 31 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||
| 33 | ## if the first argument is a resultset of the current model name | ||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ## then use that instead of a new one. | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | 10 | my $rs = $c->model($self->model_name) | |||||||||||||||||||||||||||||||||||||||||||||
| 37 | ->resultset($moniker); | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 10 | ▶ | if( | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition75%6/8
builtin::blessed($passed_args[0] || "") and $passed_args[0]->isa("DBIx::Class::ResultSet") and $passed_args[0]->result_source->source_name eq $moniker
builtin::blessed($passed_args[0] || "") and $passed_args[0]->isa("DBIx::Class::ResultSet")
$passed_args[0] || ''
MC/DC33%1/3
builtin::blessed($passed_args[0] || "") and $passed_args[0]->isa("DBIx::Class::ResultSet") and $passed_args[0]->result_source->source_name eq $moniker
$passed_args[0]
$passed_args[0]->isa("DBIx::Class::ResultSet")
$passed_args[0]->result_source->source_name eq $moniker
Truth table50%2/4
A and B and C
A= $passed_args[0]B= $passed_args[0]->isa("DBIx::Class::ResultSet")C= $passed_args[0]->result_source->source_name eq $moniker
| |||||||||||||||||||||||||||||||||||||||||||||||
| 40 | (Scalar::Util::blessed($passed_args[0]||'')) | ||||||||||||||||||||||||||||||||||||||||||||||
| 41 | and | ||||||||||||||||||||||||||||||||||||||||||||||
| 42 | ($passed_args[0]->isa('DBIx::Class::ResultSet')) | ||||||||||||||||||||||||||||||||||||||||||||||
| 43 | and | ||||||||||||||||||||||||||||||||||||||||||||||
| 44 | ($passed_args[0]->result_source->source_name eq $moniker) | ||||||||||||||||||||||||||||||||||||||||||||||
| 45 | ) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 2 | ▶ | $c->log->info("Getting Result from existing ResultSet") if $c->debug; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 47 | 2 | $rs = shift(@passed_args); | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | ## Arguments passed via ->Model take precident. | ||||||||||||||||||||||||||||||||||||||||||||||
| 53 | 10 | ▶ | my @find = scalar(@passed_args) ? @passed_args : @args; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 10 | my $find; | |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 10 | ▶ | if($c->debug) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 0 | require JSON::MaybeXS; | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | my $json_with_args = JSON::MaybeXS->new(utf8 => 1, allow_nonref=>1); | |||||||||||||||||||||||||||||||||||||||||||||
| 59 | 0 | ▶ | $find = scalar(@find) ? $json_with_args->encode(@find) : '[NEW RESULT]'; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 10 | my $return; | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | 10 | ▶ | if(scalar(@find)) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 64 | 9 | ▶ | if($c->debug) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 0 | $c->log->info("Finding model via ${\$self->model_name}->$moniker"."::find($find)"); | |||||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 9 | $return = $rs->find(@find); | |||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | ▶ | if($c->debug and !$return) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$c->debug and not $return
MC/DC0%0/2
$c->debug and not $return
$c->debug
not $return
Truth table33%1/3
A and not(B)
A= $c->debugB= not $return
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 0 | $c->log->info("No records for ${\$self->model_name}->$moniker"."::find($find)"); | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 72 | } else { | ||||||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | if($c->debug) { | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | $c->log->info("No request arguments, returning new_result"); | |||||||||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 76 | 1 | $return = $rs->new_result(+{}); | |||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||
| 79 | 10 | return $return; | |||||||||||||||||||||||||||||||||||||||||||||
| 80 | }; | ||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||
| 87 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | Catalyst::TraitFor::Model::DBIC::Schema::Result - PerRequest Result from Catalyst Request | ||||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | In your configuration, set the trait: | ||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||
| 95 | MyApp->config( | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 'Model::Schema' => { | ||||||||||||||||||||||||||||||||||||||||||||||
| 97 | traits => ['Result'], | ||||||||||||||||||||||||||||||||||||||||||||||
| 98 | schema_class => 'MyApp::Schema', | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | connect_info => [ ... ], | ||||||||||||||||||||||||||||||||||||||||||||||
| 100 | }, | ||||||||||||||||||||||||||||||||||||||||||||||
| 101 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | Now in your actions you can call the generated models, which get their ->find($id) from | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | $c->request->args. | ||||||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||||||
| 106 | sub user :Local Args(1) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 107 | my ($self, $c, $id) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||||||||||
| 109 | ## Like: $c->model('Schema::User')->find($id) | ||||||||||||||||||||||||||||||||||||||||||||||
| 110 | my $user = $c->model('Schema::User::Result'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||||||
| 113 | You can also control how the 'find' on the Resultset works via an action attribute | ||||||||||||||||||||||||||||||||||||||||||||||
| 114 | ('ResultModelFrom') or via arguments passed to the 'model' call. | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||||||
| 116 | sub user_with_attr :Local Args(1) ResultModelFrom(first_name=>$args[0]) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | my ($self, $c, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||||||||||
| 119 | ## Like: $c->model('Schema::User')->find({first_name=>$args[0]}) | ||||||||||||||||||||||||||||||||||||||||||||||
| 120 | my $user = $c->model('Schema::User::Result'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | If you want to use a resultset that is 'prepared' you can pass it as the first | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | argument: | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||||||
| 126 | sub from_rs :Local Args(1) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | my ($self, $c, $id) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | my $rs = $c->model("Schema::User")->search({first_name=>['john','joe']}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 129 | my $user = $c->model('Schema::User::Result', $rs); | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | (This feature is probably most useful in a chaining setup.) | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | Lastly, if you invoke this method on an action the explicitly defines no arguments | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | you get a new result rather than a database lookup | ||||||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||||||
| 137 | sub new_user_result :Local Args(0) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||||||
| 140 | ## Like: $c->model('Schema::User')->new_result(+{}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | my $new_user_result = $c->model('Schema::User::Result'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||||||
| 146 | Its a common case to get the result of a L<DBIx::Class> '->find' based on the current | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | L<Catalyst> request (typically from the Args attribute). This is an experimental trait | ||||||||||||||||||||||||||||||||||||||||||||||
| 148 | to see if we can usefully encapsulate that common task in a way that is not easily broken. | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | If you can't read the source code and figure out what is going on, might want to stay | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | away for now! | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | When you compose this trait into your MyApp::Model::Schema (subclass of | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | L<Catalyst::Model::DBIC::Schema>) it automatically creates a second PerRequest model | ||||||||||||||||||||||||||||||||||||||||||||||
| 155 | for each ResultSource in your Schema. This new Model is named by taking the name | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | of the resultsource (for example 'Schema::User') and adding '::Result' to it (or | ||||||||||||||||||||||||||||||||||||||||||||||
| 157 | in the example case 'Schema::User::Result'). When you request an instance of this | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | model, it will automatically assume the arguments of the current action is intended | ||||||||||||||||||||||||||||||||||||||||||||||
| 159 | to be the index by which the ->find locates your database row. So basically the two | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | following actions are the same in effect: | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||||||
| 162 | With trait: | ||||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||||
| 164 | sub user :Local Args(1) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 165 | my ($self, $c, $id) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | my $user = $c->model('Schema::User::Result'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||||||
| 169 | Without trait: | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | sub user :Local Args(1) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | my ($self, $c, $id) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 173 | my $user = $c->model('Schema::User')->find($id); | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | I recommend that if you can use L<Catalyst> 5.9009x+ that you use a type constraint | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | to make sure the argument is the correct type (otherwise you risk generating a | ||||||||||||||||||||||||||||||||||||||||||||||
| 178 | database error if the user tries to submit a string arg and the database is expecting | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | an integer: | ||||||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||||||
| 181 | use Types::Standard 'Int'; | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||||||
| 183 | sub user :Local Args(Int) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 185 | my $user = $c->model('Schema::User::Result'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||
| 188 | =head1 SUBROUTINE ATTRIBUTES | ||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||
| 190 | For the case when a result is complex (requires more than one argument) or you | ||||||||||||||||||||||||||||||||||||||||||||||
| 191 | want to use a key other then the PK, you may add a subroutine argument to describe | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | the pattern: | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | sub user_with_attr :Local Args(1) ResultModelFrom(first_name=>$args[0]) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||
| 198 | This is experimental and may change as needed. Basically this get converted | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | to a hashref and submitted to ->find. | ||||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||||
| 201 | =head1 Actions with no arguments | ||||||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||||||
| 203 | If you current action has no arguments, we instead return a new result, which | ||||||||||||||||||||||||||||||||||||||||||||||
| 204 | is a DBIC result that is not yet in storage. You can use this to make a new | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | row in the database and save it: | ||||||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||||||
| 207 | sub new_result :Local Args(0) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 208 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | my $new_user = $c->model('Schema::User::Result'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 210 | $new_user->name('Fido'); | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | $new_user->insert; # Save the new user. | ||||||||||||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||
| 214 | You might find this useful in some common patterns for validating POST parameters | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | and using them to create a new object in the database. | ||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | Please note that you should be quite explicit in setting Args(0) since in many | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | cases leaving the Args attribute off defaults to 'as many args as you care to | ||||||||||||||||||||||||||||||||||||||||||||||
| 219 | send! | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||||||
| 221 | =head1 Passing arguments to ->model | ||||||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||||||
| 223 | Lastly, you may passing arguments to find via the ->model call. The following | ||||||||||||||||||||||||||||||||||||||||||||||
| 224 | are examples | ||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||
| 226 | With trait: | ||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||
| 228 | sub user :Local Args(1) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 229 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 230 | my $user = $c->model('Schema::User::Result', $some_other_id); | ||||||||||||||||||||||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||||||
| 233 | Without trait: | ||||||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||||||
| 235 | sub user :Local Args(1) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 236 | my ($self, $c, $id) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 237 | my $user = $c->model('Schema::User')->find($some_other_id); | ||||||||||||||||||||||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||||||
| 240 | With trait: | ||||||||||||||||||||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||||||||||||||||||||
| 242 | sub user :Local Args(1) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 243 | my ($self, $c) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 244 | my $user = $c->model('Schema::User::Result', +{first_name=>'john'}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||||||
| 247 | Without trait: | ||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||
| 249 | sub user :Local Args(1) { | ||||||||||||||||||||||||||||||||||||||||||||||
| 250 | my ($self, $c, $id) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||
| 251 | my $user = $c->model('Schema::User')->find({first_name=>'john'}); | ||||||||||||||||||||||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||||||
| 254 | If you choose to pass arguments this way, each call will 'reset' the current | ||||||||||||||||||||||||||||||||||||||||||||||
| 255 | model (changing PerRequest into a Factory type). This behavior is still | ||||||||||||||||||||||||||||||||||||||||||||||
| 256 | subject to change). | ||||||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||||||
| 258 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||||||
| 260 | L<Catalyst>, L<Catalyst::Model::DBIC::Schema>. | ||||||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||||||
| 262 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||||||||||||||||||||
| 264 | John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||||||
| 266 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||
| 268 | Copyright 2017, John Napiorkowski L<email:jjnapiork@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||
| 270 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||
| 271 | the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||
| 273 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||