2016-11-23 47 views
0

因此,我編寫了一個CakePHP 3.3應用程序,該應用程序具有「Users」表和「UserProfiles」表,並且我不能爲我的生活弄清楚爲什麼當我從我的項目中傳輸項目時,代碼停止工作個人發展環境到我們的託管服務器。我在使用最新的MySQL 5.7,現在我的主機報告我的數據庫是Percona 5.5.51-38.2-log。實質上,我相信UserProfile無法保存,因爲它找不到創建的用戶。我收到的保存錯誤是:調試CakePHP保存

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`staging`.`user_profiles`, CONSTRAINT `fk_user_profiles_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) 

我記錄的查詢和我跑最後一棒顯示logs\debug.log下面的語句:

2016-11-22 23:13:40 Debug: duration=1 rows=1 SELECT Users.id AS `Users__id`, Users.email AS `Users__email`, Users.password AS `Users__password`, Users.role AS `Users__role`, Users.created AS `Users__created`, UserProfiles.user_id AS `UserProfiles__user_id`, UserProfiles.address_one AS `UserProfiles__address_one`, UserProfiles.address_two AS `UserProfiles__address_two`, UserProfiles.age AS `UserProfiles__age`, UserProfiles.sex AS `UserProfiles__sex`, UserProfiles.first_name AS `UserProfiles__first_name`, UserProfiles.last_name AS `UserProfiles__last_name`, UserProfiles.city AS `UserProfiles__city`, UserProfiles.state AS `UserProfiles__state`, UserProfiles.zip AS `UserProfiles__zip`, UserProfiles.phone AS `UserProfiles__phone`, UserProfiles.photo AS `UserProfiles__photo` FROM users Users LEFT JOIN user_profiles UserProfiles ON Users.id = (UserProfiles.user_id) LIMIT 20 OFFSET 0 
2016-11-22 23:13:40 Debug: duration=0 rows=1 SELECT (COUNT(*)) AS `count` FROM users Users LEFT JOIN user_profiles UserProfiles ON Users.id = (UserProfiles.user_id) 
2016-11-22 23:13:44 Debug: duration=0 rows=0 BEGIN 
2016-11-22 23:13:44 Debug: duration=0 rows=0 SELECT 1 AS `existing` FROM users Users WHERE Users.email = '[email protected]' LIMIT 1 
2016-11-22 23:13:44 Debug: duration=0 rows=1 INSERT INTO users (email, password, role) VALUES ('[email protected]', 'y$Gak.CvDbw7Jg8gwbEGBiNeVdj7L8i3xScNOoDBegU7DP5aU6A8Ns2', 'admin') 
2016-11-22 23:13:44 Debug: duration=0 rows=0 SELECT 1 AS `existing` FROM user_profiles UserProfiles WHERE UserProfiles.user_id = 11 LIMIT 1 
2016-11-22 23:13:44 Debug: duration=0 rows=1 SELECT 1 AS `existing` FROM users Users WHERE Users.id = 11 LIMIT 1 
2016-11-22 23:13:44 Debug: duration=0 rows=0 INSERT INTO user_profiles (user_id, address_one, address_two, age, sex, first_name, last_name, city, state, zip, phone, photo) VALUES (11, 'Test Place 42', 'Teeeeeeest', 24, 'm', 'Kyle', 'Person', 'Place', 'FL', '12312', '1231231234', '') 
2016-11-22 23:13:44 Debug: duration=0 rows=0 ROLLBACK 

所以,看來INSERT INTO user_profiles語句正確選擇(在這種情況下爲11),但是我在互聯網上讀到的所有內容都表示它可能無法通過id 11找到用戶。CakePHP在我的***中非常痛苦,並且隱藏了可變數據代碼從我的錯誤,說我的SQL查詢是:

INSERT INTO user_profiles (user_id, address_one, address_two, age, sex, first_name, last_name, city, state, zip, phone, photo) VALUES (:c0, :c1, :c2, :c3, :c4, :c5, :c6, :c7, :c8, :c9, :c10, :c11) 

因此,如果有人知道如何閱讀這些「c變量」,那麼我會想象一下,這將有很大的幫助。我不擅長調試,所以請隨時推薦我可以用來提高生產力的任何方法。

這是構建兩個表的SQL代碼:

SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0; 
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; 
SET @[email protected]@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES'; 

CREATE SCHEMA IF NOT EXISTS `staging` DEFAULT CHARACTER SET utf8 ; 
USE `staging` ; 

DROP TABLE IF EXISTS `staging`.`users` ; 

CREATE TABLE IF NOT EXISTS `staging`.`users` (
    `id` INT NOT NULL AUTO_INCREMENT, 
    `email` VARCHAR(255) NOT NULL, 
    `password` VARCHAR(128) NOT NULL, 
    `role` ENUM('admin', 'vet', 'client') NOT NULL, 
    PRIMARY KEY (`id`)); 

DROP TABLE IF EXISTS `staging`.`user_profiles` ; 

CREATE TABLE IF NOT EXISTS `staging`.`user_profiles` (
    `user_id` INT NOT NULL, 
    `address_one` VARCHAR(255) NULL, 
    `address_two` VARCHAR(255) NULL, 
    `age` INT NULL, 
    `sex` ENUM('m', 'f') NULL, 
    `first_name` VARCHAR(45) NULL, 
    `last_name` VARCHAR(45) NULL, 
    `city` VARCHAR(45) NULL, 
    `state` VARCHAR(45) NULL, 
    `zip` VARCHAR(6) NULL, 
    `phone` VARCHAR(45) NULL, 
    `photo` VARCHAR(255) NULL, 
    PRIMARY KEY (`user_id`), 
    INDEX `fk_user_profiles_users2_idx` (`user_id` ASC), 
    CONSTRAINT `fk_user_profiles_users` 
    FOREIGN KEY (`user_id`) 
    REFERENCES `staging`.`users` (`id`) 
    ON DELETE CASCADE 
    ON UPDATE CASCADE) 
ENGINE = InnoDB; 

SET [email protected]_SQL_MODE; 
SET [email protected]_FOREIGN_KEY_CHECKS; 
SET [email protected]_UNIQUE_CHECKS; 

這是由MySQLWorkbench產生。

非常感謝你的時間和幫助!

回答

0

這不再是一個PHP問題。現在要解僱我的問題。

+0

這不是問題的答案,請嘗試評論或編輯。 –