Jan van de Laar
Jan van de Laar

Hi all,

I am developing a website using OctoberCMS, and ran into a problem when using a morphOne relationship. I configured the relation in the backend using the RelationController behaviour as explained in the docs. The backend renders this relation as a set of extra fields displayed under the parent, with an Add button. When clicking this button, a dialog box appears where a new child (morphOne) can be created. When trying to save the child, an error: General error: field (foreign_keyname) doesn't have a default vale in query (insert query). I looked in the modules/backend/behaviors/RelationController.php source, and found the following:

In function

/**
 * Create a new related model
*/
public function onRelationManageCreate()

appears the following code:

        /*
         * Has one relations will save as part of the add() call.
         */
        if ($this->deferredBinding || ($this->relationType != 'hasOne')) {
            $newModel->save();
        }

When I change this into

        /*
         * Has one relations will save as part of the add() call.
         */
        if ($this->deferredBinding || ($this->relationType != 'hasOne' && $this->relationType != 'morphOne')) {
            $newModel->save();
        }

The insert succeeds. It looks to me that the morphOne case was added later and this piece of code was not updated for the new case. It might be that a similar bug exists for the morphMany case, but I haven't tested that.

1-1 of 1