How do I use SQL LEFT() in Symfony2 query builder? - symfony

Here's the line that's making my query fail.
$query = $query->where('a.field LIKE :keyword OR LEFT(a.otherfield, 3) = LEFT(:keyword, 3)');
I get this error:
[Syntax Error] line 0, col 104: Error: Expected known function, got 'LEFT'
This SQL code works:
SELECT * FROM `table`
WHERE field LIKE 'searchterm'
OR LEFT(`otherfield`, 3) = LEFT('searchterm', 3)
Why does LEFT() return an error? Is there a different way to do it with query builder?

Try to look at this question. Otherwise, try the old school way! native-sql-with-doctrine.

#Houssem Zitoun's answer was good, and I'm marking it as the answer because it's relevant to my question. It's not the answer I used however
I already had the query written in sql that I wanted to use, so i decided to use a custom query with doctrine. It explains here how to do it. Symfony2 & Doctrine: Create custom SQL-Query

Related

TYPO3 Symfony Expression: isset() for Query Parameters?

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]

Doctrine -Matching entities on common attribute (OneToMany & ManyToMany)

I am trying to match two entities based on a set of criteria but I can't seem to make it work in this particular case.
For the purpose of the example, let's say I have:
Article with N tags
Category with 1 tag
What I would like to do would be to, in my repository do something like:
$this->createQueryBuilder('article')
->join('App\Enttiy\Category', 'c', Join::WITH, 'c.id = :category_id)
->where('c.tag IN article.tags')
->andWhere(':category_id', $category->getId())
->getQuery()
->getResult();
Of course, in this example, the simplest way would be to define a relationship between my objects but in my real case, this relationship would be a nightmare .
The problem I face is the where line as the IN clause doesn't work that way.
Anyone has an idea of how I could use DQL to do this ? :)
Thanks by advance
First of all your andWhere will not work, you need to use ->setParameter() instead, like this:
$this->createQueryBuilder('article')
->join('App\Enttiy\Category', 'c', Join::WITH, 'c.id = :category_id')
->where('c.tag IN article.tags')
->setParameter('category_id', $category->getId())
->getQuery()
->getResult();
After, you forgot an ' in your join, here: 'c.id = :category_id, I added it in my example (don't know if it's a typing error).
Please try with this and tell me if you have an error again and if you have an error, please tell me what is it.

MS ACCESS update with an aggregate function on another table

I am trying to run the below in ms access 2010 and not getting saying "Operation must use an updatable query"
Please some advice on how to solve this without creating a query or temp table.
UPDATE tmp SET non_null_cnt = (SELECT COUNT(id_sec) FROM ESG_Results WHERE asset4_id IS NOT NULL);
Thanks in advance.
Access is very finicky about subqueries in various contexts - I just try to avoid them, for the most part. As a workaround, you can try a domain count solution:
UPDATE tmp SET non_null_cnt = DCount("id_sec", "ESG_Results", "asset4_id IS NOT NULL");
See if that works for you. You could also write a quick function which uses a recordset solution if you want.

Binding data with pragma query

I'm starting to develop a website with Laravel/SQLite.
I need to display some information from my database structure, so I'm trying to use PRAGMA statements that SQLite provides.
My question is : can I use the binding tool with pragma queries ?
The bad-old school method works fine :
DB::select('PRAGMA table_info(\''.$value->name.'\')');
If I try to use binding :
DB::select('PRAGMA table_info( ? )', array($value->name));
I get this error :
SQLSTATE[HY000]: General error: 1 near "?": syntax error (SQL: PRAGMA table_info( migrations ))
I don't see in documentation if binding with DB's specific queries is allowed or not. I don't think so, but I don't find any documentation of the binding tool, so maybe I've done a mistake.
Parameters are placeholders for an expression.
A table name (whether in a PRAGMA or in any other statement) is not an expression but an identifer, so it cannot be bound to a parameter.

filter functions problem

I'm working on a search component for an app I'm working on and I needed to add some filters to it. I've found an example and got the first filter working fine.
Now I'm trying to add a second filter I'm running into problems... In the example I found they use filterFunctions, but I only get an option for filterFunction, why is that?
Here's the example code
productsCollection.filterFunctions =
[
filterByPrice, filterByType,
filterByCondition, filterByVendor
]
And this is what I'm trying
acData.filterFunction = [filterByStatus, filterByDate]
but with this code I get the following error message - 1067: Implicit coercion of a value of type Array to an unrelated type Function.
Why am I getting this error and how would I go about add multiple filters to my Array Collection?
Thanks!
filterFunction must be set to a single function, not an Array or any other datatype. To combine multiple functions create one that combines them, like this:
acData.filterFunction = function(item:Object)
{
return
filterByPrice(item) &&
filterByType(item) &&
filterByCondition(item) &&
filterByVendor(item);
};
If you saw a sample that used filterFunctions plural that accepted an array, post a link. That's not anywhere in the standard Flex framework or in the new 4.0 beta afaik.
It looks like you are going to have to extend an arraycollection to make it work. this link should spell it out for you: http://blog.rotundu.eu/flex/arraycollection-with-multiple-filter-functions/

Resources