-
Notifications
You must be signed in to change notification settings - Fork 14
php 7.4 Trying to access array offset on value of type null in SlugBehavior #43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
$this->_table->getSchema()->getColumn($this->getConfig('field'))['length'] So unless the configured slug field isn't an actual column in the table, why would |
Mr. Jad! 👋🏼 |
@jadb it is not the field, but the schema from a CakePHP core call, that returns null for the length for whatever reason. We are using CakePHP 3.8.x. public function initialize(array $config)
{
if (!$this->getConfig('displayField')) {
$this->setConfig('displayField', $this->_table->getDisplayField());
}
if ($this->getConfig('maxLength') === null) {
//debug($this->_table->getTable());
//debug($this->getConfig('field'));
debug($this->_table->getSchema()->getColumn($this->getConfig('field')));
$this->setConfig(
'maxLength',
$this->_table->getSchema()->getColumn($this->getConfig('field'))['length']
);
}
if ($this->getConfig('unique') === true) {
$this->setConfig('unique', [$this, '_uniqueSlug']);
}
}
Line 122 is this line: $this->_table->getSchema()->getColumn($this->getConfig('field'))['length'] I don't know yet why this is null in the specific case, however, either this needs to be addressed or an exception with a meaningful message should be thrown. |
OK, the issue was that one of the old fixture haven't had the slug field. However, the error handling / info for the developer could be improved. I've made some changes to do so in the SlugBehavior. It is now checking if the field exists and if it has a length. If not it will throw an exception with an error message pointing at the cause. public function initialize(array $config)
{
if (!$this->getConfig('displayField')) {
$this->setConfig('displayField', $this->_table->getDisplayField());
}
$field = $this->getConfig('field');
if (!$this->getTable()->hasField($field)) {
throw new RuntimeException(sprintf(
'SlugBehavior: Table `%s` is missing field `%s`',
$this->getTable()->getTable(),
$field
));
}
$fieldSchema = $this->_table->getSchema()->getColumn($field);
if ($this->getConfig('maxLength') === null) {
if ($fieldSchema['length'] === null) {
throw new RuntimeException(sprintf(
'SlugBehavior: Table `%s.%s` has no length defined',
$this->getTable()->getTable(),
$field
));
}
$this->setConfig(
'maxLength',
$fieldSchema['length']
);
}
if ($this->getConfig('unique') === true) {
$this->setConfig('unique', [$this, '_uniqueSlug']);
}
} |
Running our tests on Scrutinizer-ci.com has shown this issue with 7.4
The text was updated successfully, but these errors were encountered: