php在OOP中处理许多订单

时间:2015-12-15 16:03:17

标签: php oop collections

尝试学习OOP(最后),但努力处理多个项目,例如客户订单,订单订单行等。据我了解OOP我不应该在对象中创建对象,我不应该访问数据直接,只有通过setter和getter,我不应该重复方法,我应该保持类完整性/封装。

Ex:简单的订单类,获取,添加,插入订单没有问题。为了处理许多订单,我尝试了不同的方法。

1)最简单的方法是在Order中为客户返回所有orderid,然后在脚本中创建一个循环,为每个orderid创建订单对象。不好,因为它多次调用数据库。每个对象一个。

2)接下来是让相同的方法为客户的所有订单返回所有orderdata,并在循环中创建Order对象,而不必从数据库获取数据。这个问题是它似乎不正确,因为我然后直接处理脚本中的数据而不是通过getter和setter处理。

3)接下来,我将许多订单的处理移到了自己的班级。我试图创建一个OrderCollection类,并将订单集合视为一个对象。似乎使用它时ArrayIterator等有优势。但我发现没有办法以这种方式解决它,因为我希望它们都是Order对象并在它们中使用Order Class中的方法。

这似乎是一个基本的东西,我很惊讶,花了几天时间搜索和阅读教程和论坛却一无所获。 因此,请指出我在正确的方向上如何在OOP中处理它时多个项目。我想也是这次我在完全错误的方向思考,但请保持温和,我是新手。

脚本:

// test data
$customerid = 1001;
$orderid = 2233;

// db
$dbh = new DB();

// This works fine
// Order
$order = new Order($dbh);
$order-set_orderid($orderid);
$order->select_order();
echo "orderdate: ".$order-get_date();

// This is were I have problems
// Orders
$customerorders = new OrderCollection($dbh);
$customerorders->select_all_orders_for_customerid($customerid);

// now I can iterate through the customerorders:
foreach ($customerorders as $orderrow)
{
    echo $orderrow->orderid . ", ";
    echo $orderrow->orderdate . ", ";
    echo $orderrow->ordervalue."<br />";
}
// but this seems not correct since I am now handling the data directly from database and not via getters
// also I cant use any other Order method on these since they are not Order objects

订单类

    class Order
{
    private $order;
    private $orderdate;
    private $ordervalue;
    private $db;
    public function __construct($db_handle)
    {
        $this->db = $db_handle;
    }
    public function select_order()
    {
        try 
        {
            $stmt = $this->db->con->prepare("SELECT * FROM order WHERE orderid=:orderid");
            $stmt->bindParam(':orderid', $this->orderid, PDO::PARAM_INT);
            $stmt->execute();
            $order_row = $stmt->fetch(PDO::FETCH_ASSOC);

            $this->orderdate = $order_row['orderdate'];
            $this->ordervalue = $order_row['ordervalue'];
        catch(PDOException $e)
        {
            // ...
        }
    }
    public function select_all_orders_for_customerid()
    {
        // get all orderdata from database for customerid and return an array of orders with all data

    }
    // update, insert, delete methods ... in here

    // getters
    public function get_orderid() { return $this->orderid;  }
    public function get_sorderdate() {  return $this->orderdate;    }
    public function get_ordervalue() {  return $this->ordervalue;   }
    // setters
    public function set_orderid($new_servid) { $this->orderid = $new_orderid; }
    public function set_orderdate($new_orderdate) { $this->orderdate = $new_orderdate; }
    public function set_ordervalue($new_ordervalue) { $this->ordervalue = $new_ordervalue; }
}

订单收集类

class OrderCollection implements IteratorAggregate
{
    private $collection = array();
    private $db;

    public function __construct($db_handle)
    {
        $this->db = $db_handle;
    }

    public function select_all_orders_for_customerid($customerid)
    {
        try 
        {
            $stmt = $this->db->con->prepare("SELECT * FROM order WHERE customerid=:customerid");
            $stmt->bindParam(':customerid', $this->customerid, PDO::PARAM_INT);
            $stmt->execute();
            $this->collection = $stmt->fetchAll(PDO::FETCH_OBJ);
        }
        catch(PDOException $e)
        {
            // ...
        }
    }

    public function getIterator()
    {
        return new ArrayIterator($this->collection);
    }
}

0 个答案:

没有答案