尝试使用singletons模式为多个查询建立多个DB连接


Trying to make multiple DB connections with singletons pattern for multiple queries

我正在尝试从一个类中执行多个查询。每个对象数组都返回到一个私有_Data变量。

问题是,当我执行新查询时,_datavar会被新数据覆盖。解决这种情况的最佳方法是什么?

    public function getDeliveryBy($orderid) {
//ORDER CLASS
    $data = $this->_db->query("SELECT deliveryby FROM order WHERE orderid = $orderid");
    //$data = $this->_db->get('rt_order', array('orderid', '=', $orderid));
    if ($data->count()) {
        $this->_data = $data->first();
        //this returns the orderline...
        $return = '';
        $return .= '<select id="status" class="form-control" name="status">';
        //if in orderline the user is 0 set not set
        if ($this->data()->deliveryby === "0") {
            $return .= '<option value="0" selected>Not set</option>';
        }
        // Now i want to get a list of all employees...
        // get all employees
        //$result = $this->_db->query("SELECT id,username FROM users");
        foreach () {
        //make... mutiple options
        }

        $return .= '</select>';
        return $return;
    }
    return false;
}

////更新我做了什么

  1. 在类顺序构造函数(用于用户类)中创建了一个新实例

    public function __construct() {
    $this->_db = DB::getInstance();
    $this->_employee = new User();
    

    }

  2. 在方法中添加此项

            //Get all employees
        $this->employee()->getAllUsers();
        foreach ($this->employee()->data() as $key=>$employee) {
            echo $employee->firstname, '<BR>';
        }
    

现在私有var数据仍然保存订单数据。。。并且用户数据保存用户数据。。。

问题是,我读到从另一个类中实例化一个新类是不好的。。。这就是为什么我想知道这是否可以

我做了什么

  1. 在类顺序构造函数(用于用户类)中创建了一个新实例

    public function __construct() {
    $this->_db = DB::getInstance();
    $this->_employee = new User();
    

    }

  2. 在方法中添加此项

            //Get all employees
        $this->employee()->getAllUsers();
        foreach ($this->employee()->data() as $key=>$employee) {
            echo $employee->firstname, '<BR>';
        }
    

现在私有var数据仍然保存订单数据。。。并且用户数据保存用户数据。。。

问题是,我读到从另一个类中实例化一个新类是不好的。。。s