本文整理汇总了PHP中Cake\ORM\Association::primaryKey方法的典型用法代码示例。如果您正苦于以下问题:PHP Association::primaryKey方法的具体用法?PHP Association::primaryKey怎么用?PHP Association::primaryKey使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Cake\ORM\Association
的用法示例。
在下文中一共展示了Association::primaryKey方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _manyToOneAssociatedRecords
/**
* Method that retrieves many to one associated records
* @param \Cake\ORM\Table $table Table object
* @param \Cake\ORM\Association $association Association object
* @return array associated records
*/
protected function _manyToOneAssociatedRecords(\Cake\ORM\Table $table, \Cake\ORM\Association $association)
{
$tableName = $table->table();
$primaryKey = $table->primaryKey();
$assocTableName = $association->table();
$assocPrimaryKey = $association->primaryKey();
$assocForeignKey = $association->foreignKey();
$recordId = $this->request->params['pass'][0];
$displayField = $association->displayField();
$connection = ConnectionManager::get('default');
$records = $connection->execute('SELECT ' . $assocTableName . '.' . $displayField . ' FROM ' . $tableName . ' LEFT JOIN ' . $assocTableName . ' ON ' . $tableName . '.' . $assocForeignKey . ' = ' . $assocTableName . '.' . $assocPrimaryKey . ' WHERE ' . $tableName . '.' . $primaryKey . ' = :id LIMIT 1', ['id' => $recordId])->fetchAll('assoc');
// store associated table records
$result = $records[0][$displayField];
return $result;
}
示例2: _manyToOneAssociatedRecords
/**
* Method that retrieves many to one associated records.
*
* @param \Cake\ORM\Association $association Association object
* @param \Cake\Network\Request $request passed
* @return array associated records
*/
protected function _manyToOneAssociatedRecords(Association $association, Request $request)
{
$result = [];
$tableName = $this->_tableInstance->table();
$primaryKey = $this->_tableInstance->primaryKey();
$assocTableName = $association->table();
$assocPrimaryKey = $association->primaryKey();
$assocForeignKey = $association->foreignKey();
$recordId = $request->params['pass'][0];
$displayField = $association->displayField();
/*
* skip inverse relationship
*
* @todo find better way to handle it
*/
if ($tableName === $assocTableName) {
return $result;
}
$connection = ConnectionManager::get('default');
// NOTE: This will break if $assocTableName has no primary key or has a combined primary key
$records = $connection->execute('SELECT ' . $assocTableName . '.' . $assocPrimaryKey . ' FROM ' . $tableName . ' LEFT JOIN ' . $assocTableName . ' ON ' . $tableName . '.' . $assocForeignKey . ' = ' . $assocTableName . '.' . $assocPrimaryKey . ' WHERE ' . $tableName . '.' . $primaryKey . ' = :id LIMIT 1', ['id' => $recordId])->fetchAll('assoc');
// store associated table records, make sure associated record still exists.
if (!empty($records[0][$assocPrimaryKey]) && $association->exists([$assocPrimaryKey => $records[0][$assocPrimaryKey]])) {
//$result = $association->get($records[0][$assocPrimaryKey])->{$displayField};
$records = $association->get($records[0][$assocPrimaryKey]);
} else {
$records = null;
}
try {
$csvFields = $this->_getAssociationCsvFields($association, static::ASSOC_FIELDS_ACTION);
} catch (\Exception $e) {
$csvFields = [];
}
// get associated index View csv fields
$fields = array_unique(array_merge([$association->displayField()], $csvFields));
// store association name
$result['assoc_name'] = $association->name();
// store associated table name
$result['table_name'] = $association->table();
// 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'] = Inflector::singularize($assocTableName) . '_' . $association->primaryKey();
// store associated table fields
$result['fields'] = $fields;
// store associated table records
$result['records'] = $records;
return $result;
}