晚安。我正在使用cakephp 3.0,如果可以帮助我解决以下问题,我将非常感激:
我有一个在这个数组中定义的选择列表:
$daysOfWeek = ['Monday' => 'Monday', 'Tuesday' => 'Tuesday', 'Wednesday' => 'Wednesday', 'Thursday' => 'Thursday', 'Friday' => 'Friday', 'Saturday' => 'Saturday', 'Sunday' => 'Sunday'];
在我的 添加 视图中,我有类似的内容,以便选择多天:
echo $this->Form->input('day_of_week', [
'options' => $daysOfWeek,
multiple' => 'checkbox']);
这样做,cakephp总是在我的数据库的 daysOfWeek 字段中保存我的空值,所以我必须编辑模型添加下一个函数:
public function beforeMarshal(CakeEvent $event, ArrayObject $data, ArrayObject $options)
{
$data['day_of_week'] = json_encode($data['day_of_week']);
}
现在,多个选择的值在数据库中保存如下:
["Saturday","Sunday"]
但是如果用户想要编辑此表的某些条目的信息,则永远不会选中 daysOfWeek 的复选框。我该如何解决这个问题?
提前致谢。
答案 0 :(得分:0)
您错过了下拉列表的selected
属性。请使用此代码:
$arr = array("Saturday","Sunday");
$this->Form->input('day_of_week',array(
'type' => 'select',
'multiple' => true,
'options' => $daysOfWeek,
'selected' => $arr));
答案 1 :(得分:0)
感谢您的回复。最后我在控制器中解决了以下问题:
Correctly Classified Instances 416 39.6568 %
Incorrectly Classified Instances 633 60.3432 %
Kappa statistic 0.091
Mean absolute error 0.4371
Root mean squared error 0.4663
Relative absolute error 98.4524 %
Root relative squared error 98.9763 %
Coverage of cases (0.95 level) 100 %
Mean rel. region size (0.95 level) 100 %
Total Number of Instances 1049
=== Detailed Accuracy By Class ===
TP Rate FP Rate Precision Recall F-Measure MCC ROC Area PRC Area Class
0.310 0.231 0.377 0.310 0.340 0.084 0.554 0.448 16-18
0.271 0.167 0.460 0.271 0.341 0.123 0.501 0.359 19+
0.599 0.511 0.382 0.599 0.467 0.084 0.570 0.395 All Age
Weighted Avg. 0.397 0.306 0.407 0.397 0.384 0.098 0.541 0.399
在视图中是同样的事情:
public function edit($id = null)
{
$event = $this->Events->get($id, [
'contain' => ['Categories', 'Playlists']
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$event = $this->Events->patchEntity($event, $this->request->data);
if ($this->Events->save($event)) {
$this->Flash->success(__('The event has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The event could not be saved. Please, try again.'));
}
}
$categories = $this->Events->Categories->find('list', ['limit' => 200]);
$playlists = $this->Events->Playlists->find('list', ['limit' => 200]);
$daysOfWeek = ['Monday' => 'Monday', 'Tuesday' => 'Tuesday', 'Wednesday' => 'Wednesday', 'Thursday' => 'Thursday', 'Friday' => 'Friday', 'Saturday' => 'Saturday', 'Sunday' => 'Sunday'];
$event->day_of_week = json_decode($event->day_of_week);
$this->set(compact('event', 'categories', 'playlists', 'daysOfWeek'));
$this->set('_serialize', ['event']);
}