public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
    {
        $query->formatResults(function ($results) use ($options) {
            return $results->map(function ($entity) use ($options) {
                if (!($entity instanceof EntityInterface)) {
                    return $entity;
                }
                foreach ($this->config('columns') as $column) {
                    if ($entity->has($column)) {
                        $eventName = $this->_table->alias() . ".{$column}.defaultValues";
                        $defaultValue = $this->_table->dispatchEvent($eventName, compact('entity'))->result;
                        $currentValue = $entity->get($column);
                        $newValue = $currentValue;

                        if (is_array($currentValue) && is_array($defaultValue)) {
                            $newValue = Hash::merge($defaultValue, $currentValue);
                        } elseif (is_string($currentValue) && $currentValue === '') {
                            $newValue = $defaultValue;
                        } elseif (empty($currentValue) && !empty($defaultValue)) {
                            $newValue = $defaultValue;
                        }

                        $entity->set($column, $newValue);
                        if (!empty($options['flatten']) && is_array($entity->get($column))) {
                            foreach ($entity->get($column) as $key => $value) {
                                $entity->set("{$column}:{$key}", $value);
                            }
                        }
                    }
                }

                return $entity;
            });
        });
    }
public function beforeFind(Event $event, Query $query, ArrayObject $options, $primary)
    {
        $query->formatResults(function ($results) use ($options) {
            return $results->map(function ($entity) use ($options) {
                if (!($entity instanceof EntityInterface)) {
                    return $entity;
                }
                foreach ($this->config('columns') as $column) {
                    if ($entity->has($column)) {
                        $eventName = $this->_table->alias() . ".{$column}.defaultValues";
                        $defaultValue = $this->_table->dispatchEvent($eventName, compact('entity'))->result;
                        $currentValue = $entity->get($column);
                        $newValue = $currentValue;

                        if (is_array($currentValue) && is_array($defaultValue)) {
                            $newValue = Hash::merge($defaultValue, $currentValue);
                        } elseif (is_string($currentValue) && $currentValue === '') {
                            $newValue = $defaultValue;
                        } elseif (empty($currentValue) && !empty($defaultValue)) {
                            $newValue = $defaultValue;
                        }

                        $entity->set($column, $newValue);
                        if (!empty($options['flatten']) && is_array($entity->get($column))) {
                            foreach ($entity->get($column) as $key => $value) {
                                $entity->set("{$column}:{$key}", $value);
                            }
                        }
                    }
                }

                return $entity;
            });
        });
    }
public function beforeFind ( Event $ event , Query $ query , ArrayObject $ options , $ primary ) { $ query -> formatResults ( function ( $ results ) use ( $ options ) { return $ results -> map ( function ( $ entity ) use ( $ options ) { if ( ! ( $ entity instanceof EntityInterface ) ) { return $ entity ; } foreach ( $ this -> config ( 'columns' ) as $ column ) { if ( $ entity -> has ( $ column ) ) { $ eventName = $ this -> _table -> alias ( ) . ".{$column}.defaultValues" ; $ defaultValue = $ this -> _table -> dispatchEvent ( $ eventName , compact ( 'entity' ) ) -> result ; $ currentValue = $ entity -> get ( $ column ) ; $ newValue = $ currentValue ; if ( is_array ( $ currentValue ) && is_array ( $ defaultValue ) ) { $ newValue = Hash :: merge ( $ defaultValue , $ currentValue ) ; } elseif ( is_string ( $ currentValue ) && $ currentValue === '' ) { $ newValue = $ defaultValue ; } elseif ( empty ( $ currentValue ) && ! empty ( $ defaultValue ) ) { $ newValue = $ defaultValue ; } $ entity -> set ( $ column , $ newValue ) ; if ( ! empty ( $ options [ 'flatten' ] ) && is_array ( $ entity -> get ( $ column ) ) ) { foreach ( $ entity -> get ( $ column ) as $ key => $ value ) { $ entity -> set ( "{$column}:{$key}" , $ value ) ; } } } } return $ entity ; } ) ; } ) ; }
Here we set default values for serializable columns.

This method triggers the `<TableAlias>.<columnName>.defaultValues` event, for
example "Plugins.settings.defaultValues" for the "settings" columns of the
"Plugins" table. Event listeners should catch this event and provides the
desired values.

### Options:

- flatten: Flattens serialized information into plain entity properties, for
example `settings:some_option` => `value`, where `settings` is the
serialized column and `some_option` a key of the serialized array value.
Valid only for column that stores array values. Example:

Consider the following entity:

```php
object(Cake\Datasource\EntityInterface) {
'settings' => [
'option_1' => 'Lorem ipsum',
'option_2' => [1, 2, 3, 4],
'option_3' => object,
],
}
```

Once `settings` column is flattened the entity will look as follow:

```php
object(Cake\Datasource\EntityInterface) {
'settings' => [
'option_1' => 'Lorem ipsum',
'option_2' => [1, 2, 3, 4],
'option_3' => object,
],
'settings:option_1' => 'Lorem ipsum',
'settings:option_2' => [1, 2, 3, 4],
'settings:option_3' => object,
}
```

@param \Cake\Event\Event $event The event that was triggered
@param \Cake\ORM\Query $query Query object
@param \ArrayObject $options Additional options as an array
@param bool $primary Whether is find is a primary query or not
@return void
Here we set default values for serializable columns .