使用 Doctrine PHP 生成 SQL 命令时出现 SQL 错误

发布于 2024-09-30 14:12:58 字数 4779 浏览 9 评论 0原文

我在 PHP Web 应用程序上使用 Doctrine 和 MySQL 数据库。我的请求有一个小问题:

$q = Doctrine_Query::create()
     ->select('event.EventDate, event.EventId, player.PlayerId, player.FirstName, 
             player.LastName, player.Login, player.Email, 
             event_period.EventPeriodId, event.Type, event.Description, 
             period.StartHour, period.EndHour, player_event_period.Participate, 
             event_period.CourtNumber')
     ->from('Event event, Player player, Period period')
     ->leftJoin('player.PlayerEventPeriod player_event_period')
     ->leftJoin('player_event_period.EventPeriod event_period')
     ->where('period.PeriodId = event_period.PeriodId')
     ->andWhere('event.EventId = event_period.EventId');

如果我显示生成的 MySQL 命令是什么,我可以看到

SELECT e.eventid AS e__eventid, e.eventdate AS e__eventdate, e.type AS e__type, e.description AS e__description, p.playerid AS p__playerid, p.firstname AS p__firstname, p.lastname AS p__lastname, p.login AS p__login, p.email AS p__email, p2.periodid AS p2__periodid, p2.starthour AS p2__starthour, p2.endhour AS p2__endhour, p3.playereventperiodid AS p3__playereventperiodid, p3.participate AS p3__participate, e2.eventperiodid AS e2__eventperiodid, e2.courtnumber AS e2__courtnumber
FROM event e, player p, period p2
LEFT JOIN player_event_period p3 ON p.playerid = p3.playerid
LEFT JOIN event_period e2 ON p3.eventperiodid = e2.eventperiodid
WHERE (
p2.periodid = e2.periodid
AND e.eventid = e2.eventid
);

错误:

#1054 - Unknown column 'p.playerid' in 'on clause'

我的请求出了什么问题?

预先感谢您

编辑:

如果这可以帮助您。我已经添加了创建数据库表的脚本。

CREATE TABLE `event` (
  `EventId` int(11) NOT NULL AUTO_INCREMENT,
  `Type` varchar(100) NOT NULL,
  `Description` text,
  `EventDate` datetime NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`EventId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE `event_period` (
  `EventPeriodId` int(11) NOT NULL AUTO_INCREMENT,
  `PeriodId` int(11) NOT NULL,
  `EventId` int(11) NOT NULL,
  `CourtNumber` int(11) NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`EventPeriodId`),
  KEY `fk_event_period_to_period` (`PeriodId`),
  KEY `fk_event_period_to_event` (`EventId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE `period` (
  `PeriodId` int(11) NOT NULL AUTO_INCREMENT,
  `StartHour` time NOT NULL,
  `EndHour` time NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`PeriodId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE `player` (
  `PlayerId` int(11) NOT NULL AUTO_INCREMENT,
  `Login` varchar(100) NOT NULL,
  `Password` varchar(100) NOT NULL,
  `RankId` int(11) NOT NULL DEFAULT '1',
  `FirstName` varchar(100) NOT NULL,
  `LastName` varchar(100) NOT NULL,
  `Email` varchar(100) NOT NULL,
  `ValidateId` varchar(100) NOT NULL,
  `InscriptionDate` datetime NOT NULL,
  `Enable` tinyint(1) NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`PlayerId`),
  KEY `fk_player_to_rank` (`RankId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE `player_event_period` (
  `PlayerEventPeriodId` int(11) NOT NULL AUTO_INCREMENT,
  `PlayerId` int(11) NOT NULL,
  `EventPeriodId` int(11) NOT NULL,
  `Participate` tinyint(1) NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`PlayerEventPeriodId`),
  KEY `fk_player_event_period_to_player` (`PlayerId`),
  KEY `fk_player_event_period_to_event_period` (`EventPeriodId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


ALTER TABLE `event_period`
  ADD CONSTRAINT `fk_event_period_to_period` FOREIGN KEY (`PeriodId`) REFERENCES `period` (`PeriodId`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_event_period_to_event` FOREIGN KEY (`EventId`) REFERENCES `event` (`EventId`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `player`
  ADD CONSTRAINT `fk_player_to_rank` FOREIGN KEY (`RankId`) REFERENCES `rank` (`RankId`);

ALTER TABLE `player_event_period`
  ADD CONSTRAINT `fk_player_event_period_to_player` FOREIGN KEY (`PlayerId`) REFERENCES `player` (`PlayerId`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_player_event_period_to_event_period` FOREIGN KEY (`EventPeriodId`) REFERENCES `event_period` (`EventPeriodId`) ON DELETE CASCADE ON UPDATE CASCADE;

I use Doctrine on my PHP web application with a MySQL database. I have a little problem with my request :

$q = Doctrine_Query::create()
     ->select('event.EventDate, event.EventId, player.PlayerId, player.FirstName, 
             player.LastName, player.Login, player.Email, 
             event_period.EventPeriodId, event.Type, event.Description, 
             period.StartHour, period.EndHour, player_event_period.Participate, 
             event_period.CourtNumber')
     ->from('Event event, Player player, Period period')
     ->leftJoin('player.PlayerEventPeriod player_event_period')
     ->leftJoin('player_event_period.EventPeriod event_period')
     ->where('period.PeriodId = event_period.PeriodId')
     ->andWhere('event.EventId = event_period.EventId');

if I show what is the generated MySQL command, I can see

SELECT e.eventid AS e__eventid, e.eventdate AS e__eventdate, e.type AS e__type, e.description AS e__description, p.playerid AS p__playerid, p.firstname AS p__firstname, p.lastname AS p__lastname, p.login AS p__login, p.email AS p__email, p2.periodid AS p2__periodid, p2.starthour AS p2__starthour, p2.endhour AS p2__endhour, p3.playereventperiodid AS p3__playereventperiodid, p3.participate AS p3__participate, e2.eventperiodid AS e2__eventperiodid, e2.courtnumber AS e2__courtnumber
FROM event e, player p, period p2
LEFT JOIN player_event_period p3 ON p.playerid = p3.playerid
LEFT JOIN event_period e2 ON p3.eventperiodid = e2.eventperiodid
WHERE (
p2.periodid = e2.periodid
AND e.eventid = e2.eventid
);

I get error :

#1054 - Unknown column 'p.playerid' in 'on clause'

What's wrong with my request ?

Thank you in advance

EDIT:

If that could help you. I have add the script that creates my database tables.

CREATE TABLE `event` (
  `EventId` int(11) NOT NULL AUTO_INCREMENT,
  `Type` varchar(100) NOT NULL,
  `Description` text,
  `EventDate` datetime NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`EventId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE `event_period` (
  `EventPeriodId` int(11) NOT NULL AUTO_INCREMENT,
  `PeriodId` int(11) NOT NULL,
  `EventId` int(11) NOT NULL,
  `CourtNumber` int(11) NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`EventPeriodId`),
  KEY `fk_event_period_to_period` (`PeriodId`),
  KEY `fk_event_period_to_event` (`EventId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE `period` (
  `PeriodId` int(11) NOT NULL AUTO_INCREMENT,
  `StartHour` time NOT NULL,
  `EndHour` time NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`PeriodId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE `player` (
  `PlayerId` int(11) NOT NULL AUTO_INCREMENT,
  `Login` varchar(100) NOT NULL,
  `Password` varchar(100) NOT NULL,
  `RankId` int(11) NOT NULL DEFAULT '1',
  `FirstName` varchar(100) NOT NULL,
  `LastName` varchar(100) NOT NULL,
  `Email` varchar(100) NOT NULL,
  `ValidateId` varchar(100) NOT NULL,
  `InscriptionDate` datetime NOT NULL,
  `Enable` tinyint(1) NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`PlayerId`),
  KEY `fk_player_to_rank` (`RankId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

CREATE TABLE `player_event_period` (
  `PlayerEventPeriodId` int(11) NOT NULL AUTO_INCREMENT,
  `PlayerId` int(11) NOT NULL,
  `EventPeriodId` int(11) NOT NULL,
  `Participate` tinyint(1) NOT NULL,
  `CreationDate` datetime NOT NULL,
  `UpdateDate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`PlayerEventPeriodId`),
  KEY `fk_player_event_period_to_player` (`PlayerId`),
  KEY `fk_player_event_period_to_event_period` (`EventPeriodId`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;


ALTER TABLE `event_period`
  ADD CONSTRAINT `fk_event_period_to_period` FOREIGN KEY (`PeriodId`) REFERENCES `period` (`PeriodId`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_event_period_to_event` FOREIGN KEY (`EventId`) REFERENCES `event` (`EventId`) ON DELETE CASCADE ON UPDATE CASCADE;

ALTER TABLE `player`
  ADD CONSTRAINT `fk_player_to_rank` FOREIGN KEY (`RankId`) REFERENCES `rank` (`RankId`);

ALTER TABLE `player_event_period`
  ADD CONSTRAINT `fk_player_event_period_to_player` FOREIGN KEY (`PlayerId`) REFERENCES `player` (`PlayerId`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_player_event_period_to_event_period` FOREIGN KEY (`EventPeriodId`) REFERENCES `event_period` (`EventPeriodId`) ON DELETE CASCADE ON UPDATE CASCADE;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言月老 2024-10-07 14:12:58

不确定你的模型看起来如何,但你的名为“player”的表中有一个名为“playerid”的列吗?

这就是mysql所抱怨的。

[编辑]
我认为 Doctrine 更喜欢小写的列名。
(不记得 MySQL 是否区分列名的大小写......)

Not sure how your models look but do your table called "player" have a column called "playerid" in it?

That's what mysql is complaining about.

[Edit]

I think that Doctrine prefers column names i lowercase.
(Can't remember if MySQL is case-sensitive regarding column names...)

夏夜暖风 2024-10-07 14:12:58

MySQL 列名的大小写敏感度取决于数据库/架构/表的配置方式,但看起来您的列名是区分大小写的。

您尚未提供实体定义,因此我猜测问题所在,但请尝试设置列的名称。

例如,对于玩家 id,注释将是:

/**
 * @Column(name="PlayerId", type="integer", length=11, nullable=false)
 * @Id
 * GeneratedValue(strategy="AUTO")
 */
protected $PlayerId;

Case sensitivity of MySQL column names depends on how the database/schema/tables are configured, but it looks like yours are case sensitive.

You haven't provided the entity definitions so I'm guessing at the issue, but try setting the name of the column.

For example for player id the annotation would be:

/**
 * @Column(name="PlayerId", type="integer", length=11, nullable=false)
 * @Id
 * GeneratedValue(strategy="AUTO")
 */
protected $PlayerId;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文