DBIx::Class resultset creating related data without the select

南笙酒味 提交于 2019-12-13 00:41:16

问题


I'm trying to do something akin to this:

$schema->resultset('Foo')->create({ Property => 1,
                                       Bar => { 
                                                Property => 'non-unique',
                                             },
                                      });

Where Bar is a belongs_to relation to another table, Bar, with an auto-incrementing primary key.

Problem is that behind the scenes, dbix is always doing a select and finding an existing row in Bar with Property = 'non-unique', and inserting that row's PK into Foo. What I'd like it to do instead is create a new row in Bar each time with a new auto-generated primary key.

Is there a way to tell DBIx not to search for a matching related row first, but to force it to always create the related row?


回答1:


create_related() or new_reated() The latter creates the related object, the former creates the object and saves it in the DB.

So, given an Foo->Bar relationship called 'bars', more like:

 $foo = $schema->resultset('Foo')->create({ Property => 1 });
 $bardata = {Property => 'non-unique'};                            
 $foo->create_related('bars', $bardata);


来源:https://stackoverflow.com/questions/11659083/dbixclass-resultset-creating-related-data-without-the-select

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!