Getting primary key (id) after populate

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Getting primary key (id) after populate

Hetényi Csaba
Dear friends

Sorry for newbie question!
I'd like to know, how to get the last inserted row's primary key after a
populate?
I have the following code in a Catalyst app (it inserts data to related
tables too, but in the main table: FoldkHrsz only one row):

# DB Populate
my $hrsz_res = $c->model('DB::FoldkHrsz')->populate([
         {
         %$hrsz_data,
         foldk_alreszlets => \@$alreszlet_AoH,
      foldk_szeljegyzetts => \@$szeljegyzett_adat_AoH,
      foldk_szolgalmis => \@$szolgalmi_adat_AoH,
      foldk_tulajs => \@$tulaj_adat_AoH,
      foldk_jogok_tenyeks => \@$jogok_tenyek_adat_AoH
        },
     ]);

After this, if i try to get the last inserted row's PK with this:

$hrsz_res->id;

..gives an error:
"Can't call method "id" on unblessed reference at ..."

I know, that the populate method's result is:
\@result_objects (scalar context) | @result_objects (list context)
but don't know, how to use them?

Thank You in advance!
Best wishes from Hungary.

Csaba Hetenyi

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Getting primary key (id) after populate

D Perrett
Given that you're evaluating populate in scalar context, the
documentation you've quoted is telling you that you should expect an
arrayref back. You can't call methods on arrayrefs, hence the error.

Therefore, the last item inserted is $hrsz_res->[-1] - and
$hrsz_res->[-1]->id should give you what you need.

Daniel

On 10 September 2015 at 19:04, Hetényi Csaba <[hidden email]> wrote:

> Dear friends
>
> Sorry for newbie question!
> I'd like to know, how to get the last inserted row's primary key after a
> populate?
> I have the following code in a Catalyst app (it inserts data to related
> tables too, but in the main table: FoldkHrsz only one row):
>
> # DB Populate
> my $hrsz_res = $c->model('DB::FoldkHrsz')->populate([
>         {
>         %$hrsz_data,
>         foldk_alreszlets => \@$alreszlet_AoH,
>      foldk_szeljegyzetts => \@$szeljegyzett_adat_AoH,
>      foldk_szolgalmis => \@$szolgalmi_adat_AoH,
>      foldk_tulajs => \@$tulaj_adat_AoH,
>      foldk_jogok_tenyeks => \@$jogok_tenyek_adat_AoH
>        },
>     ]);
>
> After this, if i try to get the last inserted row's PK with this:
>
> $hrsz_res->id;
>
> ..gives an error:
> "Can't call method "id" on unblessed reference at ..."
>
> I know, that the populate method's result is:
> \@result_objects (scalar context) | @result_objects (list context)
> but don't know, how to use them?
>
> Thank You in advance!
> Best wishes from Hungary.
>
> Csaba Hetenyi
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@...

_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Getting primary key (id) after populate

Dave Cross-2
In reply to this post by Hetényi Csaba
On 10/09/15 19:04, Hetényi Csaba wrote:

> Dear friends
>
> Sorry for newbie question!
> I'd like to know, how to get the last inserted row's primary key after a
> populate?
> I have the following code in a Catalyst app (it inserts data to related
> tables too, but in the main table: FoldkHrsz only one row):
>
> # DB Populate
> my $hrsz_res = $c->model('DB::FoldkHrsz')->populate([
>          {
>          %$hrsz_data,
>          foldk_alreszlets => \@$alreszlet_AoH,
>       foldk_szeljegyzetts => \@$szeljegyzett_adat_AoH,
>       foldk_szolgalmis => \@$szolgalmi_adat_AoH,
>       foldk_tulajs => \@$tulaj_adat_AoH,
>       foldk_jogok_tenyeks => \@$jogok_tenyek_adat_AoH
>         },
>      ]);
>
> After this, if i try to get the last inserted row's PK with this:
>
> $hrsz_res->id;
>
> ..gives an error:
> "Can't call method "id" on unblessed reference at ..."
>
> I know, that the populate method's result is:
> \@result_objects (scalar context) | @result_objects (list context)
> but don't know, how to use them?

You're calling populate() in scalar context, so you're getting an array
reference back. You want the first element in the referenced array.

   $hrsz->[0]->id;

But a better solution might be to call populate() in list context.

   my ($hrsz_res) = $c->model('DB::FoldkHrsz')->populate([ ... ]);

Then $hrsz_res will contain a row object which you can use in the way
you were originally expecting.

   $hrsz->id;

hth,

Dave...


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...
Reply | Threaded
Open this post in threaded view
|

Re: Getting primary key (id) after populate

Hetényi Csaba
Dear Daniel & Dave

Thank You all, now i understand, and tried "$hrsz_res->[-1]->id" and
works as i expected!
:)
It is a very valuable mail-list, thanks to You!

Csaba Hetenyi

2015.09.10. 20:16 keltezéssel, Dave Cross írta:

> On 10/09/15 19:04, Hetényi Csaba wrote:
>> Dear friends
>>
>> Sorry for newbie question!
>> I'd like to know, how to get the last inserted row's primary key after a
>> populate?
>> I have the following code in a Catalyst app (it inserts data to related
>> tables too, but in the main table: FoldkHrsz only one row):
>>
>> # DB Populate
>> my $hrsz_res = $c->model('DB::FoldkHrsz')->populate([
>>          {
>>          %$hrsz_data,
>>          foldk_alreszlets => \@$alreszlet_AoH,
>>       foldk_szeljegyzetts => \@$szeljegyzett_adat_AoH,
>>       foldk_szolgalmis => \@$szolgalmi_adat_AoH,
>>       foldk_tulajs => \@$tulaj_adat_AoH,
>>       foldk_jogok_tenyeks => \@$jogok_tenyek_adat_AoH
>>         },
>>      ]);
>>
>> After this, if i try to get the last inserted row's PK with this:
>>
>> $hrsz_res->id;
>>
>> ..gives an error:
>> "Can't call method "id" on unblessed reference at ..."
>>
>> I know, that the populate method's result is:
>> \@result_objects (scalar context) | @result_objects (list context)
>> but don't know, how to use them?
>
> You're calling populate() in scalar context, so you're getting an
> array reference back. You want the first element in the referenced array.
>
>   $hrsz->[0]->id;
>
> But a better solution might be to call populate() in list context.
>
>   my ($hrsz_res) = $c->model('DB::FoldkHrsz')->populate([ ... ]);
>
> Then $hrsz_res will contain a row object which you can use in the way
> you were originally expecting.
>
>   $hrsz->id;
>
> hth,
>
> Dave...
>
>
> _______________________________________________
> List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
> IRC: irc.perl.org#dbix-class
> SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
> Searchable Archive:
> http://www.grokbase.com/group/dbix-class@...


_______________________________________________
List: http://lists.scsys.co.uk/cgi-bin/mailman/listinfo/dbix-class
IRC: irc.perl.org#dbix-class
SVN: http://dev.catalyst.perl.org/repos/bast/DBIx-Class/
Searchable Archive: http://www.grokbase.com/group/dbix-class@...