| 1 | package DBIx::Class::FormTools; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | our $VERSION = '0.000011'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use 5.10.1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | #use DBIx::Class::FormTools::Form; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use Carp; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use Data::Dump 'pp'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | # use Debug::ShowStuff ':all'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 10 | has 'schema' => (is => 'rw', isa => 'Ref'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | 10 | has '_formdata' => (is => 'rw', isa => 'HashRef'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | 10 | has '_objects' => (is => 'rw', isa => 'HashRef'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | 10 | has '_delayed_attributes' => (is => 'rw', isa => 'HashRef'); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | DBIx::Class::FormTools - Helper module for building forms with multiple related L<DBIx::Class> objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | This document describes DBIx::Class::FormTools version 0.0.11 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | =head2 This is BETA software | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | There may be bugs. The interface might change (But the it hasn't changed in a long time, so it is probably safe to use). | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | =head2 Prerequisites | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | In the examples I use 3 objects, a C<Film>, an C<Actor> and a C<Role>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | C<Role> is a many to many relation between C<Film> and C<Actor>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | package MySchema; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | use base 'DBIx::Class::Schema'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | __PACKAGE__->load_classes(qw[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | Film | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | Actor | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | Role | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | package MySchema::Film; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | __PACKAGE__->table('films'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | __PACKAGE__->add_columns(qw[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | title | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | __PACKAGE__->set_primary_key('id'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | __PACKAGE__->has_many(roles => 'MySchema::Role', 'film_id'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | package MySchema::Actor; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | __PACKAGE__->table('films'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | __PACKAGE__->add_columns(qw[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | __PACKAGE__->set_primary_key('id'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | __PACKAGE__->has_many(roles => 'MySchema::Role', 'actor_id'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | package MySchema::Role; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | __PACKAGE__->table('roles'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | __PACKAGE__->add_columns(qw[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | film_id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | actor_id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | __PACKAGE__->set_primary_key(qw[ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | film_id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | actor_id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | ]); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | __PACKAGE__->belongs_to(film_id => 'MySchema::Film'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | __PACKAGE__->belongs_to(actor_id => 'MySchema::Actor'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | =head2 In your Controller | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | use DBIx::Class::FormTools; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | my $formtool = DBIx::Class::FormTools->new({ schema => $schema }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | =head2 In your view - L<HTML::Mason> example | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | <%init> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | my $film = $schema->resultset('Film')->find(42); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | my $actor = $schema->resultset('Actor')->find(24); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | my $role = $schema->resultset('Role')->new; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | </%init> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | <form> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | <input | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | name="<% $formtool->fieldname($film, 'title', 'o1') %>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | type="text" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | value="<% $film->title %>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | <input | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | name="<% $formtool->fieldname($film, 'length', 'o1') %>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | type="text" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | value="<% $film->length %>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | <input | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | name="<% $formtool->fieldname($film, 'comment', 'o1') %>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | type="text" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | value="<% $film->comment %>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | <input | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | name="<% $formtool->fieldname($actor, 'name', 'o2') %>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | type="text" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | value="<% $actor->name %>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | <input | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | name="<% $formtool->fieldname($role, undef, 'o3', { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | film_id => 'o1', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | actor_id => 'o2' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | }) %>" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | type="hidden" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | value="dummy" | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | /> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | </form> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | =head2 In your controller (or cool helper module, used in your controller) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | my @objects = $formtool->formdata_to_objects(\%querystring); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | foreach my $object ( @objects ) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | # Assert and Manupulate $object as you like | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | $object->insert_or_update; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | =head2 Introduction | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | L<DBIx::Class::FormTools> is a data serializer, that can convert HTML formdata | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | to L<DBIx::Class> objects based on element names created with | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | L<DBIx::Class::FormTools>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | It uses user supplied object ids to connect the objects with each-other. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | The objects do not need to exist on beforehand. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | The module is not ment to be used directly, although it can of-course be done | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | as seen in the above example, but rather used as a utility module in a | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | L<Catalyst> helper module or other equivalent framework. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | =head2 Connecting the dots - The problem at hand | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | Creating a form with data from one object and storing it in a database is | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | easy, and several modules that does this quite well already exists on CPAN. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | What I am trying to accomplish here, is to allow multiple objects to be | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | created and updated in the same form - This includes the relations between | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | the objects i.e. "connecting the dots". | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | =head2 Non-existent ids - Enter object_id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | When converting the formdata to objects, we need "something" to identify the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | objects by, and sometimes we also need this "something" to point to another | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | object in the formdata to signify a relation. For this purpose we have the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | C<object_id> which is user definable and can be whatever you like. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | =head2 C<new> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | Arguments: { schema => $schema } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | Creates new form helper | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | my $formtool = DBIx::Class::FormTools->new({ schema => $schema }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | =head2 C<schema> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | Arguments: None | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | Returns the schema | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | my $schema = $formtool->schema; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | =head2 C<fieldname> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | Arguments: $object, $accessor, $object_id, $foreign_object_ids | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | my $name_film = $formtool->fieldname($film, 'title', 'o1'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | my $name_actor = $formtool->fieldname($actor, 'name', 'o2'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | my $name_role = $formtool->fieldname($role, undef,'o3', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | { film_id => 'o1', actor_id => 'o2' } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | my $name_role = $formtool->fieldname($role,'charater','o3', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | { film_id => 'o1', actor_id => 'o2' } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | Creates a unique form field name for use in an HTML form. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | =item C<$object> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | The object you wish to create a key for. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | =item C<$accessor> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | The attribute in the object you wish to create a key for. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | =item C<$object_id> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | A unique string identifying a specific object in the form. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | =item C<$foreign_object_ids> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | A C<HASHREF> containing C<attribute =E<gt> object_id> pairs, use this to | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | connect objects with each-other as seen in the above example. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | sub fieldname | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | 57 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
fieldname: called CC=7 SCAR=19.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | 57 | my ($self,$object,$attribute,$object_id,$foreign_object_ids) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | # Get class name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | 57 | ▶ | my $class = $object->source_name || ref($object); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$object->source_name || ref $object
MC/DC0%0/2
$object->source_name || ref $object
$object->source_name
ref $object
Truth table33%1/3
A || B
A= $object->source_nameB= ref $object
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | 57 | my @primary_keys = $object->primary_columns; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | my %relationships | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | 57 | = ( map { $_,$object->relationship_info($_) } $object->relationships ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | 57 | my %id_fields = (); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | 57 | foreach my $primary_key ( @primary_keys ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | # Field is foreign key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | 62 | ▶ | if ( exists $relationships{$primary_key} ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | 10 | $id_fields{$primary_key} = $foreign_object_ids->{$primary_key}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | # Field is local | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 52 | ▶ | $id_fields{$primary_key} | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $object and $object->$primary_key
MC/DC50%1/2
ref $object and $object->$primary_key
ref $object
$object->$primary_key
Truth table66%2/3
A and B
A= ref $objectB= $object->$primary_key
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | = ( ref($object) && $object->$primary_key ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | ? $object->$primary_key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | : 'new'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | # Build object key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | my $fieldname = join('|', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | 'dbic', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | $object_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | $class, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | 57 | ▶ | join(q{;}, map { "$_:".$id_fields{$_} } keys %id_fields), | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$attribute || ''
MC/DC100%1/1
$attribute || ''
$attribute
Truth table100%2/2
A
A= $attribute
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | ($attribute || ''), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | 57 | return($fieldname); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | =head2 C<formdata_to_object_hash> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | Arguments: \%formdata | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | my @objects = $formtool->formdata_to_objects($formdata); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | Turn formdata(a querystring) in the form of a C<HASHREF> into an C<HASHREF> of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | C<DBIx::Class> objects indexed by object_id. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | sub formdata_to_object_hash | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
formdata_to_object_hash: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | 7 | my ($self,$formdata) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | # my $to_object_indent = indent(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | 7 | my $objects = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 7 | ▶ | my $rs = $self->schema->txn_do(sub{ | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=9 SCAR=22.4
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | # Cleanup old objects | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 7 | $self->_objects({}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 7 | $self->_formdata({}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 7 | $self->_delayed_attributes({}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | # Extract all dbic fields | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 7 | my @dbic_formkeys = grep { /^dbic\|/ } keys %$formdata; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | # Create a todo list with one entry for each unique objects | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | 7 | my $to_be_inflated = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | # Sort data into piles for later object creation/updating | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 7 | foreach my $formkey ( @dbic_formkeys ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | 43 | my ($prefix,$object_id,$class,$id,$attribute) = split(/\|/,$formkey); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | # Store form value for $attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | 43 | ▶ | if ( $attribute ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 41 | my $real_attribute = $self->_get_real_attribute_from_class($class,$attribute); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | 41 | my $metadata = $self->schema->source($class)->column_info($real_attribute); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 41 | my $value = $formdata->{$formkey}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | # Handle empty fields for numeric attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | 41 | ▶ | $value = undef if !$value && $self->schema->storage->is_datatype_numeric($metadata->{data_type}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $value and $self->schema->storage->is_datatype_numeric($metadata->{"data_type"})
MC/DC0%0/2
not $value and $self->schema->storage->is_datatype_numeric($metadata->{"data_type"})
not $value
$self->schema->storage->is_datatype_numeric($metadata->{"data_type"})
Truth table33%1/3
not(A) and B
A= not $valueB= $self->schema->storage->is_datatype_numeric($metadata->{"data_type"})
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | # Handle empty fields for date attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 41 | ▶ | $value = undef if !$value && exists($metadata->{_ic_dt_method}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $value and exists $metadata->{"_ic_dt_method"}
MC/DC0%0/2
not $value and exists $metadata->{"_ic_dt_method"}
not $value
exists $metadata->{"_ic_dt_method"}
Truth table33%1/3
not(A) and B
A= not $valueB= exists $metadata->{"_ic_dt_method"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | # Store value | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | 41 | $self->_formdata->{$object_id}->{'content'}->{$real_attribute} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | # Build id field | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | 43 | my %id; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | 43 | foreach my $field ( split(/;/,$id) ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 46 | my ($key,$value) = split(/:/,$field); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | 46 | $id{$key} = $value; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | # Store id field | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 43 | $self->_formdata->{$object_id}->{'form_id'} = \%id; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | # Save class name and oid in the todo list | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | # println "$class | $object_id"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 43 | $to_be_inflated->{"$class|$object_id"} = { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | class => $class, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | object_id => $object_id, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | # Build objects from form data | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 7 | foreach my $todo ( values %$to_be_inflated ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | my $object = $self->_inflate_object( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | $todo->{ 'object_id' }, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 19 | $todo->{ 'class' }, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 19 | $objects->{ $todo->{ 'object_id' } } = $object; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | 7 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | 7 | foreach my $oid ( keys %{$self->_delayed_attributes} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | # println "Setting delayed attributes for '$oid'"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | 6 | foreach my $accessor ( keys %{$self->_delayed_attributes->{$oid}} ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | # println "- ",ref($objects->{$oid}),"->$accessor( ",ref($self->_delayed_attributes->{$oid}{$accessor})," )"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | 10 | $objects->{$oid}->$accessor($self->_delayed_attributes->{$oid}{$accessor}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | # Cleanup old objects | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 7 | $self->_objects({}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | 7 | $self->_formdata({}); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 7 | return($objects); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | =head2 C<formdata_to_objects> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | Arguments: \%formdata | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | my @objects = $formtool->formdata_to_objects($formdata); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | Turn formdata(a querystring) in the form of a C<HASHREF> into an C<ARRAY> of | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | C<DBIx::Class> objects. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | 2 | ▶ | sub formdata_to_objects { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
formdata_to_objects: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | 2 | my ($self,$formdata,$inflate_only) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | 2 | my $hash = $self->formdata_to_object_hash($formdata,$inflate_only); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | 2 | return values %$hash; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | 67 | ▶ | sub _get_real_attribute_from_class { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_real_attribute_from_class: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | 67 | my ($self,$class,$attribute) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | # Get relationship info for the attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | 67 | my $relationship_info = $self->schema->source($class)->relationship_info($attribute); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | # Resolve the actual column name in the table (e.g. a relation named 'location' might be called 'location_id' in the result_source) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | my $real_attribute = ( $relationship_info && $relationship_info->{attrs}{accessor} eq 'filter' ) ? $attribute | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | 67 | ▶ | : ( $relationship_info && $relationship_info->{attrs}{accessor} eq 'single' ) ? (keys %{$relationship_info->{'attrs'}{'fk_columns'}})[0] | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition83%5/6
$relationship_info and $relationship_info->{"attrs"}{"accessor"} eq "filter"
$relationship_info and $relationship_info->{"attrs"}{"accessor"} eq "single"
MC/DC100%2/2
$relationship_info and $relationship_info->{"attrs"}{"accessor"} eq "filter"
$relationship_info
$relationship_info->{"attrs"}{"accessor"} eq "filter"
MC/DC50%1/2
$relationship_info and $relationship_info->{"attrs"}{"accessor"} eq "single"
$relationship_info
$relationship_info->{"attrs"}{"accessor"} eq "single"
Truth table100%3/3
A and B
A= $relationship_infoB= $relationship_info->{"attrs"}{"accessor"} eq "filter"
Truth table66%2/3
A and B
A= $relationship_infoB= $relationship_info->{"attrs"}{"accessor"} eq "single"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | : $attribute; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | 67 | return $real_attribute; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | sub _flatten_id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_flatten_id: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | 0 | my ($id) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | return join(';', map { $_.':'.$id->{$_} } sort keys %$id); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | sub _inflate_object | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | 29 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_inflate_object: called CC=18 SCAR=29.1
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | 29 | my ($self,$oid,$class) = @_; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | # my $inflate_indent = indent(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | # println "[$oid] _inflate_object - Begin"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | 29 | my $id; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | 29 | my $attributes = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | # Object exists in form | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | 29 | ▶ | if ( exists($self->_formdata->{$oid}) ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | 28 | $id = $self->_formdata->{$oid}->{'form_id'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | 28 | $attributes = $self->_formdata->{$oid}->{'content'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | # oid does not exist in the formdata, use oid is a real id | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | else { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | 1 | $id = { id => $oid }; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | # We must have an attribute hash if we want to call 'new' later on | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | 29 | ▶ | $attributes = {} unless $attributes; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | # Return object if is already inflated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | return $self->_objects->{$class}->{$oid} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | 29 | ▶ | if ( $self->_objects->{$class} && $self->_objects->{$class}->{$oid} ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->_objects->{$class} and $self->_objects->{$class}{$oid}
MC/DC100%2/2
$self->_objects->{$class} and $self->_objects->{$class}{$oid}
$self->_objects->{$class}
$self->_objects->{$class}{$oid}
Truth table100%3/3
A and B
A= $self->_objects->{$class}B= $self->_objects->{$class}{$oid}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | # Build a lookup hash of fields that are relationship fields | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | my $relationships = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | 20 | map { $_,$self->schema->source($class)->relationship_info($_) } | |||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | $self->schema->source($class)->relationships | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | # Inflate foreign fields that map to a *single* column | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | 20 | my $todo_delayed_attributes = {}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | 20 | foreach my $foreign_accessor ( keys %$relationships ) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | # Resolve foreign class name | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | 39 | my $foreign_class = $self->schema->source($relationships->{$foreign_accessor}->{'class'})->result_class; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | 39 | my $foreign_relation_type = $relationships->{$foreign_accessor}->{'attrs'}->{'accessor'}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | # println "[$oid] Processing $foreign_accessor for $foreign_class"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | # Do not process multicolumn relationships, they will be processed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | # seperatly when the object to which they relate is inflated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | # I.e. only process "local" attributes | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | 39 | ▶ | next if $foreign_relation_type eq 'multi'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | # Resolve the actual column name in the table (e.g. a relation named 'location' might be called 'location_id') | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | 26 | my $real_foreign_accessor = $self->_get_real_attribute_from_class($class,$foreign_accessor); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | # Lookup foreign object id or real id, if the foreign object has already been inflated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | my $foreign_oid = ( exists($self->_formdata->{$oid}{'form_id'}{$real_foreign_accessor}) ) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | ? $self->_formdata->{$oid}{'form_id'}{$real_foreign_accessor} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | 26 | ▶ | : $self->_formdata->{$oid}{'content'}{$real_foreign_accessor}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | # say "Formdata for local object: ".pp($self->_formdata); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | # println "[$oid] Foreign oid to inflate ", $foreign_oid; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | # println pp($self->_formdata->{$oid}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | # No id found, no inflate needed | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | 26 | ▶ | next unless $foreign_oid; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | 10 | my $foreign_object = $self->_inflate_object( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | $foreign_oid, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | $foreign_class, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | # Store object for later use | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | 10 | $self->_objects->{$foreign_class}->{$oid} = $foreign_object; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | # If the field is part of the id then store it there as well | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | 10 | ▶ | $id->{$foreign_accessor} = $foreign_object->id if exists $id->{$foreign_accessor}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | # If the inflated foreign object is new, its 'id' will be undefined. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | # Therefore we delay adding the foreign object to the local object, until the local object have been inflated or created. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | # println "[$oid] Delaying setting $foreign_accessor to ".ref($foreign_object); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | 10 | $self->_delayed_attributes->{$oid}{$foreign_accessor} = $foreign_object; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | # $attributes->{$real_foreign_accessor} = $foreign_object->id; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | # All foreign objects have been now been inflated | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | # Look up object in memory | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | 20 | my $object = $self->_objects->{$class}->{$oid}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | # Lookup in object in db | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | 20 | ▶ | unless ( $object ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | 20 | my $source = $self->schema->source($class); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | # Don't lookup object if id is 'new' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | $object = $self->schema->resultset($source->source_name)->find($id) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 485 | 20 | ▶ | unless grep { defined($id->{$_}) && $id->{$_} eq 'new' } $source->primary_columns; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $id->{$_} && $id->{$_} eq 'new'
MC/DC100%2/2
defined $id->{$_} && $id->{$_} eq 'new'
defined $id->{$_}
$id->{$_} eq 'new'
Truth table100%3/3
A && B
A= defined $id->{$_}B= $id->{$_} eq 'new'
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 486 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 488 | # Still no object, the create it | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 489 | 20 | ▶ | unless ( $object ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 490 | 13 | $object = $self->schema->resultset($class)->new($attributes); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 491 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 493 | # If we have a object update it with form data, if it exists | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 494 | # println "[$oid] We have an object: ".ref($object).", add to todo list"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 495 | # println "[$oid] Attributes: ".pp($attributes); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 496 | 20 | ▶ | $object->set_columns($attributes) if $object && $attributes; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$object and $attributes
MC/DC0%0/2
$object and $attributes
$object
$attributes
Truth table33%1/3
A and B
A= $objectB= $attributes
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 498 | # Store object for later use | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 499 | 20 | ▶ | if ( $id && $object ) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$id and $object
MC/DC0%0/2
$id and $object
$id
$object
Truth table33%1/3
A and B
A= $idB= $object
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 500 | # say "Storing object ".ref($object)." for later use."; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 501 | 20 | $self->_objects->{$class}->{$oid} = $object; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 502 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 503 | # println "[$oid] _inflate_object - End"; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 504 | 20 | return($object); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 505 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 507 | 10 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||||||||||||||||||||||
| 508 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||