Skip to content

Commit 52bfd5e

Browse files
authored
Merge pull request cakephp#18264 from cakephp/issue-18263
Fix method and finder map population when using BehaviorRegistry::set()
2 parents 1bdb732 + f8fa2a0 commit 52bfd5e

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

src/ORM/BehaviorRegistry.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,24 @@ protected function _getMethods(Behavior $instance, string $class, string $alias)
211211
return compact('methods', 'finders');
212212
}
213213

214+
/**
215+
* Set an object directly into the registry by name.
216+
*
217+
* @param string $name The name of the object to set in the registry.
218+
* @param \Cake\ORM\Behavior $object instance to store in the registry
219+
* @return $this
220+
*/
221+
public function set(string $name, object $object)
222+
{
223+
parent::set($name, $object);
224+
225+
$methods = $this->_getMethods($object, $object::class, $name);
226+
$this->_methodMap += $methods['methods'];
227+
$this->_finderMap += $methods['finders'];
228+
229+
return $this;
230+
}
231+
214232
/**
215233
* Remove an object from the registry.
216234
*

tests/TestCase/ORM/BehaviorRegistryTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,14 @@ public function testLoadDuplicateFinderAliasing(): void
221221
$this->assertTrue($this->Behaviors->hasFinder('renamed'));
222222
}
223223

224+
public function testSet()
225+
{
226+
$this->Behaviors->set('Sluggable', new SluggableBehavior($this->Table, ['replacement' => '_']));
227+
228+
$this->assertEquals(['replacement' => '_'], $this->Behaviors->get('Sluggable')->getConfig());
229+
$this->assertTrue($this->Behaviors->hasMethod('slugify'));
230+
}
231+
224232
/**
225233
* test hasMethod()
226234
*/

0 commit comments

Comments
 (0)