TYPO3 Symfony Expression: isset() for Query Parameters? - symfony

I am using several Symfony expressions in my TypoScript checking for query parameters such as this:
[request.getQueryParams()['tx_news_pi1']['news'] > 0]
do something
[END]
This is working well – as long as the query parameter exists. If it doesn’t, the following error message is written into the log file:
Unable to get an item on a non-array.
In PHP I would use isset() to check whether the query parameter exists – but I could not find a similar way for Symfony expressions in TypoScript. I have tried
[request.getQueryParams()['tx_news_pi1']['news']]
which works the same, meaning: it does what it’s supposed to do, but logs an error message if the query parameter does not exist.
Is there anything like isset() for the Symfony Expression Language in TYPO3?

The is_defined() or isset() I was looking for will be returned by the condition
[request.getQueryParams()['tx_news_pi1']]
instead of
[request.getQueryParams()['tx_news_pi1']['news']]
In my use case this would even be enough. If you need to be more precise (e.g. to differentiate between different query parameters within the same plugin), go for
[request.getQueryParams()['tx_news_pi1'] && request.getQueryParams()['tx_news_pi1']['news'] > 0]
The solution was provided as a reply to a bug report on forge.typo3.org

Try this:
[request.getQueryParams()['tx_news_pi1']['news'] = ]
do something
[END]

Related

LINQ Breaking changes in EF Core 3.0. How can I compare strings without getting the warning CA1308?

