casiano has asked for the wisdom of the Perl Monks concerning the following question:
I have a Catalyst application using a data base with the following schema:
the field LUGAR is foreign key from table localizacion.IDLOCALIZACION with a relation has_many.sqlite> .schema CREATE TABLE item ( ID INTEGER primary key autoincrement, TITULO text not null, LUGAR INTEGER, KIND INTEGER ); CREATE TABLE localizacion ( IDLOCALIZACION INTEGER PRIMARY KEY AUTOINCREMENT, DESCRIPCION TEXT NOT NULL );
When I try to insert data in item with the following code:
davioth@kathia:~/Proyecto/catalogador/catalyst/Ejemplo$ cat -n lib/Eje +mplo/Controller/Items.pm 1 package Ejemplo::Controller::Items; 2 3 use strict; 4 use warnings; 5 use Data::Dumper; 6 use base qw/Catalyst::Controller::FormBuilder/; 7 .............................................. 24 25 sub list : Local { ................... 36 } 37 38 sub edit : Local Form { 39 my ($self, $c, $id) = @_; 40 41 #Permite buscar un elemento para actualizar una entrad +a 42 my $item = $c->model('EjemploDB::Item')->find_or_new({ +ID => $id}); 43 #Objeto para la creación de formularios 44 my $form = $self->formbuilder; 45 #Indicación del Template que renderiza el formulario 46 $c->stash->{template} = 'items/edit.tt2'; 47 48 #Añadiendo el campo de selección de localización 49 $form->field( 50 name => 'lugar', 51 type => 'select', 52 options => 53 [ map { [ $_->IDLOCALIZACION, $_->DESCRIPCION + ] } $c->model('EjemploDB::Localizacion')->all ], 54 label => 'Localización del Item', 55 required => 1 #Inidca que el campo es obligato +rio 56 ); 57 #Campo para la selección de tipos de medios 58 $form->field( 59 name => 'tipo', 60 type => 'select',#Permite indicar el tipo S +elect en el Formulario 61 options => 62 [ map { [ $_->IDTIPO, $_->DESCRIPCION ] } $c-> +model('EjemploDB::Tipo')->all ], 63 label => 'Tipo de Medio', 64 required => 1 65 ); 66 67 #En caso de que no se indique un id el primer if permitirá cre +ar una nueva entrada 68 if ($form->submitted && $form->validate) { 69 $c->stash->{message} = 'Prueba de Form'; 70 71 my $id_info = $item->infos; 72 my $infos = $c->model('EjemploDB::Info')->find_or_new( +{DATOS => $form->field('info')}); 73 $item->TITULO($form->field('title')); 74 #$item->LUGAR($form->field('lugar')); 75 #$item->add_to_lugar( 76 my $local = $c->model('EjemploDB::Localizacion +')->find($form->field('lugar')); 77 $item->KIND($form->field('tipo')); 78 $infos->DATOS($form->field('info')); 79 $infos->update_or_insert; 80 my $nitem = $item->update_or_insert; 81 my $schema = $c->model('EjemploDB'); 82 print Dumper($schema); 83 print "*************************************\n +"; 84 my $intermedia = $schema->resulset('Localizar' +); 85 print Dumper($intermedia); 86 $item->add_to_lugar ($intermedia->find($form-> +field('lugar'))); 87 $item->add_to_item_info ({ID_INFO => $infos->I +DINFO}); 88 #$item->create_related ('item_info',{ID_INFO = +> $infos->IDINFO}); 89 #Insertar Localización y Tipo 90 #$c->model('EjemploDB::Localizacion')->find($f +orm->field('lugar'))->add_to_itemlugar({ID => $item->ID}); 91 $c->forward('list');#Nos devuelve al listado 92 } else { ..................... 103 } 104 } 105 sub delete: Local { ..................... 112 } ..................... 124 1;
Probably, is that I don't know how to create the realtionship at inserting-time (rather that at configuration time) when I use add_to_relation.[debug] Applying HTML page layout wrappers to items/edit.tt2 [error] Caught exception in Ejemplo::Controller::Items->edit "Can't lo +cate object method "resulset" via package "Ejemplo::Model::EjemploDB" + at /home/davioth/Proyecto/catalogador/catalyst/Ejemplo/script/../lib +/Ejemplo/Controller/Items.pm line 84." [info] Request took 0.221216s (4.520/s)
Can you help?
Many, many thanks
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Catalyst and has_many relationship
by moritz (Cardinal) on Jun 02, 2008 at 12:26 UTC | |
|
Re: Catalyst and has_many relationship
by skirnir (Monk) on Jun 02, 2008 at 19:04 UTC | |
by casiano (Pilgrim) on Jun 03, 2008 at 10:10 UTC |