本文整理汇总了PHP中Cake\ORM\Association::foreignKey方法的典型用法代码示例。如果您正苦于以下问题:PHP Association::foreignKey方法的具体用法?PHP Association::foreignKey怎么用?PHP Association::foreignKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Association
的用法示例。
在下文中一共展示了Association::foreignKey方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
}
}
示例2: 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->source()->alias());
}
return $this->_foreignKey;
}
return parent::foreignKey($key);
}
示例3: 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 $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) {
$key = Inflector::singularize($this->source()->alias());
$this->_foreignKey = Inflector::underscore($key) . '_id';
}
return $this->_foreignKey;
}
return parent::foreignKey($key);
}
示例4: _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;
}
示例5: _oneToManyAssociatedRecords
/**
* Method that retrieves one to many associated records
*
* @param \Cake\ORM\Association $association Association object
* @param \Cake\Network\Request $request passed
* @return array associated records
*/
protected function _oneToManyAssociatedRecords(Association $association, Request $request)
{
$result = [];
$assocName = $association->name();
$assocTableName = $association->table();
$assocForeignKey = $association->foreignKey();
$recordId = $request->params['pass'][0];
$csvFields = $this->_getAssociationCsvFields($association, static::ASSOC_FIELDS_ACTION);
if (empty($csvFields)) {
return $result;
}
// get associated index View csv fields
$fields = array_unique(array_merge([$association->displayField()], $csvFields));
$query = $this->_tableInstance->{$assocName}->find('all', ['conditions' => [$assocForeignKey => $recordId]]);
$records = $query->all();
// store association name
$result['assoc_name'] = $assocName;
// store associated table name
$result['table_name'] = $assocTableName;
// store associated table class name
$result['class_name'] = $association->className();
// store associated table display field
$result['display_field'] = $association->displayField();
// store associated table primary key
$result['primary_key'] = $association->primaryKey();
// store associated table foreign key
$result['foreign_key'] = $association->foreignKey();
// store associated table fields
$result['fields'] = $fields;
// store associated table records
$result['records'] = $records;
return $result;
}