src/Model/Behavior/KeyValueBehavior.php
public function getKeyValues(Entity $entity, array $keys)
    {
        $scope = $this->config('scope');
        $query = $this->_table->query();

        $query
            ->where([
                'scope' => $scope,
                $this->config('fields.key') . ' IN' => $keys
            ])
            ->hydrate(false);

        // unserialize serialized fields
        $query->formatResults(function (ResultSet $results) {
            return $results->map(function ($row) {
                if ($row['serialized'] === true) {
                    $row['value'] = unserialize($row['value']);
                }
                return $row;
            });
        });

        foreach ($query->all() as $row) {
            $entity->{$row[$this->config('fields.key')]} = $row[$this->config('fields.value')];
        }

        return $entity;
    }
public function getKeyValues(Entity $entity, array $keys)
    {
        $scope = $this->config('scope');
        $query = $this->_table->query();

        $query
            ->where([
                'scope' => $scope,
                $this->config('fields.key') . ' IN' => $keys
            ])
            ->hydrate(false);

        // unserialize serialized fields
        $query->formatResults(function (ResultSet $results) {
            return $results->map(function ($row) {
                if ($row['serialized'] === true) {
                    $row['value'] = unserialize($row['value']);
                }
                return $row;
            });
        });

        foreach ($query->all() as $row) {
            $entity->{$row[$this->config('fields.key')]} = $row[$this->config('fields.value')];
        }

        return $entity;
    }
public function getKeyValues ( Entity $ entity , array $ keys ) { $ scope = $ this -> config ( 'scope' ) ; $ query = $ this -> _table -> query ( ) ; $ query -> where ( [ 'scope' => $ scope , $ this -> config ( 'fields.key' ) . ' IN' => $ keys ] ) -> hydrate ( false ) ; // unserialize serialized fields $ query -> formatResults ( function ( ResultSet $ results ) { return $ results -> map ( function ( $ row ) { if ( $ row [ 'serialized' ] === true ) { $ row [ 'value' ] = unserialize ( $ row [ 'value' ] ) ; } return $ row ; } ) ; } ) ; foreach ( $ query -> all ( ) as $ row ) { $ entity -> { $ row [ $ this -> config ( 'fields.key' ) ] } = $ row [ $ this -> config ( 'fields.value' ) ] ; } return $ entity ; }
Generate a custom entity with all keys as properties and
their corresponding values

Returns a computed entity that can be used by FormHelper.


@param Entity $entity The base entity to customize.
@param array $keys The keys to set on the entity.
@return Entity
Generate a custom entity with all keys as properties and their corresponding values