本文整理汇总了PHP中Cake\ORM\Table::primaryKey方法的典型用法代码示例。如果您正苦于以下问题:PHP Table::primaryKey方法的具体用法?PHP Table::primaryKey怎么用?PHP Table::primaryKey使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Table
的用法示例。
在下文中一共展示了Table::primaryKey方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _subQuery
/**
* Generates a SQL sub-query for replacing in ORDER BY clause.
*
* @param string $column Name of the column being replaced by this sub-query
* @param string|null $bundle Consider attributes only for a specific bundle
* @return string SQL sub-query statement
*/
protected function _subQuery($column, $bundle = null)
{
$alias = $this->_table->alias();
$pk = $this->_table->primaryKey();
$type = $this->_toolbox->getType($column);
$subConditions = ['EavAttribute.table_alias' => $this->_table->table(), 'EavValues.entity_id' => "{$alias}.{$pk}", 'EavAttribute.name' => $column];
if (!empty($bundle)) {
$subConditions['EavAttribute.bundle'] = $bundle;
}
$subQuery = TableRegistry::get('Eav.EavValues')->find()->contain(['EavAttribute'])->select(["EavValues.value_{$type}"])->where($subConditions)->sql();
return str_replace([':c0', ':c1', ':c2', ':c3'], ['"' . $this->_table->table() . '"', "{$alias}.{$pk}", '"' . $column . '"', '"' . $bundle . '"'], $subQuery);
}
示例2: generate
/**
* Load a table, get it's config and then regenerate the thumbnails for that tables upload fields.
*
* @param string $table The name of the table
* @return void
*/
public function generate($table)
{
$this->checkTable($table);
$config = $this->Table->behaviors()->Proffer->config();
foreach ($config as $field => $settings) {
$records = $this->{$this->Table->alias()}->find()->select([$this->Table->primaryKey(), $field, $settings['dir']])->where(["{$field} IS NOT NULL", "{$field} != ''"]);
foreach ($records as $item) {
if ($this->param('verbose')) {
$this->out(__('Processing ' . $this->Table->alias() . ' ' . $item->get($this->Table->primaryKey())));
}
if (!empty($this->param('path-class'))) {
$class = (string) $this->param('path-class');
$path = new $class($this->Table, $item, $field, $settings);
} else {
$path = new ProfferPath($this->Table, $item, $field, $settings);
}
if (!empty($this->param('image-class'))) {
$class = (string) $this->param('image_class');
$transform = new $class($this->Table, $path);
} else {
$transform = new ImageTransform($this->Table, $path);
}
$transform->processThumbnails($settings);
if ($this->param('verbose')) {
$this->out(__('Thumbnails regenerated for ' . $path->fullPath()));
} else {
$this->out(__('Thumbnails regenerated for ' . $this->Table->alias() . ' ' . $item->get($field)));
}
}
}
$this->out($this->nl(0));
$this->out(__('<info>Completed</info>'));
}
示例3: basepath
/**
* Returns the basepath for the current field/data combination.
* If a `path` is specified in settings, then that will be used as
* the replacement pattern
*
* @return string
* @throws LogicException if a replacement is not valid for the current dataset
*/
public function basepath()
{
$defaultPath = 'webroot{DS}files{DS}{model}{DS}{field}{DS}';
$path = Hash::get($this->settings, 'path', $defaultPath);
if (strpos($path, '{primaryKey}') !== false) {
if ($this->entity->isNew()) {
throw new LogicException('{primaryKey} substitution not allowed for new entities');
}
if (is_array($this->table->primaryKey())) {
throw new LogicException('{primaryKey} substitution not valid for composite primary keys');
}
}
$replacements = ['{primaryKey}' => $this->entity->get($this->table->primaryKey()), '{model}' => $this->table->alias(), '{relatedModel}' => $this->entity->model, '{table}' => $this->table->table(), '{field}' => $this->field, '{time}' => time(), '{microtime}' => microtime(), '{DS}' => DIRECTORY_SEPARATOR];
return str_replace(array_keys($replacements), array_values($replacements), $path);
}
示例4: resetSlugs
/**
* 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;
}
示例5: _extractForeignKey
/**
* Returns an array with foreignKey value.
*
* @param \Cake\Datasource\EntityInterface $entity Entity.
* @return array
*/
protected function _extractForeignKey($entity)
{
$foreignKey = (array) $this->_config['foreignKey'];
$primaryKey = (array) $this->_table->primaryKey();
$pkValue = $entity->extract($primaryKey);
return array_combine($foreignKey, $pkValue);
}
示例6: getVirtualColumns
/**
* Gets a list of all virtual columns present in given $query's SELECT clause.
*
* This method will alter the given Query object removing any virtual column
* present in its SELECT clause in order to avoid incorrect SQL statements.
* Selected virtual columns should be fetched after query is executed using
* mapReduce or similar.
*
* @param \Cake\ORM\Query $query The query object to be scoped
* @param string|null $bundle Consider attributes only for a specific bundle
* @return array List of virtual columns names
*/
public function getVirtualColumns(Query $query, $bundle = null)
{
static $selectedVirtual = [];
$cacheKey = md5($query->sql()) . '_' . $bundle;
if (isset($selectedVirtual[$cacheKey])) {
return $selectedVirtual[$cacheKey];
}
$selectClause = (array) $query->clause('select');
if (empty($selectClause)) {
$selectedVirtual[$cacheKey] = array_keys($this->_toolbox->attributes($bundle));
return $selectedVirtual[$cacheKey];
}
$selectedVirtual[$cacheKey] = [];
$virtualColumns = array_keys($this->_toolbox->attributes($bundle));
foreach ($selectClause as $index => $column) {
list($table, $column) = pluginSplit($column);
if ((empty($table) || $table == $this->_table->alias()) && in_array($column, $virtualColumns)) {
$selectedVirtual[$cacheKey][$index] = $column;
unset($selectClause[$index]);
}
}
if (empty($selectClause) && !empty($selectedVirtual[$cacheKey])) {
$selectClause[] = $this->_table->primaryKey();
}
$query->select($selectClause, true);
return $selectedVirtual[$cacheKey];
}
示例7: _getQuery
/**
* Builds a query for loading the passed list of entity objects along with the
* associations specified in $contain.
*
* @param Cake\Collection\CollectionInterface $objects The original entitites
* @param array $contain The associations to be loaded
* @param Cake\ORM\Table $source The table to use for fetching the top level entities
* @return Cake\ORM\Query
*/
protected function _getQuery($objects, $contain, $source)
{
$primaryKey = $source->primaryKey();
$method = is_string($primaryKey) ? 'get' : 'extract';
$keys = $objects->map(function ($entity) use($primaryKey, $method) {
return $entity->{$method}($primaryKey);
});
$query = $source->find()->select((array) $primaryKey)->where(function ($exp, $q) use($primaryKey, $keys, $source) {
if (is_array($primaryKey) && count($primaryKey) === 1) {
$primaryKey = current($primaryKey);
}
if (is_string($primaryKey)) {
return $exp->in($source->aliasField($primaryKey), $keys->toList());
}
$types = array_intersect_key($q->defaultTypes(), array_flip($primaryKey));
$primaryKey = array_map([$source, 'aliasField'], $primaryKey);
return new TupleComparison($primaryKey, $keys->toList(), $types, 'IN');
})->contain($contain);
foreach ($query->eagerLoader()->attachableAssociations($source) as $loadable) {
$config = $loadable->config();
$config['includeFields'] = true;
$loadable->config($config);
}
return $query;
}
示例8: mergeMany
/**
* Merges each of the elements from `$data` into each of the entities in `$entities
* and recursively does the same for each one of the association names passed in
* `$include`. When merging associations, if an entity is not present in the parent
* entity for such association, a new one will be created.
*
* Records in `$data` are matched against the entities by using the primary key
* column. Entries in `$entities` that cannot be matched to any record in
* `$data` will be discarded. Records in `$data` that could not be matched will
* be marshalled as a new entity.
*
* When merging HasMany or BelongsToMany associations, all the entities in the
* `$data` array will appear, those that can be matched by primary key will get
* the data merged, but those that cannot, will be discarded.
*
* @param array|\Traversable $entities the entities that will get the
* data merged in
* @param array $data list of arrays to be merged into the entities
* @param array $include The list of associations to be merged
* @return array
*/
public function mergeMany($entities, array $data, array $include = [])
{
$primary = (array) $this->_table->primaryKey();
$indexed = (new Collection($data))->groupBy($primary[0])->toArray();
$new = isset($indexed[null]) ? [$indexed[null]] : [];
unset($indexed[null]);
$output = [];
foreach ($entities as $entity) {
if (!$entity instanceof EntityInterface) {
continue;
}
$key = $entity->get($primary[0]);
if ($key === null || !isset($indexed[$key])) {
continue;
}
$output[] = $this->merge($entity, $indexed[$key][0], $include);
unset($indexed[$key]);
}
foreach (array_merge($indexed, $new) as $record) {
foreach ($record as $value) {
$output[] = $this->one($value, $include);
}
}
return $output;
}
示例9: beforeSave
/**
* Modifies the entity before it is saved so that versioned fields are persisted
* in the database too.
*
* @param \Cake\Event\Event $event The beforeSave event that was fired
* @param \Cake\ORM\Entity $entity The entity that is going to be saved
* @param \ArrayObject $options the options passed to the save method
* @return void
*/
public function beforeSave(Event $event, Entity $entity, ArrayObject $options)
{
$table = $this->_config['versionTable'];
$newOptions = [$table => ['validate' => false]];
$options['associated'] = $newOptions + $options['associated'];
$fields = $this->_fields();
$values = $entity->extract($fields);
$model = $this->_table->alias();
$primaryKey = (array) $this->_table->primaryKey();
$primaryKey = current($primaryKey);
$foreignKey = $entity->get($primaryKey);
$versionField = $this->_config['versionField'];
$preexistent = TableRegistry::get($table)->find()->select(['version_id'])->where(compact('foreign_key', 'model'))->order(['id desc'])->limit(1)->hydrate(false)->toArray();
$versionId = Hash::get($preexistent, '0.version_id', 0) + 1;
$created = new Time();
foreach ($values as $field => $content) {
if ($field == $primaryKey || $field == $versionField) {
continue;
}
$data = ['version_id' => $versionId, 'model' => $model, 'foreign_key' => $foreignKey, 'field' => $field, 'content' => $content, 'created' => $created];
$new[$field] = new Entity($data, ['useSetters' => false, 'markNew' => true]);
}
$entity->set('__version', $new);
if (!empty($versionField) && in_array($versionField, $fields)) {
$entity->set($this->_config['versionField'], $versionId);
}
}
示例10: _bundleTranslatedFields
/**
* Helper method used to generated multiple translated field entities
* out of the data found in the `_translations` property in the passed
* entity. The result will be put into its `_i18n` property
*
* @param \Cake\Datasource\EntityInterface $entity Entity
* @return void
*/
protected function _bundleTranslatedFields($entity)
{
$translations = (array) $entity->get('_translations');
if (empty($translations) && !$entity->dirty('_translations')) {
return;
}
$fields = $this->_config['fields'];
$primaryKey = (array) $this->_table->primaryKey();
$key = $entity->get(current($primaryKey));
$find = [];
foreach ($translations as $lang => $translation) {
foreach ($fields as $field) {
if (!$translation->dirty($field)) {
continue;
}
$find[] = ['locale' => $lang, 'field' => $field, 'foreign_key' => $key];
$contents[] = new Entity(['content' => $translation->get($field)], ['useSetters' => false]);
}
}
if (empty($find)) {
return;
}
$results = $this->_findExistingTranslations($find);
foreach ($find as $i => $translation) {
if (!empty($results[$i])) {
$contents[$i]->set('id', $results[$i], ['setter' => false]);
$contents[$i]->isNew(false);
} else {
$translation['model'] = $this->_config['referenceName'];
$contents[$i]->set($translation, ['setter' => false, 'guard' => false]);
$contents[$i]->isNew(true);
}
}
$entity->set('_i18n', $contents);
}
示例11: beforeFind
/**
* beforeFind callback
*
* inject where condition if context is 'tenant'
*
* @param \Cake\Event\Event $event The afterSave event that was fired.
* @param \Cake\ORM\Query $query The query.
* @return void
*/
public function beforeFind(Event $event, Query $query, $options)
{
// if context is tenant, add conditions to query
if (MTApp::getContext() == 'tenant') {
// check if find option has "skipTenant", recursive error fix
if (!isset($options['skipTenantCheck']) || $options['skipTenantCheck'] !== true) {
// secure the configured tenant table by adding a primary key condition
if ($this->_table->alias() === MTApp::config('model')['className']) {
$query->where([$this->_table->alias() . '.' . $this->_table->primaryKey() => MTApp::tenant()->id]);
} else {
$query->where([$this->_table->alias() . '.' . $this->config('foreign_key_field') => MTApp::tenant()->id]);
}
}
} else {
throw new DataScopeViolationException('Tenant Scoped accessed globally');
}
return $query;
}
示例12: getEntityId
/**
* Calculates entity's primary key.
*
* If PK is composed of multiple columns they will be merged with `:` symbol.
* For example, consider `Users` table with composed PK <nick, email>, then for
* certain User entity this method could return:
*
* john-locke:john@the-island.com
*
* @param \Cake\Datasource\EntityInterface $entity The entity
* @return string
*/
public function getEntityId(EntityInterface $entity)
{
$pk = [];
$keys = $this->_table->primaryKey();
$keys = !is_array($keys) ? [$keys] : $keys;
foreach ($keys as $key) {
$pk[] = $entity->get($key);
}
return implode(':', $pk);
}
示例13: beforeFind
/**
* Attaches comments to each entity on find operation.
*
* @param \Cake\Event\Event $event The event that was triggered
* @param \Cake\ORM\Query $query The query object
* @param array $options Additional options as an array
* @param bool $primary Whether is find is a primary query or not
* @return void
*/
public function beforeFind(Event $event, $query, $options, $primary)
{
if ($this->_enabled && $query->count() > 0) {
$pk = $this->_table->primaryKey();
$tableAlias = Inflector::underscore($this->_table->alias());
$query->contain(['Comments' => function ($query) {
return $query->find('threaded')->contain(['Users'])->order($this->config('order'));
}]);
if ($this->config('count') || isset($options['comments_count']) && $options['comments_count'] === true) {
$query->formatResults(function ($results) use($pk, $tableAlias) {
return $results->map(function ($entity) use($pk, $tableAlias) {
$entityId = $entity->{$pk};
$count = TableRegistry::get('Comment.Comments')->find()->where(['entity_id' => $entityId, 'table_alias' => $tableAlias])->count();
$entity->set('comments_count', $count);
return $entity;
});
});
}
}
}
示例14: _getUserEntity
/**
* Gets or constructs an user entity with a given id.
*
* @paramx mixed array|int|string $userId
* @return \Cake\Datasource\EntityInterface
*/
protected function _getUserEntity($userId)
{
if (is_a($userId, 'Cake\\Datasource\\EntityInterface')) {
return $userId;
}
if (is_string($userId) || is_integer($userId)) {
return $this->UserTable->newEntity([$this->UserTable->primaryKey() => $userId]);
}
if (is_array($userId)) {
return $this->UserTable->newEntity($userId);
}
}
示例15: _tablePrimaryKey
/**
* Gets table's PK as an array.
*
* @return array
*/
protected function _tablePrimaryKey()
{
$alias = $this->_table->alias();
$pk = $this->_table->primaryKey();
if (!is_array($pk)) {
$pk = [$pk];
}
$pk = array_map(function ($key) use($alias) {
return "{$alias}.{$key}";
}, $pk);
return $pk;
}