【问题标题】:Debug CakePHP Save调试 CakePHP 保存
【发布时间】:2016-11-23 00:26:47
【问题描述】:

所以,我编写了一个 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 mysql cakephp-3.0 percona


    【解决方案1】:

    这不再是 PHP 问题。暂时忽略我的问题。

    【讨论】:

    • 这不是问题的答案。尝试评论或编辑。
    猜你喜欢
    • 2023-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多