gpt4 book ai didi

php - 调试 CakePHP 保存

转载 作者:行者123 更新时间:2023-11-29 19:53:30 26 4
gpt4 key购买 nike

所以,我编写了一个 CakePHP 3.3 应用程序,它有一个“Users”表和一个“UserProfiles”表,但我一生都无法弄清楚为什么当我从个人开发环境转移项目时我的代码停止工作到我们的托管服务器。我在构建中使用的是最新的 MySQL 5.7,现在我的主机报告我的数据库是 Percona 5.5.51-38.2-log。本质上,我认为 UserProfile 无法保存,因为它找不到创建的用户。我在保存时收到的错误是:

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 = 'admin1@domain.com' LIMIT 1
2016-11-22 23:13:44 Debug: duration=0 rows=1 INSERT INTO users (email, password, role) VALUES ('admin1@domain.com', '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 语句似乎正确地获取了用户的 ID(本例中为 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 @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@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 SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

这是由 MySQLWorkbench 生成的。

非常感谢您的时间和帮助!

最佳答案

这不再是 PHP 问题。现在要驳回我的问题。

关于php - 调试 CakePHP 保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40754107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com