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

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.

Related

This use of posts_where() function is not clear

I have this very short function that I do not understand at all, and the comment is laughable:
// This function makes the meta query work
function my_posts_where( $where ) {
$where = str_replace("meta_key = 'bonuses_%", "meta_key LIKE 'bonuses_%", $where);
return $where;
}
add_filter('posts_where', 'my_posts_where');
The meta query in question is a standard WP query with array_push($args['meta_query'],$bonusesArgs); pushing $bonusesArgs (the meta query) into it.
Without the my_posts_where function, the meta query doesn't work. And I have no idea why (and neither did the developer apparently...!)
I am hoping that someone can explain the my_posts_where function - what is it doing, I've looked all over and can't make sense of it.
Well.. all it seems to be doing is rebuilding the meta query in a different, but using LIKE instead of a direct =
Like performs a search for the value in the string, instead of a direct equality.
So it would seem that this function is completely superfluous, if the original meta query just used the compare = 'LIKE' value.
But without seeing the entire code, it'd be really tough to tell exactly what repercussions this might have. But no matter what, there are better ways to interact with a query object than running some weird str_replace on its data.

Is there a SoQL DISTINCT or equivelent directive

For the URL based query in Socrata's SoQL is there a SQL equivalent of DISTINCT.
Thanks.
You can use $group to get something pretty similar. For example: https://data.agency.gov/resource/abcd-1234.json?$select=type&$group=type
I ran into the same use case. I just figured out that if you are using soda-java lib, you can build soqlquery like this:
new SoqlQueryBuilder()
.addSelectPhrase(field1)
.addGroupByPhrase(field1)
.build();

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

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

Symfony querybuilder compare string when not empty

I am new to CreateQueryBuilder and I have a quite complex query with nested and->or->and condition. It almost works (thanks Patrik Karisch) but I am blocked by a simply and "stupid" question. I want to compare a string column only when the value from column or parameter is not empty. (Note: I cannot use if and add, I have to use logic inside orX).
$expr->neq('s.studentEmail', '') always returns true when the vale is '', but I am expecting false
$queryBuilder = $repo->createQueryBuilder('s');
$expr = $queryBuilder->expr();
->andWhere($expr->orX(
$expr->andX($expr->neq('s.studentEmail', ''), $expr->isNotNull('s.studentEmail'), $expr->like('lower(s.studentEmail)', 'lower(:email)'),
$expr->......
I think you might be over complicating things. You can test your parameter outside of the query and simply skip adding the condition at all;
$qb = $studentRepo->createQueryBuilder('student');
if ($email) {
$qb->andWhere('student.email LIKE %:email%');
$qb->setParameter('email',$email);
}
I also don't see the need for the lower functions but put them in if you like.
I started using expressions because that is the way the documentation shows but not really got comfortable with them. Very verbose. Now I just write the DQL out as shown above. Seems to work the same and is a bit easier to read.
Finally, you should define your email column to either be NOT NULL in which case all empty emails are blank strings. Or allow it to be NULL and ensure that empty strings are always entered as NULL. Mixing the two together can over complicate things.

generic identifier

as I probably do not describe the problem in the right terms, I was not able to get an answer with google. Please excuse!
In the following code, I would like to replace 'hardcoded' identifier COMMENT with the variable editedField. How to do that?
var editedField:String = event.dataField;
if (model.multipleProcessingData[i][editedInformationProductNO].COMMENT != null{
...
}
Make sure you wrap this in try/catch block for NPE's, as you'll eventually find one with this many [] accessors.
A better, more OOP, would be to have an accessor function on your model that you can pass your data to:
model.getEditedField(i, editedInformatioNProductNO, editedField)
This will make it easier to troubleshoot and add good error messages to your app if things don't turn out like you expected.
var editedField:String = event.dataField;
if (model.multipleProcessingData[i][editedInformationProductNO][editedField] != null{
...
}

Resources