Returns the behavior registry for this table.
public behaviors ( ) : Cake\ORM\BehaviorRegistry | ||
return | Cake\ORM\BehaviorRegistry | The BehaviorRegistry instance. |
public function testStiTable() { $this->table->behaviors()->get('StiParent')->config('tableMap', ['Readers' => 'reader_*'], false); $this->table->behaviors()->get('StiParent')->config('discriminatorMap', ['*author' => TableRegistry::get('Authors')], false); $map = ['reader_1' => 'Readers', 'reader_2' => 'Readers', 'super_author' => 'Authors', 'bestselling-author' => 'Authors', 'other' => 'Users', '' => 'Users']; foreach ($map as $discriminator => $alias) { $entity = $this->table->newEntity(['discriminator' => $discriminator]); $table = $this->table->stiTable($entity); $this->assertEquals($alias, $table->alias()); } }
/** * Do some checks on the table which has been passed to make sure that it has what we need * * @param string $table The table * @return void */ protected function checkTable($table) { try { $this->Table = $this->loadModel($table); } catch (Exception $e) { $this->out(__('<error>' . $e->getMessage() . '</error>')); exit; } if (get_class($this->Table) === 'AppModel') { $this->out(__('<error>The table could not be found, instance of AppModel loaded.</error>')); exit; } if (!$this->Table->hasBehavior('Proffer')) { $out = __("<error>The table '" . $this->Table->alias() . "' does not have the Proffer behavior attached.</error>"); $this->out($out); exit; } $config = $this->Table->behaviors()->Proffer->config(); foreach ($config as $field => $settings) { if (!$this->Table->hasField($field)) { $out = __("<error>The table '" . $this->Table->alias() . "' does not have the configured upload field in it's schema.</error>"); $this->out($out); exit; } if (!$this->Table->hasField($settings['dir'])) { $out = __("<error>The table '" . $this->Table->alias() . "' does not have the configured dir field in it's schema.</error>"); $this->out($out); exit; } } }
/** * ResetSlugs method. * * Regenerate all slugs. On large dbs this can take more than 30 seconds - a time * limit is set to allow a minimum 100 updates per second as a preventative measure. * * Note that you should use the Reset behavior if you need additional functionality such * as callbacks or timeouts. * * @param array $params * @return bool Success */ public function resetSlugs($params = []) { if (!$this->_table->hasField($this->_config['field'])) { throw new Exception('Table does not have field ' . $this->_config['field']); } $defaults = ['page' => 1, 'limit' => 100, 'fields' => array_merge([$this->_table->primaryKey()], $this->_config['label']), 'order' => $this->_table->displayField() . ' ASC', 'conditions' => $this->_config['scope'], 'overwrite' => true]; $params = array_merge($defaults, $params); $count = $this->_table->find('all', compact('conditions'))->count(); $max = ini_get('max_execution_time'); if ($max) { set_time_limit(max($max, $count / 100)); } $this->_table->behaviors()->Slugged->config($params, null, false); while ($records = $this->_table->find('all', $params)->toArray()) { foreach ($records as $record) { $record->isNew(true); $options = ['validate' => true, 'fieldList' => array_merge([$this->_table->primaryKey(), $this->_config['field']], $this->_config['label'])]; if (!$this->_table->save($record, $options)) { throw new Exception(print_r($this->_table->errors(), true)); } } $params['page']++; } return true; }
/** * Build the map of property => marshalling callable. * * @param array $data The data being marshalled. * @param array $options List of options containing the 'associated' key. * @throws \InvalidArgumentException When associations do not exist. * @return array */ protected function _buildPropertyMap($data, $options) { $map = []; $schema = $this->_table->schema(); // Is a concrete column? foreach (array_keys($data) as $prop) { $columnType = $schema->columnType($prop); if ($columnType) { $map[$prop] = function ($value, $entity) use($columnType) { return Type::build($columnType)->marshal($value); }; } } // Map associations if (!isset($options['associated'])) { $options['associated'] = []; } $include = $this->_normalizeAssociations($options['associated']); foreach ($include as $key => $nested) { if (is_int($key) && is_scalar($nested)) { $key = $nested; $nested = []; } $assoc = $this->_table->association($key); // If the key is not a special field like _ids or _joinData // it is a missing association that we should error on. if (!$assoc) { if (substr($key, 0, 1) !== '_') { throw new \InvalidArgumentException(sprintf('Cannot marshal data for "%s" association. It is not associated with "%s".', $key, $this->_table->alias())); } continue; } if (isset($options['forceNew'])) { $nested['forceNew'] = $options['forceNew']; } if (isset($options['isMerge'])) { $callback = function ($value, $entity) use($assoc, $nested) { $options = $nested + ['associated' => []]; return $this->_mergeAssociation($entity->get($assoc->property()), $assoc, $value, $options); }; } else { $callback = function ($value, $entity) use($assoc, $nested) { $options = $nested + ['associated' => []]; return $this->_marshalAssociation($assoc, $value, $options); }; } $map[$assoc->property()] = $callback; } $behaviors = $this->_table->behaviors(); foreach ($behaviors->loaded() as $name) { $behavior = $behaviors->get($name); if ($behavior instanceof PropertyMarshalInterface) { $map += $behavior->buildMarshalMap($this, $map, $options); } } return $map; }
/** * Process action. * * @param Table $table * @param $action * @param array $ids * @param array $options * @return \Cake\Network\Response|void */ public function process(Table $table, $action, array $ids = [], array $options = []) { $idsCount = count($ids); $options = Hash::merge($this->_config, $options); $_options = ['messages' => $this->__setupMessages($idsCount)]; $options = Hash::merge($_options, $options); $redirect = $options['redirect']; $messages = $options['messages']; if (!$action) { $this->Flash->error($messages['noAction']); return $this->_controller->redirect($redirect); } if ($idsCount == 0) { $this->Flash->error($messages['noChose']); return $this->_controller->redirect($redirect); } $behaviors = $table->behaviors(); if (!in_array('BulkProcess', $behaviors->loaded())) { $behaviors->load('Union/Core.BulkProcess'); } $event = Event::dispatch($this->_getEventName($action, 'bulkBefore'), $this->_controller, ['ids' => $ids]); if (is_array($event->result)) { $ids = $event->result; if (count($ids) == 0) { $this->Flash->error($messages['noChose']); return $this->_controller->redirect($redirect); } $messages = $this->__setupMessages(count($ids)); } $process = $table->processAction($action, $ids); if ($process) { if (!empty($messages[$action])) { $message = $messages[$action]; } else { $message = __d('union', '{0} processed', Inflector::humanize($this->_controller->name)); } Event::dispatch($this->_getEventName($action), $this->_controller, ['ids' => $ids]); $this->Flash->success($message); return $this->_controller->redirect($redirect); } $this->Flash->error(__d('union', 'An error occurred')); return $this->_controller->redirect($redirect); }
/** * 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')]); } }