I had the following code, which was running well with EF Core 2.1:
.FirstOrDefault(a => (a.Name.Equals(b, StringComparison.InvariantCultureIgnoreCase).
(Ok, running well means I got the right results even if it was being evaluated in the client side and I didn't know it).
I updagred to EF Core 3.0 and I didn't get any error, but this code was not giving the expected results.
I saw here a solution. I tried a.Name.ToLower() == b.ToLower() but then I got the the error:
Error CA1304 The behavior of 'string.ToLower()' could vary based on the current user's locale settings. Replace this call in 'MyFunction(string, string)' with a call to 'string.ToLower(CultureInfo)'
If I use a ToLower(CultureInfo.InvariantCulture) I get the message:
Error CA1308 In method 'MyFunction', replace the call to 'ToLower' with 'ToUpperInvariant'.
If I use ToUpperInvariant(), then I get the error (I'm already aware of the LINQ breaking changes in EF Core 3.0):
The LINQ expression (... all the expression...) could not be translated. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
So, I am the starting point.
Is there a way to both comply with CA1304 and run the query in the DB and not in the client side?
The solution, as PanagiotisKanavos commented, was to simply use a.Name == b. Easy and it works!

Error messages: result.rejectvalue 3rd argument

in the following code what is the point of the third argument to rejectValue ?
errors.rejectValue("descriptions", "second_lang_desc_required", new String[] { secondLang.getCode() }, null );
I have this in the message value : Description in second language of application required {0} but this is exactly waht I see in the JSP, no replacement
According to Spring doc the purpose of the third argument is to provide a string array of arguments for replacing vars in messages.
In other words and telling from your code example its exactly what you expected it to be.
I guess you already checked if secondLang.getCode() is different from null. If so please have a look at whether or not you are using the latest release of org.springframework.
It is working in 4.2.4 but I remember having had to use a workaround before switching to 4.2.4 (Yes, of course - a clever guy would check change history and would 'know' instead of assuming, but I never claimed to be one, did I?)

How do I test for an invalid WordPress WP_Query query?

I have a page template which displays a list of post summaries, depending on a user-defined custom query, which is supplied using the post_content, for example:
category_name=blog
This is then passed to WP_Query() which will return however many 'blog' posts there are.
However, if I pass a completely invalid query (or simply make a mistake) such as:
the rain in spain falls mainly on the plain
WP_Query->get_posts() will return ALL posts in the database, rather than letting me know that the query is meaningless / erroneous.
Is there a built-in method to test for invalid queries?
Currently, I'm doing a parse_str() to convert the query to an array, then an array_intersect_keys() against a list of valid query parameters I've compiled from the WP_Query page.
It works, but feels pretty hacky - am I missing something?
Thanks,
Dave
I don't believe there is a failed query catch built in to WP like you are asking for (I wish there were, too).
However, there may be a cleaner hack. wp_count_posts will return a count of all posts in the database. And wp_query will return a count of its results in $post_count. One thing you could do is a check on your query - if the result of wp_count_posts is equal to $post_count then you know something is wrong.
It's not as clean as a 'no results' response, but perhaps more straight forward than your current solution.
http://codex.wordpress.org/Function_Reference/wp_count_posts
http://codex.wordpress.org/Class_Reference/WP_Query (search the page for $post_count)

How to use GET and POST Arguments in Symfony2

We are transforming PHP Application to Symfony2 Application.
Most of the pages we are completely writing new but some pages we decided to keep it as it is. i.e I want to use the same php without any major change.
In the php page we used GET['prospect_id'], GET['executive_id'] and many other arguments. Both GET and Post methods. When I view the page in Symfony1.4 there is no error or warning.
But when I view in Symfony 2 I am getting undefined index error.
How can I solve the issue?
EDIT: if GET['prospect_id'] is null there is no error in Symfony 1.4 but i'm getting undefined index notice in Symfony2. There are many variables like that. Is it necessary to define variable before use it. How to avoid this notice message.
What i want is if i am using $_GET['xxx']. symfony2 should not show any notice or error. i want to escape from that.
Use (in Symfony2) the controllers request-object, to get those params:
$this->request->get('prospect_id');
$this->request->get('executive_id');
You can also set default values, if there is no value given. Take a look at this documentation.

XQuery Update: insert or replace depending if node exists not possible?

I am trying to build simple XML database (inside BaseX or eXist-db), but I have trouble figuring how to modify values in document :
content is simple as this for test :
<p>
<pl>
<id>6></id>
</pl>
</p>
I am trying to build something like function which would insert element into <pl> if that element is not present or replace it if it is present. But XQuery is giving me troubles yet :
When I try it head-on with if-then-else logic :
if (exists(/p/pl[id=6]/name)=false)
then insert node <name>thenname</name> into /p/pl[id=6]
else replace value of node /p/pl[id=6]/name with 'elsename'
I get error Error: [XUDY0027] Replace target must not be empty. Clearly I am confused, why the else part is evaluated in both cases, thus the error.
When I empty out the else part :
if (exists(/p/pl[id=6]/name)=true)
then insert node <name>thenname</name> into /p/pl[id=6]
else <dummy/>
Then I get Error: [XUST0001] If expression: no updating expression allowed.
When I try through declaring updating function, even then it reports error :
declare namespace testa='test';
declare updating function testa:bid($a, $b)
{
if (exists(/p/pl[id=6]/name)=true)
then insert node <name>thenname</name> into /p/pl[id=6]
else <dummy/>
};
testa:bid(0,0)
Error: [XUST0001] If expression: no updating expression allowed.
I've got these errors from BaseX 6.5.1 package.
So how can I modify values in a simple fashion if possible ?
If I call insert straight, the there could be multiple elements of same value.
If I call replace it will fail when the node does not exist.
If I delete the node before insert/replace then I could destroy sub-nodes which I don't want.
In most SQL databases, these are quite simple task (like MYSQL 'replace' command).
#Qiqi: #Alejandro is right. Your if expression is incorrect XQuery syntax:
if (exists(/p/pl[id=6]/name))
then insert node <name>thenname</name> into /p/pl[id=6]
else replace value of node /p/pl[id=6]/name with 'elsename'
Note that eXist-db's XQuery Update functionality is currently an eXist-specific implementation, so in eXist (currently) 1.4.x and 1.5dev, you'll want:
if (exists(/p/pl[id=6]/name))
then update insert <name>thenname</name> into /p/pl[id=6]
else update value /p/pl[id=6]/name with 'elsename'
This eXist-specific XQuery Update syntax is documented on http://exist-db.org/update_ext.html. This syntax was developed before the W3C XQuery Update spec had reached its current state. The eXist team plans to make eXist fully compliant with the W3C spec soon, but in the meantime the docs above should help you achieve what you need to if you use eXist.
Note too that your example code contains a typo inside the pl and id elements. The valid XML version would be:
<p>
<pl>
<id>6</id>
</pl>
</p>

Resources