php - CakePHP 节省约定

标签 php mysql cakephp cakephp-2.3

我在一个模型上建立了一个关系,其中 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
                     )
                )
            );

最佳答案

  1. 我忘记了模型关系中插件名称的前缀。

    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'
         ));
    
  2. 此时我仍然遇到同样的问题,但我最终通过在我的保存方法中使用深度关联解决了这个问题。

    $this->Document->saveAll($data, array('deep' => true));

关于php - CakePHP 节省约定,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29850962/

相关文章:

php - 使用 php 来完成这种类型的工作

Php 不会运行 MySQL 更新查询,但查询在 MySQL 控制台中有效

mysql - 无法让 glassfish 连接到 mysql

php - 使用php上传docx文件并将内容和图像存储到数据库中

MySql 统计某个值至少出现两次的行总数

cakephp - 如何使用 CakePHP 最好地处理这种复杂的 ACL 情况?

php - 解析bbcode的最佳方式

php - htaccess带参数从asp重定向到php

mysql - 如何在cakephp中获取相关表的COUNT值

PHP isset 不适用于复选框