Skip to content

Commit 5e193ce

Browse files
committed
Added copy() method for properties sharing
1 parent dcb8d3c commit 5e193ce

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

MysqliDb.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,8 @@ protected function _prepareQuery()
769769
public function __destruct()
770770
{
771771
if (!$this->isSubQuery)
772+
return;
773+
if ($this->_mysqli)
772774
$this->_mysqli->close();
773775
}
774776

@@ -931,4 +933,14 @@ public static function subQuery()
931933
return new MysqliDb();
932934
}
933935

936+
/**
937+
* Method returns a copy of a mysqlidb subquery object
938+
*
939+
* @param object new mysqlidb object
940+
*/
941+
public function copy ()
942+
{
943+
return clone $this;
944+
}
945+
934946
} // END class

readme.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,20 @@ $products = $db->get ("products p", null, "u.name, p.productName");
220220
print_r ($products);
221221
```
222222

223+
### Properties sharing
224+
Its is also possible to copy properties
225+
```php
226+
$db->where ("agentId", 10);
227+
228+
$customers = $common->copy ();
229+
$res = $customers->get ("customers");
230+
// SELECT * FROM customers where agentId = 10
231+
232+
$db->orWhere ("agentId", 20);
233+
$res = $db->get ("users");
234+
// SELECT * FROM users where agentId = 10 or agentId = 20
235+
```
236+
223237
### Subqueries
224238
Subquery in selects:
225239
```php
@@ -246,7 +260,6 @@ $data = Array (
246260
$id = $db->insert ("products", $data);
247261
// Gives INSERT INTO PRODUCTS (productName, userId, lastUpdated) values ("test product", (SELECT name FROM users WHERE id = 6), NOW());
248262
```
249-
250263
### Helper commands
251264
Reconnect in case mysql connection died
252265
```php

tests.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,9 @@ function createTable ($name, $data) {
228228
$usersQ->where ("login", "user2");
229229
$usersQ->getOne ("users", "id");
230230

231-
$db->where ("userId", $usersQ);
232-
$res = $db->getOne ("products", "count(id) as cnt");
231+
$db2 = $db->copy();
232+
$db2->where ("userId", $usersQ);
233+
$res = $db2->getOne ("products", "count(id) as cnt");
233234
if ($res['cnt'] != 2) {
234235
echo "Invalid select result with subquery";
235236
exit;

0 commit comments

Comments
 (0)