⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Issue with search having same field names in with relation



Amit Shah
  • profile picture
  • Member

Posted 28 May 2013 - 06:19 AM

Hi there,

 

Today i was adding a custom search box in flexgrid and i sucessfully did but have had this small problem with it. When i tried searching the text ... it started throwing error - ambigious field names. So to overcome the same i altered some part of the code and that 1 worked like a charm.

	protected function set_ajax_list_queries($state_info = null)
	{
		if(!empty($state_info->per_page))
		{
			if(empty($state_info->page) || !is_numeric($state_info->page) )
				$this->limit($state_info->per_page);
			else
			{
				$limit_page = ( ($state_info->page-1) * $state_info->per_page );
				$this->limit($state_info->per_page, $limit_page);
			}
		}

		if(!empty($state_info->order_by))
		{
			$this->order_by($state_info->order_by[0],$state_info->order_by[1]);
		}

		if(!empty($state_info->search))
		{
			if(!empty($this->relation))
				foreach($this->relation as $relation_name => $relation_values)
					$temp_relation[$this->_unique_field_name($relation_name)] = $this->_get_field_names_to_search($relation_values);
			
			if($state_info->search->field !== null)
			{
				if(isset($temp_relation[$state_info->search->field]))
				{
					if(is_array($temp_relation[$state_info->search->field]))
						foreach($temp_relation[$state_info->search->field] as $search_field)
							$this->or_like($search_field , $state_info->search->text);
					else
						$this->like($temp_relation[$state_info->search->field] , $state_info->search->text);
				}
				elseif(isset($this->relation_n_n[$state_info->search->field]))
				{
					$escaped_text = $this->basic_model->escape_str($state_info->search->text);
					$this->having($state_info->search->field." LIKE '%".$escaped_text."%'");
				}
				else
				{
					$this->like($state_info->search->field , $state_info->search->text);
				}
			}
			else
			{
				$columns = $this->get_columns();

				$search_text = $state_info->search->text;

				if(!empty($this->where))
					foreach($this->where as $where)
						$this->basic_model->having($where[0],$where[1],$where[2]);

				foreach($columns as $column)
				{
					if(isset($temp_relation[$column->field_name]))
					{
						if(is_array($temp_relation[$column->field_name]))
						{
							foreach($temp_relation[$column->field_name] as $search_field)
							{
								$this->or_like($search_field, $search_text);
							}
						}
						else
						{
							$this->or_like($temp_relation[$column->field_name], $search_text);
						}
					}
					elseif(isset($this->relation_n_n[$column->field_name]))
					{
						//@todo have a where for the relation_n_n statement
					}
					else
					{
						$this->or_like($this->basic_db_table . "." . $column->field_name, $search_text);
					}
				}
			}
		}
	}
    protected function _get_field_names_to_search(array $relation_values)
    {
        if(!strstr($relation_values[2],'{'))
            return $this->_unique_join_name($relation_values[0]).'.'.$relation_values[2];
        else
        {
            $relation_values[2] = ' '.$relation_values[2].' ';
            $temp1 = explode('{',$relation_values[2]);
            unset($temp1[0]);

            $field_names_array = array();
            foreach($temp1 as $field)
                list($field_names_array[]) = explode('}',$field);
            for($i=0; $i < count($field_names_array); $i++) {
                $field_names_array[$i] = $this->_unique_join_name($relation_values[0]).'.'. $field_names_array[$i];
            }
            return $field_names_array;
        }
    }

just altered / added the code which are bold / italic and underlined. Think this is worth a peice of code to add it in the new library itself to fix this search issue.

 

Hope this helps out a lot of other GC Users like me.

 

Happy GCing.

 

 


cdaganache
  • profile picture
  • Member

Posted 21 June 2015 - 02:39 AM

Hi Sir Amit,

 

Thanks because this post help me to solve the same issue. You are really genius man.. =')

Now i'm stuck after i move my application folder from xampp server to wamp server, my application can't login even though i change login password to enter my application in database. For your information i'm using xampp 3.2.1 and the server using wamp 1.7.4. Please advise me sir. :unsure:  :)