3

我知道这个问题在这里被问了很多次,但我也尽量遵循提供的解决方案。当我学习 cakephp 时,一些解决方案似乎很难在代码中实现。我正在使用 cakephp 2.5。

我想要做的是创建一个附有一个或多个上传的问题报告。以下是我迄今为止实施的一些内容:-

我有以下型号:

  • 候选人
  • 候选人问题报告
  • 候选人问题报告上传

有如下关联:

  • 候选人问题报告有许多候选人问题报告上传

  • 候选人有很多候选人问题报告

  • 候选人问题报告属于候选人

  • 候选人问题报告上传属于候选人问题报告

候选人.php

    <?php

    class Candidate extends AppModel {

        public $name = 'Candidate';
        public $hasMany = array(

            'CandidatesProblemReport' => array(
                'className' => 'CandidatesProblemReport',
                'foreignKey' => 'candidate_id'
            )

        );
    }

候选人问题报告.php

    <?php

    class CandidatesProblemReport extends AppModel {

        public $name = "CandidatesProblemReport";
        public $belongsTo = array(
            'Candidate' => array(
                'className' => 'Candidate'
            )
        );
        public $hasMany = array(
            'Uploads' => array(
                'className' => 'CandidatesProblemReportsUpload'
            ),
            'Replies' => array(
                'className' => 'CandidatesProblemReportsReply'
            )
        );    
    }

CandidatesProblemReportsController.php

    class CandidatesProblemReportsController extends AppController {

        public $name = "CandidatesProblemReports";

        // ############# Report a Problem #############
        // ********************************************
        public function create() {
            $userid = $this->Auth->user('id'); // Grabs the current user id
            $this->set('userId', $userid); // Sends the current user id to the form

            if ($this->request->is('post') && !empty($this->request->data)):

                $this->CandidatesProblemReport->create();

                $report = $this->CandidatesProblemReport->save($this->request->data);
                if (!empty($report)):         
                    $this->request->data['CandidatesProblemReportsUpload']['candidates_problem_report_id'] = $this->CandidatesProblemReport->id;
                endif;

                if ($this->CandidatesProblemReport->saveAssociated($this->request->data)):

                    $this->Session->setFlash('Your report has been submitted '
                            . 'successfully. Thank you!');

                    $this->redirect(array(
                        'action' => 'viewall')
                    );
                else:
                    $this->Session->setFlash('Your report could not be submitted, '
                            . 'please try again');
                endif;

            endif;
        }
    }

创建.ctp

<h1>Create a report</h1>
<?php
echo $this->Form->create('CandidatesProblemReport', array('type' => 'file'));

echo $this->Form->input('CandidatesProblemReport.report_subject');

echo $this->Form->input('CandidatesProblemReport.report_handle_department', array(
    'options' => array(
        'Technical' => 'Technical',
        'Sales' => 'Sales',
        'Support' => 'Support',
        'Other' => 'Other'
    )
));
echo $this->Form->input('CandidatesProblemReport.report_description');

echo $this->Form->input('CandidatesProblemReport.report_date', array(
    'type' => 'hidden',
    'value' => date('Y-m-d H:i:s'))
);

echo $this->Form->input('CandidatesProblemReport.candidate_id', array(
    'type' => 'hidden',
    'value' => $userId)
);
?>

<div>
    <p><strong>Upload Screenshot/Files</strong></p>
    <hr>
</div>
<?php
echo $this->Form->input('CandidatesProblemReportsUpload.0.report_upload', array(
    'type' => 'file'
));
?>
<button class="add-new-upload" type="button">Add more</button>
<?php
echo $this->Form->end('submit');

echo $this->Html->script('jquery-2.1.1.min.js');
?>

<script type="text/javascript">
    var i = 1;
    $('.add-new-upload').click(function () {
        $('.file').append('<input type="file" name="data[CandidatesProblemReportsUpload]['
                + i +
                '][report_upload]" id="CandidatesProblemReportsUpload'
                + i +
                'ReportUpload">');
        i++;
    });
</script>

现在发生的事情是我能够保存主模型数据,即 CandidatesProblemReports,但是当我保存关联数据时,它再次保存主模型,创建第二个重复条目,但上传没有得到保存。

4

2 回答 2

0

使用关联保存数据

这是预期的行为,saveAssociated()不是只保存关联记录,它也会保存主记录,所以你应该saveAssociated()只使用,不需要手动设置外键等,CakePHP 会自动完成。

控制器

public function create() {
    // ...

    if ($this->request->is('post') && !empty($this->request->data)):

        $this->CandidatesProblemReport->create();

        if ($this->CandidatesProblemReport->saveAssociated($this->request->data)):
            // ...
        endif;

    endif;
}

注意你的别名

未创建上传记录的原因是您没有在表单/数据中使用正确的别名,您已将别名设置为Uploads,但在您的表单中您正在使用CandidatesProblemReportsUpload,因此 CakePHP 将忽略此数据。

形式

echo $this->Form->input('Uploads.0.report_upload', array(
    'type' => 'file'
));
<script type="text/javascript">
    var i = 1;
    $('.add-new-upload').click(function () {
        $('.file').append('<input type="file" name="data[Uploads]['
                + i +
                '][report_upload]" id="Uploads'
                + i +
                'ReportUpload">');
        i++;
    });
</script>

存储文件数据

正如评论中提到的,CakePHP 不处理开箱即用的文件上传数据,您必须事先准备好它,以便将其存储在磁盘上,并且模型存储文件的路径。

虽然上面的代码通常应该可以工作,但它很可能会触发错误,因为它会尝试将文件上传数组存储在数据库中而不是平面数据中。

有一些插件可以处理文件上传,检查它们,也可以在 stackoverflow 上搜索并查看文档以获取有关如何在保存数据之前修改数据的示例。

对于初学者:

于 2014-10-30T14:58:38.813 回答
0

您可以使用$this->CandidatesProblemReport->saveAssociated($this->request->data)代码使用一个查询将关联数据保存在多个表中。

于 2019-05-28T12:27:59.600 回答