I have two tables,Forms and Attributes. I'm tring to retrieve the last inserted id from the Forms table and insert it to the form_id column of the Attributes table, along with the other field columns.
我有两个表,表单和属性。我想从Forms表中检索最后插入的id,并将其插入Attributes表的form_id列以及其他字段列。
Earlier I retrieved the form Id from the Forms table and used it to update the value of the Form name column. It worked fine.The code for that is given below:
之前我从Forms表中检索了表单ID,并用它来更新Form name列的值。它工作得很好。代码如下:
function saveFormName($data)
{
$this->data['Form']['formname']=$data['Form']['formname'];
$this->data['Form']['id']=$this->find('all', array(
'fields' => array('Form.id'),
'order' => 'Form.id DESC'
));
$this->id=$this->data['Form']['id'][0];
$this->saveField('name',$this->data['Form']['formname']);
}
But When I tried to do it in a similar way for updating the attributes table,the row is not saved in the database,since the value of $this->data['Form']['id'][0] is an 'Array'. Even in the saveFormName function, the value of $this->data['Form']['id'][0] is an 'Array',but the form name gets updated correctly. Someone explain me the concept.
但是当我尝试以类似的方式更新属性表时,该行不会保存在数据库中,因为$ this-> data ['Form'] ['id'] [0]的值是“数组”。即使在saveFormName函数中,$ this-> data ['Form'] ['id'] [0]的值也是'Array',但表单名称会正确更新。有人向我解释了这个概念。
function saveFieldEntries($data)
{
$this->data['Form']['id']=$this->find('all', array(
'fields' => array('Form.id'),
'order' => 'Form.id DESC'
));
$this->data['Attribute']['form_id'] = $this->data['Form']['id'][0];
$this->data['Attribute']['label']= 'Label';
$this->data['Attribute']['size']='20';
$this->data['Attribute']['type']=$data['Attribute']['type'];
$this->data['Attribute']['sequence_no'] = $data['Attribute']['sequence_no'];
$this->Attribute->save($this->data);
}
EDIT:
Ok, here is the corresponding code in the controller.
好的,这是控制器中的相应代码。
function insertFormName()
{
$this->data['Form']['formname']=$this->params['form']['formname'];
$this->Form->saveFormName($this->data);
}
function insertFieldEntry()
{
$this->data['Attribute']['type']=$this->params['form']['type'];
$this->data['Attribute']['sequence_no'] = $this->params['form']['sequence_no'];
$this->Form->saveFieldEntries($this->data);
}
The parameters form name,type and sequence no are passed to the controller from the corresponding view file.
参数形式名称,类型和序列号从相应的视图文件传递给控制器。
1 个解决方案
#1
$this->data['Form']['id'][0] holds an array because find('all') returns an array.
$ this-> data ['Form'] ['id'] [0]包含一个数组,因为find('all')返回一个数组。
So if you need first ID from this array, you need to pick it properly in function saveFieldEntries:
因此,如果您需要此数组中的第一个ID,则需要在函数saveFieldEntries中正确选择它:
...
$this->data['Attribute']['form_id'] = $this->data['Form']['id'][0]['Form']['id'];
...
#1
$this->data['Form']['id'][0] holds an array because find('all') returns an array.
$ this-> data ['Form'] ['id'] [0]包含一个数组,因为find('all')返回一个数组。
So if you need first ID from this array, you need to pick it properly in function saveFieldEntries:
因此,如果您需要此数组中的第一个ID,则需要在函数saveFieldEntries中正确选择它:
...
$this->data['Attribute']['form_id'] = $this->data['Form']['id'][0]['Form']['id'];
...