Adds a behavior to this table's behavior collection. Behaviors
provide an easy way to create horizontally re-usable features
that can provide trait like functionality, and allow for events
to be listened to.
Example:
Load a behavior, with some settings.
$this->addBehavior('Tree', ['parent' => 'parentId']);
Behaviors are generally loaded during Table::initialize().
/** * testCamposEmArray * * @retun void * @access public */ public function testCamposEmArray() { $this->Noticias->addBehavior("CakePtbr.AjusteData", ["autorizado_em", "publicado_em"]); $noticia = $this->__preparaNoticia("25/03/15 16:42:05"); $this->assertEquals("2015-03-22", $noticia->get("autorizado_em")); $this->assertEquals("2015-03-25 16:42:05", $noticia->get("publicado_em")); }
public function setUp() { parent::setUp(); $this->entityMap = ['Authors' => Author::class, 'Users' => User::class, 'Editors' => Editor::class, 'Readers' => Reader::class, 'Subscribers' => Reader::class, '' => User::class]; $this->table = TableRegistry::get('Users'); $this->table->entityClass(User::class); $authors = TableRegistry::get('Authors', ['table' => 'users']); $editors = TableRegistry::get('Editors', ['table' => 'users']); $readers = TableRegistry::get('Readers', ['table' => 'users']); $authors->addBehavior('Robotusers/TableInheritance.Sti'); $editors->addBehavior('Robotusers/TableInheritance.Sti'); $readers->addBehavior('Robotusers/TableInheritance.Sti'); $this->table->addBehavior('Robotusers/TableInheritance.StiParent', ['discriminatorMap' => ['Authors' => 'Authors', 'Editors' => 'Editors'], 'tableMap' => ['Readers' => ['Readers', 'Subscribers']]]); $authors->entityClass(Author::class); $editors->entityClass(Editor::class); $readers->entityClass(Reader::class); }
/** * Loads the User behavior for the user model if it is not already loaded * * @return void */ public function loadUserBehaviour() { if ($this->_config['autoloadBehavior'] && !$this->UserTable->hasBehavior('UserTools.User')) { if (is_array($this->_config['autoloadBehavior'])) { $this->UserTable->addBehavior('Burzum/UserTools.User', $this->_config['autoloadBehavior']); } else { $this->UserTable->addBehavior('Burzum/UserTools.User'); } } }
/** * Initial Tree * * - One * -- One-SubA * - Two * -- Two-SubA * --- Two-SubA-1 * ---- Two-SubA-1-1 * - Three * - Four * -- Four-SubA * * @return void */ public function setUp() { parent::setUp(); $this->Tree = new TreeHelper(new View(null)); $this->Table = TableRegistry::get('AfterTrees'); $this->Table->addBehavior('Tree'); //$this->Table->truncate(); $connection = ConnectionManager::get('test'); $sql = $this->Table->schema()->truncateSql($connection); foreach ($sql as $snippet) { $connection->execute($snippet); } //$this->Table->deleteAll(array()); $data = [['name' => 'One'], ['name' => 'Two'], ['name' => 'Three'], ['name' => 'Four'], ['name' => 'One-SubA', 'parent_id' => 1], ['name' => 'Two-SubA', 'parent_id' => 2], ['name' => 'Four-SubA', 'parent_id' => 4], ['name' => 'Two-SubA-1', 'parent_id' => 6], ['name' => 'Two-SubA-1-1', 'parent_id' => 8]]; foreach ($data as $row) { $row = new Entity($row); $this->Table->save($row); } }
/** * Test adding a behavior that is a duplicate. * * @return void */ public function testAddBehaviorDuplicate() { $table = new Table(['table' => 'articles']); $this->assertNull($table->addBehavior('Sluggable', ['test' => 'value'])); $this->assertNull($table->addBehavior('Sluggable', ['test' => 'value'])); try { $table->addBehavior('Sluggable', ['thing' => 'thing']); $this->fail('No exception raised'); } catch (\RuntimeException $e) { $this->assertContains('The "Sluggable" alias has already been loaded', $e->getMessage()); } }
/** * Loads the purifier behavior for the given table if not already attached. * * @param \Cake\ORM\Table $table Table object. * @param array Set of fields to sanitize * @return void */ protected function _loadBehavior(Table $table, $fields) { if (!in_array('HtmlPurifier', $table->behaviors()->loaded())) { $table->addBehavior('Burzum/HtmlPurifier.HtmlPurifier', ['fields' => $fields, 'purifierConfig' => $this->param('config')]); } }
/** * startTest * * @retun void * @access public */ public function setUp() { $this->Produtos = TableRegistry::get('CakePtbr.Produtos'); $this->Produtos->addBehavior("CakePtbr.AjusteFloat"); }
/** * setUp(). * * @return void */ public function setUp() { $this->table = TableRegistry::get('Dummy'); $this->table->addBehavior('Eav.Eav'); }
/** * Test adding a behavior to a table. * * @return void */ public function testAddBehavior() { $mock = $this->getMock('Cake\\ORM\\BehaviorRegistry', [], [], '', false); $mock->expects($this->once())->method('load')->with('Sluggable'); $table = new Table(['table' => 'articles', 'behaviors' => $mock]); $table->addBehavior('Sluggable'); }