doctrine2 findby two columns OR condition - symfony

My action:
$matches_request = $em->getRepository('Bundle:ChanceMatch')->findByRequestUser(1);
$matches_reply = $em->getRepository('Bundle:ChanceMatch')->findByReplyUser(1);
Is it possible to join the querys with an or condition with getRepository, eg.
$matches_reply = $em->getRepository('FrontendChancesBundle:ChanceMatch')->findBy(array('requestUser' => 1, 'replyUser' => 1);
//this of course gives me the a result when `requestUser` and `replyUser` is `1`.
My table
id | requestUser | replyUser
....
12 | 1 | 2
13 | 5 | 1
My query should return the id 12 & 13.
Thanks for help!

You can use QueryBuilder or create a custom repository for that entity and create a function that internally use QueryBuilder.
$qb = $em->getRepository('FrontendChancesBundle:ChanceMatch')->createQueryBuilder('cm');
$qb
->select('cm')
->where($qb->expr()->orX(
$qb->expr()->eq('cm.requestUser', ':requestUser'),
$qb->expr()->eq('cm.replyUser', ':replyUser')
))
->setParameter('requestUser', $requestUserId)
->setParameter('replyUser', $replyUserId)
;
$matches_reply = $qb->getQuery()->getSingleResult();
// $matches_reply = $qb->getQuery()->getResult(); // use this if there can be more than one result
For more information on custom Repository see official documentation:
http://symfony.com/doc/2.0/book/doctrine.html#custom-repository-classes

It's possible to use Criteria for the complex queries with getRepository():
$criteria = new \Doctrine\Common\Collections\Criteria();
$criteria
->orWhere($criteria->expr()->contains('domains', 'a'))
->orWhere($criteria->expr()->contains('domains', 'b'));
$domain->ages = $em
->getRepository('Group')
->matching($criteria);

Related

Doctrine: select query to refresh hydrated entity join?

A query load and hydrate a main entity with it's joined entities, which are filtered.
Same query with a different filtering did not update the joined entities.
Some data:
Tiers:
| id | name |
| 1 | alpha |
| 2 | beta |
Container:
| id | tiers_id | category |
| 10 | 1 | A |
| 20 | 1 | A |
| 30 | 1 | B |
| 40 | 1 | B |
Execute 2 queries to get some tiers with theirs containers joined, category A first, then category B:
$dql = "select t, c
from Tiers t
join t.containers c
where t.id in (?1) and c.category = (?2)";
$result = $em->createQuery($dql)
->setParameter(1, array(1))
->setParameter(2, 'A')
->getResult();
$tiers = $result[0];
$containers = $tiers->getContainers(); // tiers 1 with containers 10 and 20, that's fine !
$result = $em->createQuery($dql)
->setParameter(1, array(1))
->setParameter(2, 'B')
->getResult();
$tiers = $result[0];
$containers = $tiers->getContainers(); // BAD HERE: still get containers 10 and 20, looking for containers 30 and 40.
After the 2nd query, tiers 1 preserve its containers loaded during first query. That's not what is expected.
So is there a way to get the containers 30 and 40 after 2nd query ?
Maybe a kind of "reset/detach" the containers of tiers entities after the first query ?
Or anything else...
Mutiple select in queries are used to hydrate tiers with required containers joined.
'getContainers' method gives the expected containers from each tiers.
And cost to BDD is only 1 SQL query, whatever the quantity of tiers searched.
I suppose tiers cannot be detach/reload because they are updated before, between and after the queries, it throw this kind of exception when flushing:
Uncaught Exception: Multiple non-persisted new entities were found through the given association graph
* A new entity was found through the relationship 'XXX' that was not configured to cascade persist operations for entity: XXX\Entity\Tiers#00000000257b87500000000018499b62.
Reset the tiers's containers before 2nd query:
foreach($result as $tiers)
$tiers->nullContainers();
Add method to Entity\Tiers:
public function nullContainers()
{
this->containers = null;
}
Then the 2nd query "refresh" tiers's containers.

Doctrine limit to a number ignoring duplicate

I'm using Symfony 3.4 and I have a request in my user repository.
I want the top 5 users based on score. So I have created my request with an order by on score column and a limit to 5 result.
$query = $this->createQueryBuilder('u')
->orderBy('u.score', 'DESC')
->setMaxResults(5)
->getQuery()
->getResult();
But I want the top 5 with taking the user with same score. For exmaple with:
User : Score
Jack : 100
Mick : 50
Joe : 10
Daniel : 25
Fred : 75
James : 100
Billy : 2
I want to return 6 result (because two user have the same score). What I want
Jack
James
Fred
Mick
Daniel
Joe
And If I have an other user with the same score than Mick, it should return 7 result.
The number of result return the top 5 best score but with all the user with this score.
How can I edit my query to do it ?
You should use a subquery selecting all the user that have the top5 score, as example:
$subQuery = $this->createQueryBuilder('u1')
->select('DISTINCT u1.score')
->orderBy('u1.score', 'DESC')
->setMaxResults(5)
->GetDQL();
$query = $this->createQueryBuilder('u2');
$query->where(
$query->expr()->in(
'u2.score', $subquery))
->orderBy('u2.score', 'DESC')
->getQuery()
->getResult();
Hope this help
I have found a solution with two request (feel free to say if you have better solution)
This is my code working but in my question I simplify the text, in reality my score has store on a user_session table, so it has more join and more condition but it can help.
First I get all the top 5 score with a distinct to group the same score.
$now = new \DateTime();
$leaderScore = $this->createQueryBuilder('u')
->select('us.score')
->join('u.sessionUsers', 'us')
->join('us.session', 's')
->where(':now BETWEEN s.start_date AND s.end_date')
->setParameter('now', $now)
->setMaxResults(self::NUMBER_LEADERS_DISPLAY)
->distinct()
->orderBy('us.score', 'DESC')
->getQuery()
->getResult();
I save the worst score of the top 5:
$minScore = min($leaderScore);
And I make another request to get all users with a higher or egal score than $minScore
$query = $this->createQueryBuilder('u')
->join('u.sessionUsers', 'us')
->join('us.session', 's')
->where(':now BETWEEN s.start_date AND s.end_date')
->andWhere('u.roles LIKE :role')
->andWhere('us.score >= :minScore')
->orderBy('us.score', 'DESC')
->setParameter('now', $now)
->setParameter('role', '%PLAYER%')
->setParameter('minScore', $minScore);
Hope this help :)

