Search code examples
silverstripemodeladmin

Uncaught LogicException: byID can't be called on an UnsavedRelationList


I am creating a model_admin menu to manage orders. The 'Order' data object has a Has-Many relationship to 'Package' and the 'Package' has a Has-Many relationship to 'Product'. I am using gridfield relation editor in Order and Package to add/link new 'Package' and 'Product'. So basically the process is to click on 'Add Order' then click on 'Add Package' then click on 'Add Product' and then fill in the product information.

Until this point, everything works fine, but when I entered all the product fields and clicked on create an error 'Uncaught LogicException: byID can't be called on an UnsavedRelationList' is thrown. I understand this is because the 'Package' and 'Order' has not been created so it can not associate the products with them.

Of course I can create the Order first, then create the package, then add the 'Product', then everything works fine. However this is not intuitive and it is supposed to add products to packages and then create orders. I don't know if there is any way to implement my requirement, or if using gridfields within model_admin form is not the right solution for this.

Looking forward to any suggestions. Thank you.


Solution

  • This usually means that you're trying to do something to a has-many or many-to-many relationship before the core record is saved. Check you onBeforeWrite handlers. I often find it's helpful to hide the gridfields for related lists if $this->isInDB() is false.