Reputation: 117
I am creating my first real CakePHP project. I have read the manual and gone through the blog tutorial, but am by no means an expert. I'm having a problem adding data to my database through a form generated with the form helper. The form has two text inputs and a few select boxes, all of which are populating correctly. When I fill out the form and hit submit, it tells me I have a foreign key constraint error on the first select box. However, when I debug $this->request->data, it has the correct values associated with it. Here is the debug.
Array
(
[car] => Array
(
[stock] => G123456
[vin] => 12345678
[make_id] => 1
[car_model_id] => 2
[year_id] => 20
[location_id] => 9
[service_status_id] => 1
[type_id] => 6
)
)
To make sure my schema was correct I did the insert directly from mysql console and it worked perfectly. Here is the command I ran.
INSERT INTO cars (stock, vin, make_id, car_model_id, year_id, location_id, service_status_id, type_id) VALUES ('G123456', '12345678', '1', '2', '20', '9', '1', '6');
I'm not sure why it's giving me the foreign key constraint error when I call:
$car = $this->Car->save($this->request->data);
Any ideas?
EDIT The query under the error in CakePHP is:
INSERT INTO `cars` (`modified`, `created`) VALUES ('2012-02-29 15:53:21', '2012-02-29 15:53:21')
When I run that query from a mysql console I get the same error. Foreign key constraint fails, make_id - reference make.id
Here is the add() function in my controller:
public function add()
{
$this->set('years', $this->Car->Year->find('list'));
$this->set('makes', $this->Car->Make->find('list'));
$this->set('carModels', $this->Car->CarModel->find('list'));
$this->set('locations', $this->Car->Location->find('list'));
$this->set('types', $this->Car->Type->find('list'));
$this->set('serviceStatuses', $this->Car->ServiceStatus->find('list'));
if(!empty($this->request->data))
{
$car = $this->Car->save($this->request->data);
//debug($this->request->data, true);
}
}
And here is the view file:
<?php
echo $this->Form->create('Car', array('action' => 'add'));
echo $this->Form->input('car.stock');
echo $this->Form->input('car.vin');
echo $this->Form->input('car.make_id');
echo $this->Form->input('car.car_model_id');
echo $this->Form->input('car.year_id');
echo $this->Form->input('car.location_id');
echo $this->Form->input('car.service_status_id');
echo $this->Form->input('car.type_id');
echo $this->Form->end('Add');
?>
Upvotes: 0
Views: 503
Reputation: 11574
The problem is the view. Remove the car.
from the beginning of each form input. It is not needed. The create will user the Car
model as the prefix and it will fix the problem.
<?php
echo $this->Form->create('Car', array('action' => 'add'));
echo $this->Form->input('stock');
echo $this->Form->input('vin');
echo $this->Form->input('make_id');
echo $this->Form->input('car_model_id');
echo $this->Form->input('year_id');
echo $this->Form->input('location_id');
echo $this->Form->input('service_status_id');
echo $this->Form->input('type_id');
echo $this->Form->end('Add');
?>
Upvotes: 1