使用CakePHP将记录插入数据库导致外键冲突

时间:2014-04-05 19:12:08

标签: php mysql database cakephp foreign-keys

我现在正在使用CakePHP(几天),我在使用外键插入记录方面遇到了问题。

基本理念,仓库。

我有一个仓库表,其中包含所有可用的仓库,我有一个 warehouse_types 表来保存不同类型的仓库。 warehouse_types 表中已包含一些数据。

我有一个简单的添加视图(基于CakePHP&Cookbook中的博客示例),我让它显示数据库中不同类型的名称。

当我保存页面时,我收到数据库错误。

Error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`kvs`.`warehouses`, CONSTRAINT `warehouses_ibfk_1` FOREIGN KEY (`warehouse_type_id`) REFERENCES `warehouse_types` (`id`))

正在生成的SQL是:

SQL Query: INSERT INTO `kvs`.`warehouses` (`name`, `location`, `modified`, `created`) VALUES ('Name Text', 'Location Text', '2014-04-02 12:07:13', '2014-04-02 12:07:13') 

如果我在request-> data对象中输出信息,则表明它正在获取warehouseType_id的值。

array(
    'Warehouse' => array(
        'name' => 'Name Text',
        'location' => 'Location Text',
        'warehouseType_id' => '1'
    )
)

对此的任何帮助都很棒!

我的数据库表如下:

CREATE TABLE `warehouses` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  `location` varchar(255) DEFAULT NULL,
  `warehouse_type_id` int(10) unsigned NOT NULL,
  `created` datetime DEFAULT NULL,
  `modified` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `warehouse_type_id` (`warehouse_type_id`),
  CONSTRAINT `warehouses_ibfk_1` FOREIGN KEY (`warehouse_type_id`) REFERENCES `warehouse_types` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=latin1;

CREATE TABLE `warehouse_types` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

我的模型如下:

class Warehouse extends AppModel{
    public $belongsTo = 'WarehouseType';
}

class WarehouseType extends AppModel{

}

我的观点(add.ctp)如下:

<h1>Add Warehouse</h1>
<?php
echo $this->Form->create('Warehouse');
echo $this->Form->input('name');
echo $this->Form->input('location');
echo $this->Form->input('warehouseType_id');
echo $this->Form->end('Create Warehouse');
?>

最后,我的WarehousesController中的add方法:

public function add(){
        //Check Request to see if it is a Post
        if($this->request->is('post')){
            $this->Warehouse->create(); //Make a new Warehouse object
            debug($this->request->data);
            if($this->Warehouse->save($this->request->data)){ //Try and save Warehouse
                $this->Session->setFlash(__('Warehouse '.$this->request->data['Warehouse']['name'].' created'));
                return $this->redirect(array('action' => 'index')); //Redirect back to Warehouse list
            }
            $this->Session->setFlash(__('Unable to add Warehouse :('));
        }
        $this->set('warehouseTypes', $this->Warehouse->WarehouseType->find('list'));
    }

我在这里读过这个答案似乎最相关,但我迷失了:CakePHP Can't insert record with foreign key error

还查看了有关保存的文档:http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto

2 个答案:

答案 0 :(得分:1)

您的列名与CakePHP中使用的列名不同。将CakePHP更改为使用warehouse_type_id。 (您的CakePHP代码正在使用warehouseType_id

答案 1 :(得分:1)

在您的视图文件中更改此内容 -

添加仓库

<?php
echo $this->Form->create('Warehouse');
echo $this->Form->input('name');
echo $this->Form->input('location');
echo $this->Form->input('warehouseTypes');
echo $this->Form->end('Create Warehouse');
?>