Skip to content

Transforming Request Data into Entity Properties - unclear #7889

@mehov

Description

@mehov

Hi everyone

Please clarify the documentation for Transforming Request Data into Entity Properties. Currently it's unclear how to get the custom fields from Behaviour into Entities of the Table said Behaviour is attached to. It only shows a small snippet out of context.

I created a behaviour and copied that snipet from documentation.

<?php

namespace MyPlugin\Model\Behavior;

class SluggableBehavior extends \Cake\ORM\Behavior implements \Cake\ORM\PropertyMarshalInterface
{

    public function initialize(array $config): void
    {
        // Confirm SluggableBehavior is connected and loaded
        debug([
            '$this' => $this,
            '$config' => $config,
        ]);
        parent::initialize($config);
    }

    public function buildMarshalMap(\Cake\ORM\Marshaller $marshaller, array $map, array $options): array
    {
        return [
            'slug' => function ($value, $entity) {
                // Transform the value as necessary
                return '123';
            }
        ];
    }

    public function slug($entity)
    {
        return $entity->{$this->getDisplayField()};
    }

}

When I call ->slug on an entity, I get a null. I'm clearly missing something, but I can't understand what from the documentation.

Additional resources I checked:

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions