• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PHP ORM\Association类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了PHP中Cake\ORM\Association的典型用法代码示例。如果您正苦于以下问题:PHP Association类的具体用法?PHP Association怎么用?PHP Association使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Association类的18个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的PHP代码示例。

示例1: _processAssociation

 /**
  * Updates counter cache for a single association
  *
  * @param \Cake\Event\Event $event Event instance.
  * @param \Cake\ORM\Entity $entity Entity
  * @param Association $assoc The association object
  * @param array $settings The settings for for counter cache for this association
  * @return void
  */
 protected function _processAssociation(Event $event, Entity $entity, Association $assoc, array $settings)
 {
     $foreignKeys = (array) $assoc->foreignKey();
     $primaryKeys = (array) $assoc->target()->primaryKey();
     $countConditions = $entity->extract($foreignKeys);
     $updateConditions = array_combine($primaryKeys, $countConditions);
     $countOriginalConditions = $entity->extractOriginal($foreignKeys);
     if ($countOriginalConditions !== []) {
         $updateOriginalConditions = array_combine($primaryKeys, $countOriginalConditions);
     }
     foreach ($settings as $field => $config) {
         if (is_int($field)) {
             $field = $config;
             $config = [];
         }
         if (!is_string($config) && is_callable($config)) {
             $count = $config($event, $entity, $this->_table, false);
         } else {
             $count = $this->_getCount($config, $countConditions);
         }
         $assoc->target()->updateAll([$field => $count], $updateConditions);
         if (isset($updateOriginalConditions)) {
             if (!is_string($config) && is_callable($config)) {
                 $count = $config($event, $entity, $this->_table, true);
             } else {
                 $count = $this->_getCount($config, $countOriginalConditions);
             }
             $assoc->target()->updateAll([$field => $count], $updateOriginalConditions);
         }
     }
 }
开发者ID:ansidev,项目名称:cakephp_blog,代码行数:40,代码来源:CounterCacheBehavior.php


示例2: associatedEntityTypeToHintType

 /**
  * Converts an entity class type to its DocBlock hint type counterpart.
  *
  * @param string $type The entity class type (a fully qualified class name).
  * @param \Cake\ORM\Association $association The association related to the entity class.
  * @return string The DocBlock type
  */
 public function associatedEntityTypeToHintType($type, Association $association)
 {
     if ($association->type() === Association::MANY_TO_MANY || $association->type() === Association::ONE_TO_MANY) {
         return $type . '[]';
     }
     return $type;
 }
开发者ID:AmuseXperience,项目名称:api,代码行数:14,代码来源:DocBlockHelper.php


示例3: finder

 /**
  * Get finder to use for provided association.
  *
  * @param \Cake\ORM\Association $association Association instance
  * @return string
  */
 public function finder(Association $association)
 {
     if ($association->target()->behaviors()->has('Tree')) {
         return 'treeList';
     }
     return 'list';
 }
开发者ID:hareshpatel1990,项目名称:cakephp3restapi,代码行数:13,代码来源:RelatedModelsListener.php


示例4: _deleteFileAssociationRecord

 /**
  * Method that fetches and deletes document-file manyToMany association record Entity.
  *
  * @param  string $id file id
  * @return bool
  */
 protected function _deleteFileAssociationRecord($id)
 {
     $query = $this->_fileStorageAssociation->find('all', ['conditions' => [$this->_fileStorageForeignKey => $id]]);
     $entity = $query->first();
     if (is_null($entity)) {
         return false;
     }
     return $this->_fileStorageAssociation->delete($entity);
 }
开发者ID:QoboLtd,项目名称:cakephp-csv-migrations,代码行数:15,代码来源:FileUploadsUtils.php


