将数据保存在cakephp中,一次将其保存到多个关联中
我知道这个问题在这里被问了很多次,但我也尽力按照我提供的最佳解决方案。在我学习cakephp时,有些解决方案似乎很难在代码中实现。我使用的是CakePHP2.5 我试图做的是创建一个带有一个或多个上传的问题报告。以下是我迄今为止实施的一些措施:- 我有以下型号:将数据保存在cakephp中,一次将其保存到多个关联中,php,cakephp,model-associations,cakephp-2.5,Php,Cakephp,Model Associations,Cakephp 2.5,我知道这个问题在这里被问了很多次,但我也尽力按照我提供的最佳解决方案。在我学习cakephp时,有些解决方案似乎很难在代码中实现。我使用的是CakePHP2.5 我试图做的是创建一个带有一个或多个上传的问题报告。以下是我迄今为止实施的一些措施:- 我有以下型号: 候选人 候选人问题报告 候选问题报告加载 关联如下: CandidatesProblemReport有许多CandidatesProblemReportsUpload 候选人在报告中有许多问题 候选人问题报告属于候选人 候选问题报
- 候选人
- 候选人问题报告
- 候选问题报告加载
- CandidatesProblemReport有许多CandidatesProblemReportsUpload
- 候选人在报告中有许多问题
- 候选人问题报告属于候选人
- 候选问题报告将以下内容加载到候选问题报告
<?php
class Candidate extends AppModel {
public $name = 'Candidate';
public $hasMany = array(
'CandidatesProblemReport' => array(
'className' => 'CandidatesProblemReport',
'foreignKey' => 'candidate_id'
)
);
}
<?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'
)
);
}
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;
}
}
<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>
创建.ctp
<?php
class Candidate extends AppModel {
public $name = 'Candidate';
public $hasMany = array(
'CandidatesProblemReport' => array(
'className' => 'CandidatesProblemReport',
'foreignKey' => 'candidate_id'
)
);
}
<?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'
)
);
}
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;
}
}
<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>
创建报告
上传屏幕截图/文件
添加更多
var i=1;
$('.add new upload')。单击(函数(){
$('.file').append('');
i++;
});
现在,我可以保存主模型数据,即候选问题报告,但当我保存关联数据时,它会再次保存主模型,创建第二个重复条目,但不会保存上载。保存关联数据
这是预期的行为,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'
));
var i=1;
$('.add new upload')。单击(函数(){
$('.file').append('');
i++;
});
存储文件数据
正如在评论中提到的,CakePHP不会处理开箱即用的文件上传数据,您必须事先准备好它,以便将其存储在磁盘上,并且模型存储文件的路径
虽然上面的代码通常可以工作,但它很可能会触发错误,因为它将尝试在数据库中存储文件上载数组,而不是平面数据
有一些插件可以处理文件上传,检查它们,也可以在这里搜索stackoverflow并检查文档,以获取如何在保存数据之前修改数据的示例
首先:
$this->CandidatesProblemReport->saveAssociated($this->request->data)
代码使用一个查询将关联数据保存在多个表中。除了代码中的明显问题外,您是否知道CakePHP不会自动存储文件上载数据?也就是说,当保存CandidatesProblemReportsUpload
记录时,您预计会发生什么?正如@ndm所说,处理文件上载及其数据库记录不会立即发生。您需要处理这些上传,并以可以保存的方式更改传递给模型的数据数组array('FieldName'=>'字段值(例如文件名)
感谢@ndm的回复,是的,我知道CakePHP不会自动处理文件上传。我只想将数据保存在各自的表中,到目前为止,只有主模型(即CandidatesProblemReport)的数据被保存了两次(两条不同的记录)。@AymanB。在将数据数组传递给模型之前,如何更改它。到目前为止,我正在修改数据数组,方法是在将其保存为关联数据之前,传递之前保存的CandidateProblemReport
的ID
。@ndm忘记文件类型字段也不保存文本类型dataOMG O.O我完全忘记了别名,我确信它现在必须工作,谢谢@ndm,我完成实施后会尽快与您联系。干杯谢谢你,老兄,因为我认为这是第一次尝试,非常感谢你!!您能帮助我如何回复已从CandidatesProblemReports
Controller提交/存在的当前报告吗?目前我正试图通过候选问题报告
表单中的候选问题报告
。@Iglance3这将是一个新问题,您可以在其中添加代码并解释您面临的问题。好的,我解决了。。。我刚刚用$this->MainModel->AssociatedModel->save(data)保存了数据数组代码>