[Catalyst] automatically updating a join table

Bob Miller bob at computerisms.ca
Thu Jun 5 23:20:29 GMT 2014


I am new to Perl and Catalyst, I hope I my terminology correct.  much
gratitude to the authors of the fine Catalyst documentation, the
tutorials are excellent tools of understanding...

I have been through the tutorial and am currently modelling after the
authentication and authorization article on the advent calendar found


The goal: 

I want users of roleB to create new users that are automatically set to
roleA.  So I created an authentication chain, and have created a related
set of templates and forms to play with.  The goal is to remove the Role
selection option and have the userroles join table updated automatically
at user creation time.

The efforts: 

I started in Form/EditUser.pm has_field arguments by setting a "default
=> 1" argument and omitting the field from displaying in the template,
but it did not update the join field.

I found in HTML::FormHandler::Manual::Defaults an explanation that if
the item object contains an accessor, the default won't be used.  since
in following the example I am using an accessor, it seems I will not
succeed using the form to set the value.

I looked at the useradd sub in the example again, I saw fields being
automatically set like so:

my $user = $c->model('DB::User')->new_result({});

So I figured I should be able to use the accessor to  do the same in the
join table, maybe like:

$user->userroles({'roleid' => '1'});

This, nor any variation on syntax I tried, does not update the join
table when the user is created.

I went back through the basicCRUD tutorial, and found the example where
the book_author table is updated, and it uses an argument called
create_related (or add_to_$rel), so I tried

$user->create_related('userroles', {roleid => '1'});

It actually works quite well in that when the form is processed the user
is created and the proper entries are made in the join table, but has
the unfortunate side effect of creating an empty user every time I hit
the form as well.

In digging around, I found also a new_related function, which based on
what I read should do exactly what create_related does, except only when
the form is processed and not when I touch the form:

$user->new_related('userroles', {roleid => '1'});

But this also does not update the join table when the user is created.

The question: 

have I discovered the right tool but used it incorrectly, or am I going
about this the wrong way?  If the former, that is all I need to know, I
will dance with what I have till the light comes on.  If the latter, can
someone please point me at the page I need to read?

Thank you for taking the time to read...

Bob Miller	
867-334-7117 / 867-633-3760

More information about the Catalyst mailing list