How can I order by match result in doctrine querybuilder?

I have a custom dql match function and would like to select the match result as 'score' before ordering by that score. How can I adjust the following query to achieve this?
$qb = $this->getEntityManager()->createQueryBuilder();
$qb
->select('i', 'bi')
->from('AdminBundle:Items', 'i')
->where('i.instock=1')
->leftJoin('AdminBundle:MyTable','bi',\Doctrine\ORM\Query\Expr\Join::WITH, 'i.id = bi.productId')
->andWhere('bi.productId IS NULL')
->andWhere('MATCH (i.brand, i.store, i.title, i.description, i.keywords) AGAINST (:search BOOLEAN) > 0')
->andWhere('i.instock = 1')
->setParameter('search', $searchString)
->setMaxResults( $limit )
;
$results = $qb->getQuery()->getArrayResult();
I do not know if I understand your question, do you want to add to your select what you have in your where (match...) and order by it?
You can add not-mapped selects like this:
$qb->addSelect('MY_FUNCTION(my.params) AS HIDDEN mySelectAlias')
In your case, sth like:
$qb->addSelect('(MATCH (i.brand, i.store, i.title, i.description, i.keywords) AGAINST (:search BOOLEAN)) AS HIDDEN mySelectAlias')
And in your order by:
$qb->orderBy('mySelectAlias', 'desc');
Complete example:
https://coderwall.com/p/o5snag

symfony2 doctrine select IFNULL

