[英]how do i pattern a php database call?
specs: PHP 5 with mySQL built on top of Codeigniter Framework. 规范:PHP 5,mySQL构建于Codeigniter Framework之上。
I have a database table called game
and then sport specific tables like soccerGame
and footballGame
. 我有一个名为
game
的数据库表,然后是footballGame
game
和footballGame
等特定soccerGame
。 these sport specific tables have a gameId
field linking back to the game
table. 这些运动特定表有一个
gameId
字段链接回game
表。 I have corresponding classes game
and soccerGame
/ footballGame
, which both extend game
. 我有相应的类
game
和soccerGame
/ footballGame
,其中两个延伸game
。
When I look up game information to display to the user, I'm having trouble figuring out how to dynamically link the two tables. 当我查找要向用户显示的游戏信息时,我无法确定如何动态链接这两个表。 i'm curious if it's possible to get all the information with with one query.
我很好奇是否有可能通过一个查询获得所有信息。 The problem is, I need to query the
game
table first to figure out the sport name. 问题是,我需要首先查询
game
桌以找出运动名称。
if that's not possible, my next thought is to do it with two queries. 如果那是不可能的,我的下一个想法是用两个查询来做。 have my
game_model
query the game table, then based off the sport name, call the appropriate sport specific model (ie soccer_game_model
) and get the sport specific info. 让我的
game_model
查询游戏表,然后根据运动名称,调用适当的运动特定模型(即soccer_game_model
)并获得运动特定信息。
I would also pass the game
object into the soccer_model
, and the soccer_model
would use that object to build me a soccerGame
object. 我还将
game
对象传递给soccer_model
, soccer_model
将使用该对象为我构建一个soccerGame
对象。 this seems a little silly to me because i'm building the parent object and then giving it to the extending class to make a whole new object? 这对我来说有点傻,因为我正在构建父对象,然后将它提供给扩展类来创建一个全新的对象?
thoughts? 想法?
thanks for the help. 谢谢您的帮助。
EDIT: 编辑:
game table 游戏桌
gameId
sport (soccer, basketball, football, etc)
date
other data
soccerGame table 足球游戏桌
soccerGameId
gameId
soccer specific information
footballGame table 足球游戏桌
footballGameId
gameId
football specific information
and so on for other sports 等等其他运动
So I need to know what the sport is before I can decide which sport specific table I need to pull info from. 所以我需要知道这项运动是什么,然后我才能决定从哪个运动特定的桌子提取信息。
UPDATE: 更新:
Thanks all for the input. 感谢大家的投入。 It seems like dynamic SQL is only possible through stored procedures, something I'm not well versed on right now.
似乎动态SQL只能通过存储过程实现,这是我现在不太熟悉的东西。 And even with them it's still a little messy.
即使有它们,它仍然有点凌乱。 Right now I will go the two query route, one to get the sport name, and then a switch to get the right model.
现在我将进入两个查询路线,一个获取运动名称,然后切换到获得正确的模型。
From the PHP side of things now, it seems a little silly to get a game
object, pass it to, say, my soccer_game_model
, and then have that return me a soccer_game
object, which is a child of the original game
. 从现在的PHP方面来看,获取
game
对象,将其传递给我的soccer_game_model
似乎有点傻,然后让我返回一个soccer_game
对象,这是原始game
的孩子。 Is that how it has to be done? 这是怎么做的? or am I missing something from an OO perspective here?
或者我是否从OO的角度遗漏了一些东西?
UPDATE UPDATE
Consider passing the "sport" parameter when you look up game data. 在查找游戏数据时,请考虑传递“sport”参数。 As a hidden field, most likely.
作为一个隐藏的领域,最有可能。 You can then use a switch statement in your model:
然后,您可以在模型中使用switch语句:
switch($gameValue) {
case 'football': $gameTable = "footballGame"; break;
case 'soccer': $gameTable = "soccerGame"; break;
}
Then base your query off this: 然后将您的查询基于此:
"SELECT *
FROM ". $gameTable . "
...etc
You can combine the tables with joins. 您可以将表与联接组合在一起。 http://www.w3schools.com/sql/sql_join.asp
http://www.w3schools.com/sql/sql_join.asp
For example, if you need to get all the data from game
and footballGame
based on a footballGameId of 15: 例如,如果您需要根据footballGameId 15获取来自
game
和footballGame
的所有数据:
SELECT *
FROM footballGame a
LEFT OUTER JOIN game b ON a.id = b.gameId
WHERE footballGameId = 15
To extend on Devin Young's answer, you would achieve this using Codeigniter's active record class like so: 为了扩展Devin Young的答案,你可以使用Codeigniter的活动记录类来实现这一点,如下所示:
public function get_game_by_id($game_id, $table)
{
return $this->db->join('game', 'game.id = ' . $table . '.gameId', 'left')
->where($table . '.gameId', $game_id)
->get('game')
->result();
}
So you're joining the table by the gameId which is shared, then using a where clause to find the correct one. 所以你要通过共享的gameId加入表,然后使用where子句找到正确的子句。 Finally you use result() to return an array of objects.
最后,使用result()返回一个对象数组。
EDIT: I've added a second table paramater to allow you to pass in the name of the table you can join either soccerGame, footballGame table etc. 编辑:我添加了第二个表参数,允许您传递表格的名称,您可以加入soccerGame,footballGame table等。
If you don't know which sport to choose at this point in the program then you may want to take a step back and look at how you can add that so you do know. 如果您不知道在该计划中此时选择哪项运动,那么您可能想退后一步,看看如何添加,以便您知道。 I would be reluctant to add multiple joins to all sport tables as you''ll run into issues down the line.
我不愿意为所有运动表添加多个连接,因为你会遇到问题。
Check this Stack Overflow answer for options on how to do it via a standard query. 有关如何通过标准查询执行此操作的选项 , 请查看此Stack Overflow应答 。 Then you can turn it into active record if you want (though that may be complicated and not worth your time if you don't need DB-agnostic calls in your app).
然后,如果你愿意,你可以把它变成活动记录(虽然如果你的应用程序中不需要与数据库无关的调用,这可能很复杂并且不值得你花时间)。
Fow what it's worth, there's nothing wrong with doing multiple queries, it just might be slower than an alternative. 哇它的价值,做多个查询没什么不对,它可能比另一个慢。 Try a few options, see what works best for you and your app.
尝试一些选项,看看什么最适合您和您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.