CakePHP Create() 未在 SQL 查询中插入所有字段
CakePHP Create() not inserting all fields in SQL query
我正在尝试将信息从表单传递到数据库中的新记录。
我的表格:
<?php
echo "<div>" . $this->Form->create('Machines', array('action' => 'add')) . "</div>";
echo "<div>" . $this->Form->input('name') . "</div>";
echo "<div>" . $this->Form->input('type', array('type' => 'select',
'options' => array(
'Stand-alone' => 'stand-alone', 'Host' => 'host', 'VM' => 'vm')
)
) . "</div>";
echo "<div>" . $this->Form->submit() . "</div>";
echo $this->Form->end();
?>
我的 "add()" 操作:
function add(){
if ($this->request->is('post')) {
if ($this->Machine->create()) {
$this->Machine->save($this->request->data);
}else{
debug($this->validationErrors); die();
}
}
}
浏览器正在传递表单数据,如 Chrome header 分析所示:
_method:POST
data[Machines][name]:Test1234
data[Machines][type]:VM
以及要保存到的数据库的结构:
CREATE TABLE IF NOT EXISTS `machines` (
`uid` bigint(255) NOT NULL,
`name` varchar(255) NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '1',
`os` varchar(255) NOT NULL DEFAULT 'Operating System Unknown',
`type` enum('vm','host','stand-alone') NOT NULL DEFAULT 'stand-alone',
`vh_id` bigint(255) NOT NULL DEFAULT '0',
`scvmm` varchar(255) NOT NULL DEFAULT 'apxlabscvmm',
`maint` bigint(255) NOT NULL,
`except` tinyint(2) NOT NULL DEFAULT '0',
`own` varchar(255) NOT NULL DEFAULT 'apxlab',
`reboot` bigint(255) NOT NULL,
`local_ip` varchar(255) DEFAULT '0.0.0.0',
`rmt_ip` varchar(255) NOT NULL DEFAULT '0.0.0.0' COMMENT 'iLo/iDRAC address',
`decomm` enum('0','1') NOT NULL DEFAULT '0' COMMENT 'Set to 1 if machine has been decommed'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Machines.name 字段被索引为唯一。
问题是当我进行插入时,create() 函数跳过了 uid 和名称字段(将名称字段留空)并且没有按预期将类型字段数据传递到数据库(它只保存默认值 "stand-alone").
"successful" INSERT 命令的查询示例:
INSERT INTO `TestDB`.`machines` (`status`, `os`, `type`, `vh_id`, `scvmm`, `except`, `own`, `local_ip`, `rmt_ip`, `decomm`) VALUES (1, 'Operating System Unknown', 'stand-alone', 0, 'apxlabscvmm', 0, 'apxlab', '0.0.0.0', '0.0.0.0', 0)
问题是在你看来你有
$this->Form->create('Machines', array('action' => 'add'))
,但在您的控制器中,您的模型似乎名为 Machine
,只需在视图中更改名称即可
我正在尝试将信息从表单传递到数据库中的新记录。
我的表格:
<?php
echo "<div>" . $this->Form->create('Machines', array('action' => 'add')) . "</div>";
echo "<div>" . $this->Form->input('name') . "</div>";
echo "<div>" . $this->Form->input('type', array('type' => 'select',
'options' => array(
'Stand-alone' => 'stand-alone', 'Host' => 'host', 'VM' => 'vm')
)
) . "</div>";
echo "<div>" . $this->Form->submit() . "</div>";
echo $this->Form->end();
?>
我的 "add()" 操作:
function add(){
if ($this->request->is('post')) {
if ($this->Machine->create()) {
$this->Machine->save($this->request->data);
}else{
debug($this->validationErrors); die();
}
}
}
浏览器正在传递表单数据,如 Chrome header 分析所示:
_method:POST
data[Machines][name]:Test1234
data[Machines][type]:VM
以及要保存到的数据库的结构:
CREATE TABLE IF NOT EXISTS `machines` (
`uid` bigint(255) NOT NULL,
`name` varchar(255) NOT NULL,
`status` enum('0','1') NOT NULL DEFAULT '1',
`os` varchar(255) NOT NULL DEFAULT 'Operating System Unknown',
`type` enum('vm','host','stand-alone') NOT NULL DEFAULT 'stand-alone',
`vh_id` bigint(255) NOT NULL DEFAULT '0',
`scvmm` varchar(255) NOT NULL DEFAULT 'apxlabscvmm',
`maint` bigint(255) NOT NULL,
`except` tinyint(2) NOT NULL DEFAULT '0',
`own` varchar(255) NOT NULL DEFAULT 'apxlab',
`reboot` bigint(255) NOT NULL,
`local_ip` varchar(255) DEFAULT '0.0.0.0',
`rmt_ip` varchar(255) NOT NULL DEFAULT '0.0.0.0' COMMENT 'iLo/iDRAC address',
`decomm` enum('0','1') NOT NULL DEFAULT '0' COMMENT 'Set to 1 if machine has been decommed'
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Machines.name 字段被索引为唯一。
问题是当我进行插入时,create() 函数跳过了 uid 和名称字段(将名称字段留空)并且没有按预期将类型字段数据传递到数据库(它只保存默认值 "stand-alone").
"successful" INSERT 命令的查询示例:
INSERT INTO `TestDB`.`machines` (`status`, `os`, `type`, `vh_id`, `scvmm`, `except`, `own`, `local_ip`, `rmt_ip`, `decomm`) VALUES (1, 'Operating System Unknown', 'stand-alone', 0, 'apxlabscvmm', 0, 'apxlab', '0.0.0.0', '0.0.0.0', 0)
问题是在你看来你有
$this->Form->create('Machines', array('action' => 'add'))
,但在您的控制器中,您的模型似乎名为 Machine
,只需在视图中更改名称即可