Skip to content

Commit a6e84f1

Browse files
committed
changes
1 parent 2dbc038 commit a6e84f1

File tree

3 files changed

+76
-32
lines changed

3 files changed

+76
-32
lines changed

Join.php

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
class Join
55
{
6-
public $join_type, $primary_field, $table, $alias, $foreign_field, $fields = array();
6+
public $join_type, $primary_field, $table, $alias, $foreign_field, $wh, $fields = array(), $joins = array();
77

88
public function __construct($join_type, $primary_field, $table, $alias, $foreign_field)
99
{
@@ -19,6 +19,18 @@ public static function init($join_type, $primary_field, $table, $alias, $foreign
1919
return new Join($join_type, $primary_field, $table, $alias, $foreign_field);
2020
}
2121

22+
public function join(Join $join)
23+
{
24+
$this->joins[] = $join;
25+
return $this;
26+
}
27+
28+
public function noField()
29+
{
30+
$this->fields = null;
31+
return $this;
32+
}
33+
2234
public function field($field, $alias = "")
2335
{
2436
if (!$alias)
@@ -35,16 +47,54 @@ public function getFields()
3547
$fields = array();
3648

3749
$table_alias = $this->alias ? $this->alias : $this->table;
38-
foreach($this->fields as $ailas => $field)
50+
51+
if (is_array($this->fields))
3952
{
40-
$fields[] = $table_alias . "." . $field . " AS " . $table_alias . "__" . $ailas;
53+
if (empty($this->fields))
54+
{
55+
$fields[] = "$table_alias.*";
56+
}
57+
else
58+
{
59+
foreach($this->fields as $ailas => $field)
60+
{
61+
$fields[] = $table_alias . "." . $field . " AS " . $table_alias . "__" . $ailas;
62+
}
63+
}
4164
}
4265

43-
if (!$fields)
66+
foreach($this->joins as $join)
4467
{
45-
$fields[] = "$table_alias.*";
68+
$fields = array_merge($fields, $join->getFields());
4669
}
4770

4871
return $fields;
4972
}
73+
74+
public function setWhere(Where $wh)
75+
{
76+
$this->wh = $wh;
77+
return $this;
78+
}
79+
80+
public function get($table_alias)
81+
{
82+
if (is_null($this->wh))
83+
{
84+
$this->wh = new Where("AND");
85+
}
86+
87+
$join_table_alias = $this->alias ? $this->alias : $this->table;
88+
89+
$this->wh->add($table_alias . "." . $this->primary_field, $join_table_alias . "." . $this->foreign_field , "=", "");
90+
91+
$q = $this->join_type . " " . $this->table . " AS " . $join_table_alias . " ON " . $this->wh->get();
92+
93+
foreach($this->joins as $join)
94+
{
95+
$q .= $join->get($join_table_alias);
96+
}
97+
98+
return $q;
99+
}
50100
}

Query.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,11 @@ public function get()
4747
if (!$fields)
4848
{
4949
$fields[] = "$table_alias.*";
50-
5150
}
5251

5352
foreach($this->joins as $join)
5453
{
55-
$fields = array_merge($fields, $join["join"]->getFields());
54+
$fields = array_merge($fields, $join->getFields());
5655
}
5756

5857
$fields = implode(", ", $fields);
@@ -61,7 +60,7 @@ public function get()
6160

6261
foreach($this->joins as $join)
6362
{
64-
$str = $this->getJoin($join["join"], $join["where"]);
63+
$str = $join->get($table_alias);
6564

6665
if ($str)
6766
{
@@ -90,30 +89,10 @@ public function get()
9089
return $q . $wh . $order . ";";
9190
}
9291

93-
public function join(Join $join, Where $wh = null)
92+
public function join(Join $join)
9493
{
95-
$this->joins[] = array(
96-
"join" => $join,
97-
"where" => $wh
98-
);
94+
$this->joins[] = $join;
9995

10096
return $this;
10197
}
102-
103-
public function getJoin(Join $join, Where $wh = null)
104-
{
105-
if (is_null($wh))
106-
{
107-
$wh = new Where("AND");
108-
}
109-
110-
$table_alias = $this->alias ? $this->alias : $this->table;
111-
$other_table_alias = $join->alias ? $join->alias : $join->table;
112-
113-
$wh->add($table_alias . "." . $join->primary_field, $other_table_alias . "." . $join->foreign_field , "=", "");
114-
115-
$q = $join->join_type . " " . $join->table . " AS " . $other_table_alias . " ON " . $wh->get();
116-
117-
return $q;
118-
}
11998
}

test.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,24 @@
33
require_once './Where.php';
44
require_once './Join.php';
55

6-
$qb = new \QueryBuilder\QuerySelect("orders");
6+
$qb = new \QueryBuilder\QuerySelect("legder_sales", "Legder");
77

88
$qb->field("id");
9-
$qb->join(\QueryBuilder\Join::init("INNER JOIN", "id", "order_details", "OD", "order_id")->field("product_id")->field);
9+
$qb->join(\QueryBuilder\Join::init("INNER JOIN", "legder_voucher_type_id", "legder_voucher_types", "LegderVoucherType", "id"));
10+
11+
$legder_detail_join = \QueryBuilder\Join::init("LEFT JOIN", "id", "legder_sale_details", "LegderDetail", "legder_sale_id")->noField();
12+
13+
$product_join = \QueryBuilder\Join::init("LEFT JOIN", "product_id", "products", "Product", "id");
14+
$product_join->join(
15+
\QueryBuilder\Join::init("LEFT JOIN", "id", "product_files", "ProductFile", "product_id")->noField()
16+
->join(\QueryBuilder\Join::init("LEFT JOIN", "image_id", "images", "ProductFileImage", "id"))
17+
);
18+
19+
$product_join->join(\QueryBuilder\Join::init("LEFT JOIN", "category_id", "categories", "Category", "id")->field("name"));
20+
21+
$legder_detail_join->join($product_join);
22+
$legder_detail_join->join(\QueryBuilder\Join::init("LEFT JOIN", "item_id", "items", "Item", "id"));
23+
24+
$qb->join($legder_detail_join);
1025

1126
echo $qb->get();

0 commit comments

Comments
 (0)