In addition, it seems that there are restrictions on writing data to Mongo with embedsMany relationships on update. For instance, in my SubtypeModel, in addition to _id, I have a type field, with records that look like this when embedded in a parent model
"subtypes" : [
{
"_id" : "Small Business",
"type" : "SBABusinessType"
},
{
"_id" : "Firm Fixed Price",
"type" : "pricingType"
},
],
When attempting to update an existing embedded model via save(), no matter what is passed in, this code in WEmbedsMany->associateMany() chokes:
foreach ($records as &$record) {
// @phpcs:ignore SlevomatCodingStandard.Operators.DisallowEqualOperators
if ($record[$primaryKey] == $key) {
$record = $model->getAttributes();
break;
}
with this error
ErrorException: Undefined array key "id"
because, as it indicates, the $recordarray only has an_idvalue, notid`. IT can be demonstrated with this unit test
public function testEmbeddedSubtype()
{
$fca = factory(FederalContractAwardModel::class)->create();
$subtype = new SubtypeModel([
'_id' => 'Time and Materials',
'type' => 'pricingType'
]);
$fca->subtypes()->save($subtype);
$subtype = $fca->subtypes()->first();
$subtype->update([
"_id" => "Time and Materials",
'type' => 'packageType'
]);
$this->assertEquals(
$subtype->_id,
'packageType',
'ID does not match the subtype'
);
}
even if I pass in id, it still errors out. From what we can see it git, this file hasn’t changed, However, since the core EntityModel now has $primaryKey defined as id, there is no match with the data.
How do we resolve this?