Using field value in WHERE statement on ->options
Using field value in WHERE statement on ->options
Lennart Oester
Posts: 12Questions: 2Answers: 1
in Editor
In my SQL file I have a huge problem using the value of the row in a where clause.
The hardcoded value 8069875 is present in the projects_keywords_elements.related_keywords
if I use it like this it works
Field::inst( 'projects_keywords_elements.related_keywords' )
->options( Options::inst()
->table( $_SESSION['project_keywords_database'] . '.related_searches' )
->value( 'id' )
->label( 'title' )
->where( function ($q) {
$q->and_where($_SESSION['project_keywords_database'] . '.related_searches.keyword_id', '8069875', '=');
} )
)
->validator( Validate::dbValues() ),
But if I do this as I would expect to work I get empty result
Field::inst( 'projects_keywords_elements.related_keywords' )
->options( Options::inst()
->table( $_SESSION['project_keywords_database'] . '.related_searches' )
->value( 'id' )
->label( 'title' )
->where( function ($q) {
$q->and_where($_SESSION['project_keywords_database'] . '.related_searches.keyword_id', 'projects_keywords_elements.related_keywords', '=');
} )
)
->validator( Validate::dbValues() ),
Whats the trick to use value from the db in the where clause
Answers
If I add the ,false param then I get an error stating that the column i get the following error
even if i prefix the table with the database name since I join on differentdatabases
I don't actually see where
projects_keywords_elements.keyword'
is defined in the code you show?However, line 7 in the second block of code probably isn't doing what you want case Editor's PHP libraries will automatically "bind" the value given, in order to prevent SQL injection attacks. You can pass an optional fourth parameter in order to have it not do the binding, and thus let it treat the value given as an SQL statement (i.e. a column name).
The error you are getting isn't quite what I'd expect from that being the underlying issue, but from the code above I don't see where the error is coming from at all. There is no "keyword" column referenced on its own.
Allan
Heres all the code
Can you give anexample as I cannot find anything online
Thanks for the full code. So taking this field:
Results in the error:
<?php ! ?>That is bizarre!
Could you send me the full JSON response from the server when that error happens please?
Thanks,
Allan
Yes. Thats the error I get.
This is the full json
{"fieldErrors":[],"error":"An SQL error occurred: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'projects_keywords_elements.related_keywords' in 'where clause'","data":[],"ipOpts":[],"cancelled":[]}
Can this be related to the fact that I use two different databasen in this ?
I set up debug as well.
Heres the full code for this
I solved it by doing this
$q->where($_SESSION['project_keywords_database'] . '.related_searches.keyword_id', '(SELECT DISTINCT keyword FROM admin_sbn.projects_keywords_elements WHERE project_id = '.$_SESSION['project_keywords_projectid'].')', 'IN', false);
No did not, because it selects to much for project ID. I need the row id/keyword Id
I simply cant get it to work.
My last idea is to set the keyword in a session var when I click the table and then use it like normal.
But I cant get it to register the click everywhere on the table when using inline edit, which is a must
Last hope - no dice
I tried this in hope that even tho9ugh it gets set before the inline edit, its not soon enough for the sql code
I solved it using a reload. Not the way I wanted as it now requires an extra click to activate the inline edit but, its usable
It looks like the error being given isn't the same as the one from the original post. It now says:
Before it was about
projects_keywords_elements.keyword
.This is the SQL statement that is failing:
Is it missing a join? Do you need to
leftJoin()
to theprojects_keywords_elements
table? You can certainly do that, but then the list of options would be different for every row. That is not something that Editor's PHP libraries currently support out of the box. It can be done with an Ajax call on eachinitEdit
to get the list of options for the row being edited though - that is how I normally approach such a case.Allan