Skip to content
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

Transforming Request Data into Entity Properties - unclear #7889

Open
mehov opened this issue Jul 26, 2024 · 6 comments
Open

Transforming Request Data into Entity Properties - unclear #7889

mehov opened this issue Jul 26, 2024 · 6 comments
Milestone

Comments

@mehov
Copy link
Contributor

mehov commented Jul 26, 2024

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:

@markstory markstory added this to the 5.x milestone Jul 26, 2024
@markstory
Copy link
Member

How are you marshalling data into your entity? Is slug part of the data that is being set into the entity?

@mehov
Copy link
Contributor Author

mehov commented Jul 27, 2024

How are you marshalling data into your entity?

Normal find(), nothing special

Is slug part of the data that is being set into the entity?

No, it isn't

@ADmad
Copy link
Member

ADmad commented Jul 27, 2024

Is slug in the accessible fields list of your entity?

@ADmad
Copy link
Member

ADmad commented Jul 27, 2024

Normal find(), nothing special

find() doesn't marshal anything, newEntity() or patchEntity() does.

@markstory
Copy link
Member

Is slug part of the data that is being set into the entity?

No, it isn't

The field transformer won't run then. Field marshalling only happens for fields that exist in the data being set into the entity.

@dereuromark
Copy link
Member

See https://github.com/dereuromark/cakephp-tools/blob/a917b0d2bf2721a083d44b89c0ea53e0221c9db5/src/Model/Behavior/SluggedBehavior.php#L219 for how you can add a slug into the entity based on certain other fields present when the callback is run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants