所以我正在创建一个表单生成器。用户将登录,然后填写管理员创建的表单。我在“data_controller”“add”方法中使用saveAll()。
这很好用,看起来像这样:
//debug($this->data); prints the following
//app/controllers/data_controller.php (line 21)
Array
(
[Datum] => Array
(
[0] => Array
(
[bool_val] => 1
[field_id] => 56
[form_id] => 208
[user_id] => 1
)
[1] => Array
(
[bool_val] => 0
[field_id] => 64
[form_id] => 208
[user_id] => 1
)
)
)
// data_controller.php
// the add method is like this
function add() {
debug($this->data);
if (!empty($this->data) ) {
$this->Datum->create();
if ($this->Datum->saveAll($this->data['Datum'])) {
$this->Session->setFlash(__('The Datum has been saved', true));
$this->redirect(array('action'=>'index'));
} else {
$this->Session->setFlash(__('The Datum could not be saved. Please, try again.', true));
}
}
$forms = $this->Datum->Form->find('list');
$fields = $this->Datum->Field->find('list');
$users = $this->Datum->User->find('list');
$statuses = $this->Datum->Status->find('list');
$this->set(compact('forms', 'fields', 'users', 'statuses'));
}
这样可以正常工作并在我的MySQL数据库的“数据”表中创建一系列新条目。当我尝试在“编辑”方法中使用“saveAll()”时出现错误。我用Google搜索并用谷歌搜索,没有运气。所有文章都说我的数据结构应该是正确的,或者这就是我理解它们的方式。
所以这是我的看法。它循环到输出复选框和其他表单元素,但我们只看一个简单的复选框示例。
//field_view_datum.ctp
<?php
//debug($field_datum);
switch ($field['Type']['name']) {
case "check box" :
echo "<p>";
echo $form->label($field['FieldName'][0]['name']);
echo $form->hidden('Datum.'.$field_datum['id'].'.id', array('value' => $field_datum['id']));
echo $form->hidden('Datum.'.$field_datum['id'].'.form_id', array('value' => $formID));
echo $form->hidden('Datum.'.$field_datum['id'].'.field_id', array('value' => $field['id']));
echo $form->hidden('Datum.'.$field_datum['id'].'.user_id', array('value' => $userID));
$booly = ($field_datum['bool_val'] == 0) ? false : true;
$options = array('checked' => $booly);
echo $form->checkbox('Datum.'.$field_datum['id'].'.bool_val', $options);
echo "</p>";
break;
我的视图会将以下HTML输出到浏览器。
<p>
<label for="DatumFieldOne">Field One</label>
<input type="hidden" id="Datum164Id" value="164" name="data[Datum][164][id]"/>
<input type="hidden" id="Datum164FormId" value="208" name="data[Datum][164][form_id]"/>
<input type="hidden" id="Datum164FieldId" value="56" name="data[Datum][164][field_id]"/>
<input type="hidden" id="Datum164UserId" value="1" name="data[Datum][164][user_id]"/>
<input type="hidden" value="0" id="Datum164BoolVal_" name="data[Datum][164][bool_val]"/>
<input type="checkbox" id="Datum164BoolVal" value="1" checked="checked" name="data[Datum][164][bool_val]"/>
</p>
<p>
<label for="DatumFieldTwo">Field Two</label>
<input type="hidden" id="Datum165Id" value="165" name="data[Datum][165][id]"/>
<input type="hidden" id="Datum165FormId" value="208" name="data[Datum][165][form_id]"/>
<input type="hidden" id="Datum165FieldId" value="64" name="data[Datum][165][field_id]"/>
<input type="hidden" id="Datum165UserId" value="1" name="data[Datum][165][user_id]"/>
<input type="hidden" value="0" id="Datum165BoolVal_" name="data[Datum][165][bool_val]"/>
<input type="checkbox" id="Datum165BoolVal" value="1" name="data[Datum][165][bool_val]"/>
</p>
然后,当我提交表单时,我收到以下错误:
Fatal error: Call to a member function getColumnType() on a non-object in /cake/libs/model/model.php on line 949
我传递的数据和我的控制器方法如下所示:
//debug($this->data['Datum']); prints the following
app/controllers/data_controller.php (line 39)
Array
(
[164] => Array
(
[id] => 164
[form_id] => 208
[field_id] => 56
[user_id] => 1
[bool_val] => 1
)
[165] => Array
(
[id] => 165
[form_id] => 208
[field_id] => 64
[user_id] => 1
[bool_val] => 1
)
)
//data_controller.php
function edit($formid = null) {
debug($this->data['Datum']);
if (!empty($this->data) ) {
if ($this->Datum->saveAll($this->data['Datum'])) {
$this->Session->setFlash(__('The Datum has been saved', true));
$this->redirect(array('controller' => 'forms', 'action'=>'index'));
} else {
$this->Session->setFlash(__('The Datum could not be saved. Please, try again.', true));
$this->redirect(array('controller' => 'forms', 'action'=>'view', 'id' => '$formid'));
}
}
}
您可以提供的任何帮助都会非常感激我已经完成了大量的搜索而没有找到正确的答案。对不起,如果这篇文章有点长啰嗦。 谢谢, 德文
[编辑]
如果您需要查看我的模型或我的表结构。
//Model
//datum.php
// a basic model nothing unusual here
<?php
class Datum extends AppModel {
var $name = 'Datum';
var $belongsTo = array(
'Form' => array(
'className' => 'Form',
'foreignKey' => 'form_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Field' => array(
'className' => 'Field',
'foreignKey' => 'field_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'Status' => array(
'className' => 'Status',
'foreignKey' => 'status_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
?>
//this is from my latest schema snapshot
var $data = array(
'id' => array('type' => 'integer', 'null' => false, 'default' =>
NULL, 'key' => 'primary'),
'form_id' => array('type' => 'integer', 'null' => true, 'default'
=> NULL),
'field_id' => array('type' => 'integer', 'null' => true, 'default'
=> NULL),
'user_id' => array('type' => 'integer', 'null' => true, 'default'
=> NULL),
'status_id' => array('type' => 'integer', 'null' => true,
'default' => NULL),
'value' => array('type' => 'text', 'null' => true, 'default' =>
NULL),
'file_path' => array('type' => 'string', 'null' => true, 'default'
=> NULL),
'bool_val' => array('type' => 'boolean', 'null' => true, 'default'
=> '0'), // I am using MySQL so this is a tinyint(1)
'created' => array('type' => 'datetime', 'null' => true, 'default'
=> NULL),
'modified' => array('type' => 'datetime', 'null' => true,
'default' => NULL),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique'
=> 1))
);
[编辑]
所以我做了一个海报建议,这就是我把“debug($ model);”放到的地方。 on cake / libs / model / model.php(第949行):
cake/libs/model/model.php (line 949)
data //this being what debug() returns
Fatal error: Call to a member function getColumnType() on a non-object in /cake/libs/model/model.php on line 950
我可能会看到我的模型名称是Datum或datum.php有什么问题我的脑子里没有明显的解决方案,这对我来说有点深入了。有什么建议吗?
我刚用同一个调试语句查看了我的“view”方法,它输出“Datum”而不是像我从“edit”方法得到的“数据”。以防万一对任何人都有帮助。
答案 0 :(得分:0)
不确定是否属实,但我认为输入数组不正确。应该是这样的:
array(
[0] => array(
[Datum] => array(
[id] => 164
[form_id] => 208
[field_id] => 56
[user_id] => 1
[bool_val] => 1
)
)
[1] => array(
[Datum] => array(
[id] => 165
...
)
)
)
如前所述,我不太确定。如果这没有帮助,我建议在上述行
进行调试($ model)if ($model != $this->alias && isset($this->{$model})) {
return $this->{$model}->getColumnType($column);
}
的问候, harpax
答案 1 :(得分:0)
您使用的是相关型号吗?我用非关联模型构建了我的第一个结构,当我关联它们时,我得到了同样的错误。我必须返回并更改所有我的save(),find()等,以明确定义控制器的字段。
示例:
$total = $this->Flo->find('count', array('conditions' => $conditions, 'order' => 'id DESC'));
必须成为
$total = $this->Flo->find('count', array('conditions' => $conditions, 'order' => 'Flo.id DESC'));
不确定是否适合您,但如果您使用相关型号,我认为值得一看。
答案 2 :(得分:0)
在CakePHP小组中提供了正确的答案,请参阅:
http://groups.google.com/group/cake-php/browse_thread/thread/759c93f04a626c5b#