Cakephp saveAll() Fatal error: Call to a member function getColumnType()
So I am creating a form builder. Users will login and then fillout the forms that Admins have created. I am using saveAll() in my "data_controller" "add" method.
This works fine and and looks like this:
//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'));
}
So that works fine and creates a series of new entries in the "data" table of my MySQL database. My error comes when I try to use "saveAll()" in my "edit" method. I have googled and googled and googled with no luck. All of the articles say that my data structure should be correct, or that is how I am understanding them.
So here is my view. It loops through to output checkboxes and other form elements but we will just look at a simple check box only example.
//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;
My view will output the following HTML to the browser.
<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>
So then when I submit the form I get the following error:
Fatal error: Call to a member function getColumnType() on a non-object in /cake/libs/model/model.php on line 949
The data I am passing and my controller method look like this:
//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'));
}
}
}
Any help that you can give would be much appreciated I have done tons of searching and not found the correct answer. Sorry if this post is a bit long winded. Thanks, Devin
[EDIT]
In case you need to look at my model or my table structure.
//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' => tru开发者_运维百科e, 'default'
=> NULL),
'modified' => array('type' => 'datetime', 'null' => true,
'default' => NULL),
'indexes' => array('PRIMARY' => array('column' => 'id', 'unique'
=> 1))
);
[EDIT]
So I did as one Poster suggested and this is what I got when putting "debug($model);" on cake/libs/model/model.php (line 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
I might see what is wrong my model name is Datum or datum.php not data I have no obvious solution in my head, this is a bit deep in Cake for me. Any Suggestions?
I just took a look at my "view" method with the same debug statement and it outputs "Datum" and not "data" like I get from my "edit" method. Just in case that is helpful to anyone.
The correct answer was delivered in the CakePHP group, see:
http://groups.google.com/group/cake-php/browse_thread/thread/759c93f04a626c5b#
Not sure if that is true, but I think the input array is not correct. Should look like this:
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
...
)
)
)
As mentioned before, I am not quite sure though. If that does not help I would recommend to place a debug($model) at the mentioned line
if ($model != $this->alias && isset($this->{$model})) {
return $this->{$model}->getColumnType($column);
}
regards, harpax
Are you using associated models? I built my first structure with non-associated models and when I associated them I got that same error. I had to go back and change all my save(), find(), etc to explicitly define the field for the controller.
Example:
$total = $this->Flo->find('count', array('conditions' => $conditions, 'order' => 'id DESC'));
had to become
$total = $this->Flo->find('count', array('conditions' => $conditions, 'order' => 'Flo.id DESC'));
Not sure is this is the case for you but if you're using associated models I think it's worth a look.
精彩评论