示例5: _belongsToMany

 /**
  * Marshals data for belongsToMany associations.
  *
  * Builds the related entities and handles the special casing
  * for junction table entities.
  *
  * @param \Cake\ORM\Association $assoc The association to marshal.
  * @param array $data The data to convert into entities.
  * @param array $options List of options.
  * @return array An array of built entities.
  */
 protected function _belongsToMany(Association $assoc, array $data, $options = [])
 {
     $associated = isset($options['associated']) ? $options['associated'] : [];
     $forceNew = isset($options['forceNew']) ? $options['forceNew'] : false;
     $data = array_values($data);
     $target = $assoc->target();
     $primaryKey = array_flip((array) $target->primaryKey());
     $records = $conditions = [];
     $primaryCount = count($primaryKey);
     $conditions = [];
     foreach ($data as $i => $row) {
         if (!is_array($row)) {
             continue;
         }
         if (array_intersect_key($primaryKey, $row) === $primaryKey) {
             $keys = array_intersect_key($row, $primaryKey);
             if (count($keys) === $primaryCount) {
                 $rowConditions = [];
                 foreach ($keys as $key => $value) {
                     $rowConditions[][$target->aliasfield($key)] = $value;
                 }
                 if ($forceNew && !$target->exists($rowConditions)) {
                     $records[$i] = $this->one($row, $options);
                 }
                 $conditions = array_merge($conditions, $rowConditions);
             }
         } else {
             $records[$i] = $this->one($row, $options);
         }
     }
     if (!empty($conditions)) {
         $query = $target->find();
         $query->andWhere(function ($exp) use($conditions) {
             return $exp->or_($conditions);
         });
         $keyFields = array_keys($primaryKey);
         $existing = [];
         foreach ($query as $row) {
             $k = implode(';', $row->extract($keyFields));
             $existing[$k] = $row;
         }
         foreach ($data as $i => $row) {
             $key = [];
             foreach ($keyFields as $k) {
                 if (isset($row[$k])) {
                     $key[] = $row[$k];
                 }
             }
             $key = implode(';', $key);
             // Update existing record and child associations
             if (isset($existing[$key])) {
                 $records[$i] = $this->merge($existing[$key], $data[$i], $options);
             }
         }
     }
     $jointMarshaller = $assoc->junction()->marshaller();
     $nested = [];
     if (isset($associated['_joinData'])) {
         $nested = (array) $associated['_joinData'];
     }
     foreach ($records as $i => $record) {
         // Update junction table data in _joinData.
         if (isset($data[$i]['_joinData'])) {
             $joinData = $jointMarshaller->one($data[$i]['_joinData'], $nested);
             $record->set('_joinData', $joinData);
         }
     }
     return $records;
 }
开发者ID:rlugojr,项目名称:cakephp,代码行数:80,代码来源:Marshaller.php


示例6: _mergeJoinData

 /**
  * Merge the special _joinData property into the entity set.
  *
  * @param \Cake\Datasource\EntityInterface $original The original entity
  * @param \Cake\ORM\Association $assoc The association to marshall
  * @param array $value The data to hydrate
  * @param array $options List of options.
  * @return array An array of entities
  */
 protected function _mergeJoinData($original, $assoc, $value, $options)
 {
     $associated = isset($options['associated']) ? $options['associated'] : [];
     $extra = [];
     foreach ($original as $entity) {
         // Mark joinData as accessible so we can marshal it properly.
         $entity->accessible('_joinData', true);
         $joinData = $entity->get('_joinData');
         if ($joinData && $joinData instanceof EntityInterface) {
             $extra[spl_object_hash($entity)] = $joinData;
         }
     }
     $joint = $assoc->junction();
     $marshaller = $joint->marshaller();
     $nested = [];
     if (isset($associated['_joinData'])) {
         $nested = (array) $associated['_joinData'];
     }
     $records = $this->mergeMany($original, $value, $options);
     foreach ($records as $record) {
         $hash = spl_object_hash($record);
         $value = $record->get('_joinData');
         if (!is_array($value)) {
             $record->unsetProperty('_joinData');
             continue;
         }
         if (isset($extra[$hash])) {
             $record->set('_joinData', $marshaller->merge($extra[$hash], $value, $nested));
         } else {
             $joinData = $marshaller->one($value, $nested);
             $record->set('_joinData', $joinData);
         }
     }
     return $records;
 }
开发者ID:sleeping-lion,项目名称:slboard_cakephp3,代码行数:44,代码来源:Marshaller.php


示例7: _loadAssociatedByIds

 /**
  * Loads a list of belongs to many from ids.
  *
  * @param Association $assoc The association class for the belongsToMany association.
  * @param array $ids The list of ids to load.
  * @return array An array of entities.
  */
 protected function _loadAssociatedByIds($assoc, $ids)
 {
     if (empty($ids)) {
         return [];
     }
     $target = $assoc->target();
     $primaryKey = (array) $target->primaryKey();
     $multi = count($primaryKey) > 1;
     $primaryKey = array_map(function ($key) use($target) {
         return $target->alias() . '.' . $key;
     }, $primaryKey);
     if ($multi) {
         if (count(current($ids)) !== count($primaryKey)) {
             return [];
         }
         $filter = new TupleComparison($primaryKey, $ids, [], 'IN');
     } else {
         $filter = [$primaryKey[0] . ' IN' => $ids];
     }
     return $target->find()->where($filter)->toArray();
 }
开发者ID:rederlo,项目名称:cakephp,代码行数:28,代码来源:Marshaller.php


示例8: addToJoinsMap

 /**
  * Registers a table alias, typically loaded as a join in a query, as belonging to
  * an association. This helps hydrators know what to do with the columns coming
  * from such joined table.
  *
  * @param string $alias The table alias as it appears in the query.
  * @param \Cake\ORM\Association $assoc The association object the alias represents;
  * will be normalized
  * @param bool $asMatching Whether or not this join results should be treated as a
  * 'matching' association.
  * @param string $targetProperty The property name where the results of the join should be nested at.
  * If not passed, the default property for the association will be used.
  * @return void
  */
 public function addToJoinsMap($alias, Association $assoc, $asMatching = false, $targetProperty = null)
 {
     $this->_joinsMap[$alias] = new EagerLoadable($alias, ['aliasPath' => $alias, 'instance' => $assoc, 'canBeJoined' => true, 'forMatching' => $asMatching, 'targetProperty' => $targetProperty ?: $assoc->property()]);
 }
开发者ID:nrother,项目名称:cakephp,代码行数:18,代码来源:EagerLoader.php


示例9: _oneToManyAssociatedRecords

 /**
  * Method that retrieves one to many associated records
  * @param  \Cake\ORM\Table       $table       Table object
  * @param  \Cake\ORM\Association $association Association object
  * @return array                              associated records
  */
 protected function _oneToManyAssociatedRecords(\Cake\ORM\Table $table, \Cake\ORM\Association $association)
 {
     $assocName = $association->name();
     $assocTableName = $association->table();
     $assocForeignKey = $association->foreignKey();
     $recordId = $this->request->params['pass'][0];
     // get associated index View csv fields
     $fields = $this->_getTableFields($association);
     $query = $table->{$assocName}->find('all', ['conditions' => [$assocForeignKey => $recordId], 'fields' => $fields]);
     $records = $query->all();
     // store associated table records
     $result['records'] = $records;
     // store associated table fields
     $result['fields'] = $fields;
     // store associated table name
     $result['table_name'] = $assocTableName;
     return $result;
 }
开发者ID:QoboLtd,项目名称:cakephp-csv-views,代码行数:24,代码来源:CsvViewComponent.php


示例10: _loadBelongsToMany

 /**
  * Loads a list of belongs to many from ids.
  *
  * @param Association $assoc The association class for the belongsToMany association.
  * @param array $ids The list of ids to load.
  * @return array An array of entities.
  */
 protected function _loadBelongsToMany($assoc, $ids)
 {
     $target = $assoc->target();
     $primaryKey = (array) $target->primaryKey();
     $multi = count($primaryKey) > 1;
     if ($multi) {
         if (count(current($ids)) !== count($primaryKey)) {
             return [];
         }
         $filter = new TupleComparison($primaryKey, $ids, [], 'IN');
     } else {
         $filter = [$primaryKey[0] . ' IN' => $ids];
     }
     return $assoc->find()->where($filter)->toArray();
 }
开发者ID:ripzappa0924,项目名称:carte0.0.1,代码行数:22,代码来源:Marshaller.php


示例11: _mergeJoinData

 /**
  * Merge the special _joinData property into the entity set.
  *
  * @param \Cake\Datasource\EntityInterface $original The original entity
  * @param \Cake\ORM\Association $assoc The association to marshall
  * @param array $value The data to hydrate
  * @param array $options List of options.
  * @return array An array of entities
  */
 protected function _mergeJoinData($original, $assoc, $value, $options)
 {
     $associated = isset($options['associated']) ? $options['associated'] : [];
     $extra = [];
     foreach ($original as $entity) {
         // Mark joinData as accessible so we can marshal it properly.
         $entity->accessible('_joinData', true);
         $joinData = $entity->get('_joinData');
         if ($joinData && $joinData instanceof EntityInterface) {
             $extra[spl_object_hash($entity)] = $joinData;
         }
     }
     $joint = $assoc->junction();
     $marshaller = $joint->marshaller();
     $nested = [];
     if (isset($associated['_joinData'])) {
         $nested = (array) $associated['_joinData'];
     }
     $options['accessibleFields'] = ['_joinData' => true];
     $records = $this->mergeMany($original, $value, $options);
     foreach ($records as $record) {
         $hash = spl_object_hash($record);
         $value = $record->get('_joinData');
         // Already an entity, no further marshalling required.
         if ($value instanceof EntityInterface) {
             continue;
         }
         // Scalar data can't be handled
         if (!is_array($value)) {
             $record->unsetProperty('_joinData');
             continue;
         }
         // Marshal data into the old object, or make a new joinData object.
         if (isset($extra[$hash])) {
             $record->set('_joinData', $marshaller->merge($extra[$hash], $value, $nested));
         } elseif (is_array($value)) {
             $joinData = $marshaller->one($value, $nested);
             $record->set('_joinData', $joinData);
         }
     }
     return $records;
 }
开发者ID:rlugojr,项目名称:cakephp,代码行数:51,代码来源:Marshaller.php


示例12: _save

 /**
  * Helper method for saving an association's data.
  *
  * @param Association $association The association object to save with.
  * @param Entity $entity The entity to save
  * @param array $nested Options for deeper associations
  * @param array $options Original options
  * @return bool Success
  */
 protected function _save($association, $entity, $nested, $options)
 {
     if (!$entity->dirty($association->property())) {
         return true;
     }
     if (!empty($nested)) {
         $options = (array) $nested + $options;
     }
     return (bool) $association->save($entity, $options);
 }
开发者ID:ripzappa0924,项目名称:carte0.0.1,代码行数:19,代码来源:Associations.php


示例13: _getAssociationCsvFields

 /**
  * Get association CSV fields
  * @param Cake\ORM\Associations $association ORM association
  * @param object $action action passed
  * @return array
  */
 protected function _getAssociationCsvFields(Association $association, $action)
 {
     list($plugin, $controller) = pluginSplit($association->className());
     $fields = $this->_getCsvFields($controller, $action);
     return $fields;
 }
开发者ID:QoboLtd,项目名称:cakephp-csv-migrations,代码行数:12,代码来源:ViewViewTabsListener.php


示例14: foreignKey

 /**
  * Sets the name of the field representing the foreign key to the target table.
  * If no parameters are passed current field is returned
  *
  * @param string|null $key the key to be used to link both tables together
  * @return string
  */
 public function foreignKey($key = null)
 {
     if ($key === null) {
         if ($this->_foreignKey === null) {
             $this->_foreignKey = $this->_modelKey($this->target()->alias());
         }
         return $this->_foreignKey;
     }
     return parent::foreignKey($key);
 }
开发者ID:CakeDC,项目名称:cakephp,代码行数:17,代码来源:BelongsTo.php


