【发布时间】:2015-08-21 11:46:27
【问题描述】:
我目前正在做一个扩展课程,但仍然不知道这是否是最好的方法
假设主类类似于上面的内容(请记住,表中的字段超过 2 个,但出于演示目的,我只写了 2 个字段):
Class Product () {
public $id, $product;
protected $sql;
public function __construct() {
$this->connect = Singleton::getconnect();
}
public function __destruct() {
$this->connect = null;
}
public function insert_product() {
$this->sql = 'INSERT INTO sh_products (id,product) VALUES (NULL, "'.
addslashes($this->product) . '")';
$return_id = $this->connect->mysql_execute($this->sql);
return $return_id;
}
public function update_product() {
$this->sql = 'UPDATE sh_products SET '.
'product="' . addslashes($this->product) . '"'.
'WHERE id = ' . $this->id;
$aff_row = $this->connect->mysql_execute($this->sql);
return $aff_row;
}
}
现在,我有第二张桌子来存储产品的价格。该表是sh_product_service_prices。
与sh_product_service_prices 同时更新sh_product table 的最佳方式是什么?
所有用户输入都在同一页面上,但我不会选择修改第一个类,而是创建另一个扩展 Product 类的类。
sh_product_service_prices 的字段结构如下:
id | idp (product id) | price
我想使用update_product() 程序,但在该程序中还要添加价格信息或类似的东西......
但作为第二个问题,当我从 Product 类调用更新过程时,我希望更新所有字段。
有人可以帮我提供一个不需要更改头等舱的解决方案吗?
【问题讨论】:
-
我对最后两段感到困惑。你说你想使用
Product::update_product()来做额外的操作,但是你要求一个不需要改变第一类的解决方案(我假设你的意思是Product)。我认为不改变它就不可能让班级做得更多:/ -
我在问是否可以有一个解决方案来提供第二类,假设 product_price 的 update_product() 仅包含 sh_product_service_prices 的更新 .. 可以那样做吗?