我在一个模型上建立了一个关系,其中 primaryKey 设置为“Document.guid”,同时我还有一个关联的模型 DocumentVersion。
class Document extends DocumentAppModel {
public $hasMany = array(
'DocumentVersion' => array(
'className' => 'DocumentVersion',
'foreignKey' => false,
'finderQuery' => 'SELECT * FROM document_versions as `DocumentVersion` WHERE `DocumentVersion`.`document_guid` = {$__cakeID__$} ORDER BY `DocumentVersion`.`version` DESC LIMIT 1'
)
);
class DocumentVersion extends DocumentAppModel {
public $belongsTo = array(
'Document'
);
}
当我尝试使用 $this->Document->saveAll() 时,它只保存文档数据而不保存相关数据。根据我的理解,这与我没有使用 cakephp 约定来建立关联这一事实有关。
我的数据结构
$filtered[] = array(
'Document' => array(
'guid' => $upload['guid'],
'database_revision' => Configure::read('Settings.LocalDataBaseRevision')
),
'DocumentVersion' => array(
array(
//'guid' => $upload['guid'],
'parent_guid' => (isset($upload['parent_guid'])) ? $upload['parent_guid'] : null,
'document_type_id' => $upload['type'],
'owner' => $upload['owner_id'],
'editor_id' => (isset($uplaod['editor_id'])) ? $uplaod['editor_id'] : null,
'title' => $upload['title'],
'crc' => (isset($upload['crc'])) ? $upload['crc'] : null,
'payload' => (isset($upload['payload'])) ? $upload['payload'] : null,
'database_revision' => Configure::read('Settings.LocalDataBaseRevision'),
'version' => 1 // Always set version number to 1 on uploads
)
)
);
最佳答案
我忘记了模型关系中插件名称的前缀。
public $hasMany = array( 'Document.DocumentVersion' => array( 'className' => 'DocumentVersion', 'foreignKey' => false, 'finderQuery' => 'SELECT * FROM document_versions as `DocumentVersion` WHERE `DocumentVersion`.`document_guid` = {$__cakeID__$} ORDER BY `DocumentVersion`.`version` DESC LIMIT 1' ));
此时我仍然遇到同样的问题,但我最终通过在我的保存方法中使用深度关联解决了这个问题。
$this->Document->saveAll($data, array('deep' => true));
关于php - CakePHP 节省约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29850962/