If I hardwire a created_by
clause in a joomla sql custom field, the query works as it should (for this specific userid the clause is, and created_by = 305
):
SELECT id AS value, title AS text
FROM #__content
where catid = 8 and created_by = 305
What I want to do is allow the created_by = ???
clause to be dynamic (substituted with JFactory::getUser()->id
;
I figured if I make the custom SQL like this:
SELECT id AS value, title AS text
FROM #__content
where catid = 8 and created_by = %%userid%%
I could/should be able to replace the token %%userid%%
with a dynamic number before the query is used.
I've tried bending the existing custom sql field - just to see if it will work in principle, but I'm not having too much luck.
In plugins/fields/sql/tmpl/sql.php I have:
$query = $fieldParams->get('query', '');
if (substr_count($value,"%%userid%%") > 0){
$userid = JFactory::getUser()->id;
str_replace("%%userid%%", $userid, $query);
}
// Run the query with a having condition because it supports aliases
$db->setQuery($query . ' having value in (' . trim($condition, ',') . ')');
Ideally, I would create a brand new custom field, and be happy to donate it to the world!
I'm not even having much fun in trying to debug this!
@mickmacusa and @Thiago, when my sql statement has the token %%userid%%, the str_replace("%%userid%%", $userid, $query);
simply does not happen.
I've tried simplifying the code so that it's NOT EVEN looking for my token - it's supposed to just do it:
$db->setQuery($query . ' having value in (' . trim($condition, ',') . ')');
So right now, I think that I've not understood how the supplied sql field actually works.
Absolutely fails... The Query ignores absolutely anything I try...
Even this :
$query = $fieldParams->get('query', '');
$db->setQuery('SELECT mince as value ,potatoes_oinions_peas as text FROM #__marketstall WHERE catid = 8');
Which ought to cause a MAJOR fit when the query is being executed, since it is a totally nonsense query...
But what actually happens is it executes the ORIGINAL sql statement go figure.
So there's 3 things:
Either I'm editing the wrong bit of code.
Something is being MASSIVELY cached.
Or it seems to me as though the SQL statement that is 'defined' by the developer is getting stored somewhere else, and the programmatic change of the statement via the plugin code is being utterly ignored, somehow.
I just don't know enough about how these are supposed to work. I really didn't think it would be this difficult.
$query = $fieldParams->get('query', ''); // Run the query with a having condition because it supports aliases $db->setQuery($query . ' AND created_by =' . $db->q( 305 ));
It doesn't like double spaces at the end of a line then ;-)AND created_by = 305
, it works, but if I adapt the clause to becreated_by = %%userid%%
and I also have the substitution codeif (substr_count($value,"%%userid%%") > 0){ $userid = JFactory::getUser()->id; str_replace("%%userid%%", $userid, $query); }
the substitution does not happen, the debug database section tells me that the SQL statement is wrong (of course), and the picklist of items is not created.