33
44class 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}
0 commit comments