This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

nlclothing67410
nlclothing67410

I have a couple simple models: Sku and SkuSize

$hasMany is defined for Sku and $belongsTo is defined for SkuSize.

I am trying to use firstOrNew as so:

$newSku->sizes()->firstOrNew(["size" => $size]);

And this seems to work, however, when I checked the database, the SkuSize saved with an incorrect value for the associated key. This seems the be the behavior whether I save() $newSku before trying this or not. I first thought the string may be getting turned into an integer (the sku 1000BK would save as 1000 in the sku_size table)... But even after trying to use integers rather than strings for the primary key of the sku table, I am seeing this behavior. Strangely enough sku_size.sku sometimes appears to save as 0 too.

as a work around, this seems to be functioning as expected for me:

SkuSize::firstOrNew(["size" => $size, "sku" => $sku]);

Might this be a bug? The syntax seem to be accepted, it is just the value that makes it to the database that appears wrong.

Last updated

nlclothing67410
nlclothing67410

In classic form I figured out a solution shortly after posting this.

I needed to include public $incrementing = false; in the Sku model (it must have still been using string when I tried using IDs). I got a hint when a list was producing similar unexpected values despite the database being correct.

Hope this helps someone!

Last updated

daftspunky
daftspunky

Glad to hear you got it sorted!

1-3 of 3

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.