Magic Select From

My model (MAP) , controller, search and index works brilliantly when I use it normally.

The index is presented based on the query:


SELECT `en_map_table`.* FROM `mxp_bcmp_en_map` `en_map_table` LEFT JOIN `mxp_bcmp_de_map` `language_map_table` ON `en_map_table



Now I want to call my search function from another model to get the count for that same query

In this Overviewmodel I do


        $Map = new Map;

        echo $Map ->getNew();

in the getNew I do:




$SearchParam['id'] ='';

$ar['MapSearch'] = $SearchParam;

$Data = $Search->search($ar); 



This works brilliantly

However when I do




$SearchParam['statusAttribute']= 2;

       $ar['MapSearch'] = $SearchParam;

       $Data = $Search->search($ar);                                                                                                    



Then the sql is invalid




SELECT COUNT(*) FROM `mxp_bcmp_de_map` LEFT JOIN `mxp_bcmp_de_map` `language_map_table` ON `mxp_bcmp_de_map`.`id` = `language_map_table`.`i



The reason for this is clear : mxp_bcmp_en_map is missing in the query

But I have no clue why that is missing.

I assume it is somewhere magicaly assigned. Any clues on what I am doing wrong ?

I’m sorry, but I couldn’t understand what you are trying to do in your Map model. Could you please show us the relevant codes?

hihi… I realized when I read it that it could be a bit confusing.

Overall goal:

I want a page that shows the count of certain select criteria (status)

ie: Map - 4 records (with status 2)

For this I thought I can use the search function that was already implemented in Map

(when I show the index page, the grid view has this filter on "status =2"

So my idea was to call that search function with the same parameters as if I were triggering it through the index page by selecting the status manually.

To hunt down the problem I am now using

Mapcontroller:Viewaction:


     public function actionView($id)

    {

        $model = $this->findModel($id); 

        

        $searchModel = new MapSearch();

        $SearchParam['statusAttribute']= 2;

        $ar['MapSearch'] = $SearchParam;

        $Data = $searchModel->search($ar);    


        

        echo $Data->getTotalCount();  

This code fires the exception of a missing table : Unknown column ‘en_map_table.shock’ in ‘where clause’


SELECT COUNT(*) FROM `mxp_bcmp_de_map` LEFT JOIN `mxp_bcmp_de_map` `language_map_table` ON `mxp_bcmp_de_map`.

However when I go to the indexpage and select the filter manually

then the code returns the correct data

and when I look at the db calls it did include that missing table (en_map_table)


SELECT COUNT(*) FROM `mxp_bcmp_en_map` `en_map_table` LEFT JOIN `mxp_bcmp_de_map` `language_map_table` ON `en_map_table`.`id` = `language_map_table`.`id` LEFT JOIN `mxp_generic_itemstatus` 

I hope I made it a bit clearer

**SOLVED **

Finally I solved it…

It had to do with the query builder.

Because I was directly calling the search, the model was differetly initialized.

Copying the from statements from the actionIndex did the trick.

Sometimes but rephrasing,… and never giving up… a solution comes to mind…

Good to hear that you solved it, although I still don’t understand what you are doing … ;)

There’s one thing I’m worrying about. Do you set the “from” part of the query outside of the search method before you call it? IMO it’s not a good design.

I first call the search




  $dataProvider = $searchModel->search;

$query = $dataProvider->query;

$query->from(.....

$dataProvider->getTotalCount();    



I do not like the design either, but it is a pretty complex query with lots of tables being in the join

You should at least exclude the part that causes "unknown column" error from the search method.

That part should be injected together with the "from" part on which it depends.