问题
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