0

I'm using DBIx::Class to model the following:

A Recipe with many Tags. The Tag is shared with other Recipe objects.

While creating a Recipe object I want to create a set of Tag objects and associate them with the newly created Recipe object. (The user enters a list of tags and I only have the name of the tag to go with)

For the Tags I could iterate over the list and find one that matches the user entered name or create a new object manually.

I couldn't find a documented findOrCreate type method in DBIx::Class. Any suggestions ?

Sandeep Chayapathi
  • 1,490
  • 2
  • 13
  • 31

1 Answers1

1

If you have a key on the name you can use find_or_create.

bolav
  • 6,938
  • 2
  • 18
  • 42
  • How do you know whether the object was 'found' or 'created'? $obj = $schema->resultset('ObjList')->find_or_create({...some conditions...}); – rajeev Nov 16 '16 at 03:25
  • Please read the documentation. Here is what it says: If you need to know if an existing row was found or a new one created use "find_or_new" and "in_storage" in DBIx::Class::Row instead. Don't forget to call "insert" in DBIx::Class::Row to save the newly created row to the database! – bolav Nov 18 '16 at 20:25