Reputation: 13
I am trying to pass information from a form to a new record in the database.
My form:
<?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();
?>
My "add()" action:
function add(){
if ($this->request->is('post')) {
if ($this->Machine->create()) {
$this->Machine->save($this->request->data);
}else{
debug($this->validationErrors); die();
}
}
}
The form data is being passed by the browser as shown in the Chrome header analysis:
_method:POST
data[Machines][name]:Test1234
data[Machines][type]:VM
and the structure of the database that it is saving to:
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;
The Machines.name field is indexed as Unique.
The problem is that when I do the insert, the create() function is skipping the uid and name fields (leaving the name field empty) and not passing the type field data to the database as expected (it only saves the default of "stand-alone").
Query example of "successful" INSERT command:
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)
Upvotes: 1
Views: 905
Reputation: 1400
The problem is that in your view you have
$this->Form->create('Machines', array('action' => 'add'))
, but in your controller it looks like your Model is called Machine
, just change the name in your view
Upvotes: 3