Ad

October CMS Models - Overwrite Save Function

- 1 answer

Good day,

I overwrote the save function of my October CMS model.

 public function save(Array $options=[])

When I tried to save, it threw an error.

Declaration of MyCompany\MyPlugin\Models\MyModel::save(array $options = Array) should be compatible with October\Rain\Database\Model::save(array $options = NULL, $sessionKey = NULL)" on line 42 of /a/b/c/d/mysite/plugins/mycompany/myplugin/models/MyModel.php

So I modified the save function.

public function save(Array $options=[], $sessionKey=null)

Then I get this confusing error.

Type error: Argument 1 passed to MyCompany\MyPlugin\Models\MyModel::save() must be of the type array, null given, called in /a/b/c/d/mysite/modules/backend/behaviors/FormController.php on line 251" on line 38 of /a/b/c/d/mysite/plugins/mycompany/myplugin/models/MyModel.php

So, if it's passing a null there, how are models saving, considering the class inheritance forces argument 1 to be an array?

Edit: Here is line 251 of /a/b/c/d/mysite/modules/backend/behaviors/FormController.php

$modelToSave->save(null, $this->formWidget->getSessionKey());

So the base class will accept nulls, even if type hinting requires Array, but upon overwriting the function, nulls are no longer accepted?

Ad

Answer

Your method signature does not match the original October\Rain\Database\Model method signature. If you read the original source, you'll see that the default value for $options is null.

This is important, because it tells PHP the parameter value should be either an array or null. Presently, your function requires an array only.

public function save(array $options = null, $sessionKey = null)
{
    // ...
}
Ad
source: stackoverflow.com
Ad