[Dbix-class] stupid ?: how to create new objects & save them?

Justin Guenther jguenther at gmail.com
Sat Apr 1 03:40:39 CEST 2006


> -----Original Message-----
> From: dbix-class-bounces at lists.rawmode.org 
> [mailto:dbix-class-bounces at lists.rawmode.org] On Behalf Of Mark Hedges
> Sent: March 31, 2006 6:39 PM
> To: dbix-class at lists.rawmode.org
> Subject: [Dbix-class] stupid ?: how to create new objects & save them?
> 
> 
> It isn't obvious to me why I can't do this:
> 
>   my $newsletter = $customer->newsletter;
>   my $customer_email = $customer->customer_email;
> 
>   if (!$newsletter) {
>       $newsletter = C::Newsletter->new({ 
>           email  => $customer_email 
>       });
>       warn "newsletter is a '$newsletter'\n";
>       # says it's a C::Newsletter
>   }
>   $newsletter->weekly(1);
>   $customer->newsletter( $newsletter );
>   $newsletter->update;
>   $customer->update;
> 
> I get this error:
> 
>   DBIx::Class::InflateColumn::update(): Not in database at 
> <myscript> line 51
> 
> What?
> 
> How am I supposed to create objects and save them?  I poured 
> through the manual again, but there's nothing about what 
> seems like a basic function.

$newsletter = C::Newsletter->create({ email => $customer_email })

new() only creates the object in memory. The next function after new() in
Row.pm is insert(), you just need to look harder :)

In order to store it in the database, you need to call ->insert() or create
the object with create() instead of new() (create() is just a shortcut for
new()->insert()).

> 
> Mark
> 
> _______________________________________________
> List: http://lists.rawmode.org/cgi-bin/mailman/listinfo/dbix-class
> Wiki: http://dbix-class.shadowcatsystems.co.uk/
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/trunk/DBIx-Class/

Cheers,

   Justin Guenther




More information about the Dbix-class mailing list