【发布时间】:2015-04-24 15:03:51
【问题描述】:
我在一个模型上设置了一个关系,其中 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
)
)
);
【问题讨论】:
-
您要保存哪些数据?
-
我已经编辑了我的问题,所以你可以看到我的数据格式
-
要进行故障排除,请将 Document hasMany Document_revision 更改为标准关系(即,没有
finderquery)。如果saveall有效,那么您知道它是因为finderquery选项。否则,它是您的数组格式。如果是因为finderquery,我建议您为所有文档关系(可用于保存)设置 1 个 hasMany 关系,为最新版本设置 1 个 hasOne 关系。 -
试过了,不幸的是仍然没有区别。我确信这与更改 Document 模型中的 primaryKey 有关。
-
我将 DocumentVersion 上的 hasMany 更改为字符串而不是数组,并且 SaveAll 上的关联工作正常,但这给我带来了一个新问题,因为我仍然需要返回最新版本的 DocumentVersion 而不是相关文档列表。
标签: php mysql cakephp cakephp-2.3