【发布时间】:2012-11-13 20:24:09
【问题描述】:
我找到了一个基于 PHP 的食谱和膳食计划脚本,我想对其进行编辑。
MealPlan 类允许所有用户通过从数据库中找到的所有食谱列表中进行选择来创建自己的每日菜单/膳食计划。
我想根据用户的级别切换insert语句中使用的表/列名。
原来的插入方法是:
// Insert Meal Plan
function insert($date, $meal, $recipe, $servings, $owner) {
global $DB_LINK, $db_table_mealplans, $LangUI;
$date = $DB_LINK->addq($date, get_magic_quotes_gpc());
$meal = $DB_LINK->addq($meal, get_magic_quotes_gpc());
$recipe = $DB_LINK->addq($recipe, get_magic_quotes_gpc());
$servings = $DB_LINK->addq($servings, get_magic_quotes_gpc());
$owner = $DB_LINK->addq($owner, get_magic_quotes_gpc());
$sql = "INSERT INTO $db_table_mealplans (
mplan_date,
mplan_meal,
mplan_recipe,
mplan_servings,
mplan_owner)
VALUES (
'$date',
$meal,
$recipe,
$servings,
'$owner')";
$rc = $DB_LINK->Execute($sql);
DBUtils::checkResult($rc, NULL, $LangUI->_('Recipe already added on:'.$date), $sql);
}
我的想法是改变:
mplan_date,
mplan_meal,
mplan_recipe,
mplan_servings,
mplan_owner
到:
$mplan_date,
$mplan_meal,
$mplan_recipe,
$mplan_servings,
$mplan_owner
并使用 switch 语句更改表/列名称。但有人告诉我,这种事情在 OOP 中是不受欢迎的。解决这个问题的最佳方法是什么?
(注意:如果需要,我可以发布整个课程,但我认为这已经足够了)
【问题讨论】:
-
db_table_mealplans中还有哪些其他列可以切换到?这听起来不像你有编程问题,听起来更像是应用程序设计/数据设计问题。如果你必须尝试完成这样的事情,你可能会做错事。
标签: php class methodology object-oriented-database