This topic is locked
[SOLVED]

 Dynamically add where clause to the search suggest: reprise

6/19/2012 4:46:17 PM
PHPRunner General questions
W
wpl author

Hi All,
in a post dating from September 2010
Dynamically add where clause..
DaleM described a solution for this task which I doesn't seem to work with the latest build of PHPRunner 6.1. Or did I misconceive something? Any hints?
Thanks

C
cgphp 6/19/2012

Did you try the FunkDaddy solution (last answer in the post)?

W
wpl author 6/20/2012



Did you try the FunkDaddy solution (last answer in the post)?


Cristian,
thanks, but this approach would be too static. It should not work on all tables and should (ideally) not require manual changes to generated files.

J
Jane 6/20/2012

I recommend you to have a look at this article:

http://xlinesoft.com/phprunner/docs/addwhere.htm

W
wpl author 6/20/2012



I recommend you to have a look at this article:

http://xlinesoft.com/phprunner/docs/addwhere.htm


Jane,
I have been using this function on various occasions already. It works if you call the table from menu or directly via its url. It does not work in a case where the table is a detail table of a master that has more details and some JOINed tables. Calling the master table will throw an error.
Any further idea?

J
Jane 6/21/2012

Could you publish your project on Demo Account and open a ticket at http://support.xlinesoft.com sending a URL to your pages along with instructions on reproducing this issue?

W
wpl author 6/22/2012



Could you publish your project on Demo Account and open a ticket at http://support.xlinesoft.com sending a URL to your pages along with instructions on reproducing this issue?



Jane,
the project is too big and too complex to publish. But I will try to desrcibe better what's happening. Let's say I have two tables

tableMaster and tableDetail, each of which has (among others) three fields id1, id2, id3 which in each case are joined to

the tables tableId1, tableId2, tableId3 which also contain the fields id1, id2, id3.

In tableDetail I would like to limit the results of searchsuggest.php in Quicksearch to the records the list page is limited to in

the BeforeQueryList event using whereAdd. So I used the following code in the AfterTableInitialized event of tableDetail:
$query->addWhere("tableDetail.id1=".$_SESSION["id1"]);
I had to qualify the fieldname with the tablename because id1 is also contained in tableId1, tableId2, and tableId3) which are joined to tableDetail.
This will work fine, but, as there will be more fields (id2, id3) and more conditions to be considered, and there are several

tables following this pattern, I wanted to code it in a more generalized way like so:
global $strTableName;
$query->addWhere($strTableName . "." . "id1=" . $_SESSION["myuserid"]);
This will work when tableDetail is called directly from menu or via url. However, if tableMaster is called (and from here, AfterTableInitialized

of tableDetail will be called also), there will be an error because $strTableName in the AfterTableInitialized event of tableDetail will contain

the name 'tableMaster' but addWhere will append the condition to the SQL string of tableDetail so that the query reads:
...from tableDetail1 where tableMaster.id1=....
and there will be an error "missing entry 'tableMaster' in FROM clause..".
Any hint to cope with this? I would really like to avoid hard coding the name of the table in each AfterTableInitialized event.
Thanks

W
wpl author 6/26/2012



Jane,
the project is too big and too complex to publish. But I will try to desrcibe better what's happening. Let's say I have two tables

tableMaster and tableDetail, each of which has (among others) three fields id1, id2, id3 which in each case are joined to

the tables tableId1, tableId2, tableId3 which also contain the fields id1, id2, id3.

In tableDetail I would like to limit the results of searchsuggest.php in Quicksearch to the records the list page is limited to in

the BeforeQueryList event using whereAdd. So I used the following code in the AfterTableInitialized event of tableDetail:
$query->addWhere("tableDetail.id1=".$_SESSION["id1"]);
I had to qualify the fieldname with the tablename because id1 is also contained in tableId1, tableId2, and tableId3) which are joined to tableDetail.
This will work fine, but, as there will be more fields (id2, id3) and more conditions to be considered, and there are several

tables following this pattern, I wanted to code it in a more generalized way like so:
global $strTableName;
$query->addWhere($strTableName . "." . "id1=" . $_SESSION["myuserid"]);
This will work when tableDetail is called directly from menu or via url. However, if tableMaster is called (and from here, AfterTableInitialized

of tableDetail will be called also), there will be an error because $strTableName in the AfterTableInitialized event of tableDetail will contain

the name 'tableMaster' but addWhere will append the condition to the SQL string of tableDetail so that the query reads:
...from tableDetail1 where tableMaster.id1=....
and there will be an error "missing entry 'tableMaster' in FROM clause..".
Any hint to cope with this? I would really like to avoid hard coding the name of the table in each AfterTableInitialized event.
Thanks


Hello,
really no help available? It would suffice to know how one can distinguish whether "AfterTableInit" of a detail table is called directly (menu or url) or from the master table.
Thanks

Sergey Kornilov admin 6/27/2012

I'm not sure how we can help without having access to your files and database.

W
wpl author 6/27/2012



I'm not sure how we can help without having access to your files and database.


Thanks,
since I flattened this issue down to a simple question I thought there would be a simple answer as well.Sorry for bothering you.

Sergey Kornilov admin 6/27/2012

If I understand your question right you can find what's the current URL is (master or details table) and apply this logic in your code.

W
wpl author 6/29/2012



If I understand your question right you can find what's the current URL is (master or details table) and apply this logic in your code.


Sergey,
thanks, we are almost there. The point is, however, that the AfterTableInit events of detail tables will be called immediately after the AfterTableInit

event of the master table (without clicking any of the detail links!). At this time, $strTableName in the AfterTableInit events of all detail tables

will contain the name of the master table. I echoed the names of the AfterTableInit events and $strTableName for three detail tables

(there are > 10 details in reality) in the respective events and that's what gets printed when master_list.php is called via URL:
AfterTableInit of: master

$strTableName: public.master
AfterTableInit of: detail1

$strTableName: public.master
AfterTableInit of: detail2

$strTableName: public.master
AfterTableInit of: detail3

$strTableName: public.master
If I click the detail link of table detail1 or call table detail1 directly this will read:
AfterTableInit of: detail1

$strTableName: public.detail1
If the option "show master info on detail" is chosen, this will be:
AfterTableInit of: detail1

$strTableName: public.detail1

AfterTableInit of: master

$strTableName: public.detail1
I had a look at $tdatapublic_detail and found that $tdatapublic_detail[".OriginalTable"] will contain always the correct name of the detail table, which is what I need. Will this be likely to change in the near future?
Thanks