示例15: property

 /**
  * Sets the property name that should be filled with data from the target table
  * in the source table record.
  * If no arguments are passed, currently configured type is returned.
  *
  * @param string|null $name The name of the property. Pass null to read the current value.
  * @return string
  */
 public function property($name = null)
 {
     if ($name !== null) {
         return parent::property($name);
     }
     if ($name === null && !$this->_propertyName) {
         list(, $name) = pluginSplit($this->_name);
         $this->_propertyName = Inflector::underscore(Inflector::singularize($name));
     }
     return $this->_propertyName;
 }
开发者ID:wepbunny,项目名称:cake2,代码行数:19,代码来源:HasOne.php


示例16: find

 /**
  * Proxies the finding operation to the target table's find method
  * and modifies the query accordingly based of this association
  * configuration.
  *
  * If your association includes conditions, the junction table will be
  * included in the query's contained associations.
  *
  * @param string|array $type the type of query to perform, if an array is passed,
  *   it will be interpreted as the `$options` parameter
  * @param array $options The options to for the find
  * @see \Cake\ORM\Table::find()
  * @return \Cake\ORM\Query
  */
 public function find($type = null, array $options = [])
 {
     $query = parent::find($type, $options);
     if (!$this->conditions()) {
         return $query;
     }
     $belongsTo = $this->junction()->association($this->target()->alias());
     $conditions = $belongsTo->_joinCondition(['foreignKey' => $this->foreignKey()]);
     return $this->_appendJunctionJoin($query, $conditions);
 }
开发者ID:Malek-S,项目名称:cakephp,代码行数:24,代码来源:BelongsToMany.php


示例17: attachTo

 /**
  * Alters a Query object to include the associated target table data in the final
  * result
  *
  * The options array accept the following keys:
  *
  * - includeFields: Whether to include target model fields in the result or not
  * - foreignKey: The name of the field to use as foreign key, if false none
  *   will be used
  * - conditions: array with a list of conditions to filter the join with
  * - fields: a list of fields in the target table to include in the result
  * - type: The type of join to be used (e.g. INNER)
  *
  * @param \Cake\ORM\Query $query the query to be altered to include the target table data
  * @param array $options Any extra options or overrides to be taken in account
  * @return void
  */
 public function attachTo(Query $query, array $options = [])
 {
     if (!empty($options['negateMatch'])) {
         $this->_appendNotMatching($query, $options);
         return;
     }
     $junction = $this->junction();
     $belongsTo = $junction->association($this->source()->alias());
     $cond = $belongsTo->_joinCondition(['foreignKey' => $belongsTo->foreignKey()]);
     $cond += $this->junctionConditions();
     if (isset($options['includeFields'])) {
         $includeFields = $options['includeFields'];
     }
     // Attach the junction table as well we need it to populate _joinData.
     $assoc = $this->_targetTable->association($junction->alias());
     $newOptions = array_intersect_key($options, ['joinType' => 1, 'fields' => 1]);
     $newOptions += ['conditions' => $cond, 'includeFields' => $includeFields, 'foreignKey' => false];
     $assoc->attachTo($query, $newOptions);
     $query->eagerLoader()->addToJoinsMap($junction->alias(), $assoc, true);
     parent::attachTo($query, $options);
     $foreignKey = $this->targetForeignKey();
     $thisJoin = $query->clause('join')[$this->name()];
     $thisJoin['conditions']->add($assoc->_joinCondition(['foreignKey' => $foreignKey]));
 }
开发者ID:ramb0Ram,项目名称:cakephp,代码行数:41,代码来源:BelongsToMany.php


示例18: transformRow

 /**
  * {@inheritDoc}
  */
 public function transformRow($row, $nestKey, $joined)
 {
     $alias = $this->junction()->alias();
     if ($joined) {
         $row[$this->target()->alias()][$this->_junctionProperty] = $row[$alias];
         unset($row[$alias]);
     }
     return parent::transformRow($row, $nestKey, $joined);
 }
开发者ID:Mingyangzu,项目名称:PHP-cakephp,代码行数:12,代码来源:BelongsToMany.php



注:本文中的Cake\ORM\Association类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PHP ORM\Behavior类代码示例发布时间:2022-05-23
下一篇:
PHP Email\Email类代码示例发布时间:2022-05-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap