Skip to content

Duplicate configs on certain situations #578

Open
@xavier83ar

Description

@xavier83ar

I was dealing with an issue that duplicates items when I passed nameCallback as an array like this [$object, 'methodName'], for example, with this config:

    // inside some Table::initialize()
    $this->addBehavior('Josegonzalez/Upload.Upload', [
        'imagen' => [
            'path' => 'webroot{DS}files{DS}mayoristas{DS}',
            'nameCallback' => [$this, 'buildName'],
        ],
    ]);

nameCallback is never called, to understand why, I read Behavior config after adding it, and nameCallback had this value

[
    0 => \MyObject\Instance,
    1 => "buildName",
    2 => \MyObject\Instance,
    3 => "buildName",
]

Digging into the code I find out that \Josegonzalez\Upload\Model\Behavior\UploadBehavior doesn't have a constructor, so it inherits from \Cake\ORM\Behavior, so the setConfig is called twice:

and in both cases is called with merge = true which is the default, I've tried replace that last line with $this->setConfig($configs, null, 'shallow'); and it works ok, but I don't know if that's the best solution as it is calling setConfig twice anyway, maybe a better solution is to add a constructor and call parent constructor with an empty array as configs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions