1111use ipl \Orm \Resolver ;
1212use ipl \Orm \UnionModel ;
1313use ipl \Stdlib \Filter ;
14- use ipl \Stdlib \Seq ;
1514use ipl \Stdlib \Str ;
1615use ipl \Web \Control \SearchBar \SearchException ;
1716use ipl \Web \Control \SearchBar \Suggestions ;
@@ -45,7 +44,12 @@ protected function shouldShowRelationFor(string $column): bool
4544 {
4645 $ columns = Str::trimSplit ($ column , '. ' );
4746
48- return $ columns [0 ] !== $ this ->model ->getTableName ();
47+ switch (count ($ columns )) {
48+ case 2 :
49+ return $ columns [0 ] !== $ this ->model ->getTableName ();
50+ default :
51+ return true ;
52+ }
4953 }
5054
5155 protected function createQuickSearchFilter ($ searchTerm )
@@ -69,54 +73,40 @@ protected function fetchValueSuggestions($column, $searchTerm, Filter\Chain $sea
6973 $ query = $ model ::on ($ this ->getDb ());
7074 $ query ->limit (self ::DEFAULT_LIMIT );
7175
72- if (strpos ($ column , ' ' ) !== false ) {
73- // $column may be a label
74- list ($ path , $ _ ) = Seq::find (
75- self ::collectFilterColumns ($ query ->getModel (), $ query ->getResolver ()),
76- $ column ,
77- false
78- );
79-
80- if ($ path !== null ) {
81- $ column = $ path ;
82- }
83- }
84-
8576 $ columnPath = $ query ->getResolver ()->qualifyPath ($ column , $ model ->getTableName ());
8677 $ inputFilter = Filter::like ($ columnPath , $ searchTerm );
8778
88- $ query ->columns ($ columnPath );
79+ $ query ->columns ([ $ columnPath] );
8980 $ query ->orderBy ($ columnPath );
9081
9182 if ($ searchFilter instanceof Filter \None) {
9283 $ query ->filter ($ inputFilter );
9384 } elseif ($ searchFilter instanceof Filter \All) {
9485 $ searchFilter ->add ($ inputFilter );
95-
96- $ searchFilter ->metaData ()->set ('forceOptimization ' , true );
97- $ inputFilter ->metaData ()->set ('forceOptimization ' , false );
9886 } else {
9987 $ searchFilter = $ inputFilter ;
10088 }
10189
10290 $ query ->filter ($ searchFilter );
91+ // Not to suggest something like Port=443,443,443....
92+ $ query ->getSelectBase ()->distinct ();
10393
10494 try {
10595 foreach ($ query as $ item ) {
106- try {
107- $ columns = Str::trimSplit ($ column , '. ' );
108- if ($ columns [0 ] === $ model ->getTableName ()) {
109- array_shift ($ columns );
110- }
96+ $ columns = Str::trimSplit ($ column , '. ' );
97+ if ($ columns [0 ] === $ model ->getTableName ()) {
98+ array_shift ($ columns );
99+ }
111100
112- $ value = null ;
101+ $ value = $ item ;
113102
103+ try {
114104 do {
115105 $ col = array_shift ($ columns );
116- $ value = $ item ->$ col ;
106+ $ value = $ value ->$ col ;
117107 } while (! empty ($ columns ));
118108
119- if ($ value !== '' && ! ctype_print ($ value )) { // Is binary
109+ if ($ value && ! ctype_print ($ value )) { // Is binary
120110 $ value = sprintf ('\\x%s ' , bin2hex ($ value ));
121111 }
122112
@@ -171,7 +161,7 @@ protected static function collectRelations(Resolver $resolver, Model $subject, a
171161 $ relationPath = [$ name ];
172162
173163 if (! isset ($ models [$ name ]) && ! in_array ($ name , $ path , true )) {
174- if ($ isHasOne ) {
164+ if ($ isHasOne || empty ( $ path ) ) {
175165 array_unshift ($ relationPath , $ subject ->getTableName ());
176166 }
177167
@@ -183,6 +173,8 @@ protected static function collectRelations(Resolver $resolver, Model $subject, a
183173 self ::collectRelations ($ resolver , $ relation ->getTarget (), $ models , $ relationPath );
184174 return ;
185175 }
176+ } else {
177+ $ path = [];
186178 }
187179 }
188180 }
0 commit comments