[Catalyst] basic CRUD confusion
Ian Docherty
catalyst at iandocherty.com
Sat Mar 31 20:10:02 GMT 2007
Jack Lauritsen wrote:
> Hello,
>
> I am building a (test only) site based modifying the classes in the
> catalyst tutorial, and have a hopefully simple question. I have a search
> box that takes an isbn number, and then build a book with the data from
> amazon web services. The problem comes when I want to update the authors
> list. I don't have the author id. How do I get the id out of
> MyAppDB::Author? I tried the following:
>
> ==================================
> foreach my $amazonauthor ($amazonbook->made_by()) {
> my $author = $c->model('MyAppDB::Author')->search({name =>
> $amazonauthor});
>
'search' returns an array, so in scalar context this will put the array
size into '$author' not what you want. Use either
my ($author) = $c->model('MyAppDB::Author')->search({name =>
$amazonauthor});
or
my $author = $c->model('MyAppDB::Author')->search({name =>
$amazonauthor})->first;
> if ($author->{id} != undef) {
> $author = $c->model('MyAppDB::Author')->search({name =>
> $amazonauthor});
> $book->add_to_book_authors({author_id => $author->{id}});
> } else {
> $author = $c->model('MyAppDB::Author')->create({
> name => $amazonauthor # yes, my DB has just \id name\
> });
> $book->add_to_book_authors({author_id => $author->{id}});
> }
> ==================================
> is the syntax wrong "$author->{id}"
>
I think this should be '$author->id'
> or have I made the Author incorrectly?
>
> thanks in advanced for anyone who can point me in the right direction.
>
> Jack.
>
>
More information about the Catalyst
mailing list