Ok i have this code:
SELECT
IFNULL(s2.id,s1.id) AS effectiveID,
IFNULL(s2.status, s1.status) AS effectiveStatus,
IFNULL(s2.user_id, s1.user_id) as effectiveUser,
IFNULL(s2.likes_count, s1.likes_count) as effectiveLikesCount
FROM statuses AS s1
LEFT JOIN statuses AS s2 ON s2.id = s1.shared_from_id
WHERE s1.user_id = 4310
ORDER BY effectiveID DESC
LIMIT 15
And i need to rewrite it to querybuilder. Something like that?
$fields = array('IFNULL(s2.id,s1.id) AS effectiveID','IFNULL(s2.status, s1.status) AS effectiveStatus', 'IFNULL(s2.user_id, s1.user_id) as effectiveUser','IFNULL(s2.likes_count, s1.likes_count) as effectiveLikesCount');
$qb=$this->_em->createQueryBuilder()
->select($fields)
->from('WallBundle:Status','s1')
->addSelect('u')
->where('s1.user = :user')
->andWhere('s1.admin_status = false')
->andWhere('s1.typ_statusu != :group')
->setParameter('user', $user)
->setParameter('group', 'group')
->leftJoin('WallBundle:Status','s2', 'WITH', 's2.id=s1.shared_from_id')
->innerJoin('s1.user', 'u')
->orderBy('s1.time', 'DESC')
->setMaxResults(15);
var_dump($query=$qb->getQuery()->getResult());die();
This error is
[Syntax Error] line 0, col 7: Error: Expected known function, got 'IFNULL'
Use COALESCE instead of IFNULL like this
$fields = array('COALESCE(s2.id,s1.id) AS effectiveID','COALESCE(s2.status, s1.status) AS effectiveStatus', 'COALESCE(s2.user_id, s1.user_id) as effectiveUser','COALESCE(s2.likes_count, s1.likes_count) as effectiveLikesCount');
COALESCE return the first value not null in the list, so if A is null and B not null, then COALESCE(A,B) will return B.
There is a Doctrine extension that adds this among others.
This is the DQL file from IFNULL.
https://github.com/beberlei/DoctrineExtensions/blob/master/src/Query/Mysql/IfNull.php
This chapter explains how you use them.
http://symfony.com/doc/2.0/cookbook/doctrine/custom_dql_functions.html

Doctrine2 QueryBuilder - variable in field name

I am trying to place a variable into the field name in the query , so I have a schema:
id amazon - tesco - asda - happyshopper -
1 £5 - NULL - NULL - £4.99
2 NULL - £2.99 - NULL - NULL
and then
$store = 'amazon';
$qb = $em->createQueryBuilder();
$products = $qb->select('p')->from('MyBundle:Product', 'p')
->where('p.:store IS NOT NULL')
->setParameter('store', $store)
->add('orderBy', 'p.onSale DESC')
->setMaxResults(40)
->getQuery()
->getResult();
Would return row 1.
What I've done for :
->where('p.:store IS NOT NULL')
->setParameter('store', $store)
Is incorrect and it errors.
->where(':store IS NOT NULL')
->setParameter('store', $store)
does not error, but doesn't apply the store filter.
The short answer here is to just work the store name into the string manually:
->where("p.$store IS NOT NULL")
or
->where('p.' . $store . ' IS NOT NULL')
The long answer is that your database schema could use some work. What happens, for example, if/when you want to add a new store? Are you going to add a new column and re-code the whole thing? The better solution is to separate the concept of "store", put it in its own table, and join everything together in a different table. Something like this:
Product:
id | name | onSale
1 | foo | 1
2 | bar | 0
Store:
id | name
1 | amazon
2 | tesco
3 | asda
4 | happyshopper
Price:
id | productId | storeId | price
1 | 1 | 1 | 5
2 | 1 | 4 | 4.99
3 | 2 | 2 | 2.99
Once you configure your tables and your mappings properly, your query turns in to:
$qb = $em->createQueryBuilder();
$products = $qb
->select('product')
->from('MyBundle:Price', 'price')
->innerJoin('price.product', 'product')
->innerJoin('price.store', 'store')
->where('store.name = :store')
->setParameter('store', $store)
->add('orderBy', 'product.onSale DESC')
->setMaxResults(40)
->getQuery()
->getResult();

Resources