How could I write this sql query on symfony query builder syntax?
Analysis, Region, Nature and Garden are Entities
SELECT * FROM `analysis`
INNER JOIN sample ON sample.id = analysis.sample_id
INNER JOIN region ON sample.region_id = region.id
INNER JOIN nature ON sample.nature_id = nature.id
INNER JOIN garden ON sample.garden_id = garden.id
WHERE sample.stateProduct = 'Origin'
AND analysis.status = '0'
AND region.name = 'Jangsu'
AND garden.name = 'North Tukvar'
AND nature.name = 'Thé Vert'
AND sample.sampleBio = '1'
AND sample.supplierCountry = 'Inde'
I tried this way but, I don't have error msg, but it's not same result as sql query.
public function countAnalysisByCriteria($stateProduct, $status, Nature $nature, Region $region, Garden $garden, $supplierName, $bio, $country, $startDate, $endDate){
$qb = $this->createQueryBuilder('analysis')
->addSelect('count(analysis) as result')
->innerJoin('analysis.sample', 'sample', 'WITH', 'analysis.sample = sample')
->innerJoin('sample.nature', 'nature', 'WITH', 'sample.nature = :nature')
->innerJoin('sample.region', 'region', 'WITH', 'sample.region = :region')
->innerJoin('sample.garden', 'garden', 'WITH', 'sample.garden = :garden')
->groupBy('result');
->andWhere('sample.stateProduct = :stateProduct');
->setParameter('stateProduct', $stateProduct);
->andWhere('sample.nature = :nature');
->setParameter('nature', $nature);
->andWhere('sample.region = :region');
->setParameter('region', $region);
->andWhere('sample.garden = :garden');
->setParameter('garden', $garden);
->andWhere('sample.dateReception BETWEEN :startDate AND :endDate');
$qb->setParameter('startDate', $startDate);
$qb->setParameter('endDate', $endDate);
}
return $qb->getQuery()->getArrayResult();
Your code snippet is completely wrong, try the following:
use Doctrine\ORM\Query\Expr\Join;
public function countAnalysisByCriteria(
$stateProduct,
$status,
Nature $nature,
Region $region,
Garden $garden,
$supplierName,
$bio,
$country,
$startDate,
$endDate
) {
$qb = $this->createQueryBuilder();
return $qb->select('count(analysis) as result')
->innerJoin('analysis.sample', 'sample', Join::WITH, 'analysis.sample = sample.id')
->innerJoin('sample.nature', 'nature', Join::WITH, 'sample.nature = nature.id')
->innerJoin('sample.region', 'region', Join::WITH, 'sample.region = region.id')
->innerJoin('sample.garden', 'garden', Join::WITH, 'sample.garden = garden.id')
->groupBy('result')
->andWhere('sample.stateProduct =:stateProduct')
->setParameter('stateProduct', $stateProduct)
->andWhere('sample.nature =:nature')
->setParameter('nature', $nature)
->andWhere('sample.region =:region')
->setParameter('region', $region)
->andWhere('sample.garden =:garden')
->setParameter('garden', $garden)
->andWhere('sample.dateReception BETWEEN :startDate AND :endDate')
->setParameter('startDate', $startDate)
->setParameter('endDate', $endDate)
->getQuery()
->getArrayResult();
}
DO NOT add spaces on assignments = : (wrong), =: (right)
Check your code properly, notice how I have removed some colon ; at some pieces because does not make sense have them there.
Related
I realized this sql which works without problems
SELECT meeting.name, meeting.date, community.name, participation.isPresent, participation.user_id
FROM meeting
INNER JOIN community
ON meeting.community_id = community.id
AND community.is_active = 1
LEFT join participation
ON meeting.id = participation.meeting_id
AND participation.user_id = 1078
WHERE meeting.date >= CURRENT_DATE()
ORDER BY meeting.date DESC
I'm trying to reproduce it with the doctrine query builder but I never got the right result. The user id part doesn't seem to be part of the leftJoin function but is applied to the request globally, which is not what I want.
public function getNextMeetings()
{
$qb = $this->createQueryBuilder('m')
->select('m.name AS meeting, m.date, c.name AS community, p.isPresent', 'IDENTITY(p.user) AS user')
->innerJoin('m.community', 'c')
->where('c.isActive = true')
->leftJoin('m.participations', 'p')
//->leftJoin('p.user', 'u')
//->where('u.id = 1078 OR u.id IS NULL')
//->where('IDENTITY(p.user) = 1078')
->andWhere('m.date >= CURRENT_DATE()')
->orderBy('m.date', 'DESC');
return $qb->getQuery()->execute();
}
My comments are what I tried to fix this issue.
Check Working with QueryBuilder: High level API methods
More precisely, the definition od leftJoin() function:
public function leftJoin($join, $alias, $conditionType = null, $condition = null, $indexBy = null);
You can place a condition on the joined Entity by:
use Doctrine\ORM\Query\Expr;
->leftJoin('m.participations', 'p', Expr\Join::WITH, 'p.user = :userId')
->setParameter('userId', 1078)
Note you do not need a condition for "meeting.id = participation.meeting_id", as this is autoapplied by the relation m.participations to the join constructed.
I need to make a sql query like this,
UPDATE org_mapping SET is_active = 1 WHERE (org_id = ? AND service_provider_id = ? )OR (org_id = ? AND service_provider_id = ?)
I tried this but its now working:
$q = $qb->update('Organization\Entity\OrgMapping', 'om')
->set('om.active', $qb->expr()->literal($isActive))
->where('om.organization = ?1')->andWhere('om.serviceProvider = ?2')
->orWhere('om.organization = ?2')->andWhere('om.serviceProvider = ?1')
->setParameter(1, $organizationId)
->setParameter(2, $hspId)
->getQuery();
When i am running, i am getting the following query:
UPDATE org_mapping SET is_active = 1 WHERE ((org_id = ? AND service_provider_id = ?) OR org_id = ?) AND service_provider_id = ?
Replace
->orWhere('om.organization = ?2')->andWhere('om.serviceProvider = ?1')
With
->orWhere('om.organization = ?2 AND om.serviceProvider = ?1')
Try this:
$q = $qb->update('Organization\Entity\OrgMapping', 'om')
->set('om.active', $qb->expr()->literal($isActive))
->where(
$qb->expr()->orX(
$qb->expr()->andX(
$qb->expr()->eq('om.organization', '?1')
,
$qb->expr()->eq('om.serviceProvider','?2')
),
$qb->expr()->andX(
$qb->expr()->eq('om.organization', '?2')
,
$qb->expr()->eq('om.serviceProvider','?1')
)
)
)
->setParameter(1, $organizationId)
->setParameter(2, $hspId)
->getQuery();
You should not do it.
If you do an UPDATE query, you're missing the whole point of using Doctrine, that is mapping objects to database rows, not just an abstraction to queries.
Instead, extract your objects, then do a cycle and perform actions on single objects, then flush after the cycle.
pls help i get this error
Cannot count query which selects two FROM components, cannot make distinction
when i try to break my query to add conditional statement
i have read this
KnpPaginatorBundle/Resources/doc/manual_counting.md and i arrived at this
public function findCategoryProduct($category,$minPrice=null,$maxPrice=null,$gender=null)
{
$countgb = $this->createQueryBuilder('1')
->select('count(p)')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->where('c = :category')
->andWhere('p.visible >= :true')
->setParameter('category', $category)
->setParameter('true', 1);
$count = $countgb->getQuery()->getSingleScalarResult();
$query = $this->createQueryBuilder('1')
->select('p')
->from('AppBundle:Product','p')
->join('p.group', 'g')
->join('g.category', 'c')
->where('c = :category')
->andWhere('p.visible >= :true')
->setParameter('category', $category)
->setParameter('true', 1);
$query ->getQuery()
->setHint('knp_paginator.count', $count);
return $query;
}
$paginator = $this->get('knp_paginator');
$pagination = $paginator->paginate($query,$request->query->getInt('page', 1),10,array('distinct' => false));
and i still get the error
Hi I advise you to use subquery like :
$query = $em->createQuery('SELECT u.id FROM CmsUser u WHERE EXISTS (SELECT p.phonenumber FROM CmsPhonenumber p WHERE p.user = u.id)');
$ids = $query->getResult();
or with expr:
$query->andWhere($query->expr()->notIn('c.id', $subquery->getDQL()));
some documentation here
I'm trying to update a certain number of rows of my entity "Vehicule". I have no idea how could it work.
I'm actually trying to modify only two rows where direction= 5.This is the function I used in order to update.
public function ValidAction(\OC\UserBundle\Entity\User $direction) {
$qb = $this->getDoctrine()
->getRepository('CarPfeBundle:Vehicule')
->createQueryBuilder('v');
$q = $qb->update ('CarPfeBundle:vehicule v')
->set('v.direction', '?1')
->where('v.direction = ?2')
->setParameter(1, $direction)
->setParameter(2, 5)
->getQuery();
$p = $q->execute();
return $this->redirect($this->generateUrl('demandeveh_afficher'));
}
But the above code update all rows of my database. I need to update only two rows. Any help please?
Try to do this ;
public function ValidAction(\OC\UserBundle\Entity\User $direction) {
$qb = $this->getDoctrine()
->getRepository('CarPfeBundle:Vehicule')
->createQueryBuilder('v');
// $ids an array that contains all ids with your condition
$ids = $qb->select('v.id')
->where('v.direction = :direction')
->setParameter(
array(
'direction' => $direction
)
)
->getQuery()
->getResult();
$id1 = $ids[array_rand($ids)];
$id2 = $ids[array_rand($ids)];
//To be sure that $id1 is different from id2
while ($id1 == $id2) {
$id2 = $ids[array_rand($ids)];
}
$q = $qb->update ('CarPfeBundle:vehicule v')
->set('v.direction', ':val1')
->where('v.direction = :val2')
->andWhere('v.id IN (:id1, :id2)')
->setParameter(
array(
'val1' => $direction ,
'val2' => 5 ,
'id1' => $id1,
'id2' => $id2,
)
)
->getQuery();
$p = $q->execute();
return $this->redirect($this->generateUrl('demandeveh_afficher'));
}
With the above code I hope you can update only two rows and randomly.
Good luck !
While a solution like Houssem Zitoun suggested may work, why not use a subquery?
If you get the (like I did, if not, just skip the middle SELECT)
Error: #1235 - This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery'
go with this answer and something like (doc): - untested
UPDATE CarPfeBundle:Vehicule v
SET v.direction = ?1
WHERE v.direction IN
(SELECT * FROM (
SELECT v.direction
FROM CarPfeBundle:Vehicule v2
WHERE v.direction = ?2 LIMIT 2
)) AS sq
This is my select and it works in workbench fine, but how did i write the INNER JOINS for doctrine?
$qb = $em->createQuery(
"SELECT oh.objektnummer, oh.idSubunternehmer, oh.datum, oh.typ, oh.outbox
FROM MBSAllgemeinBundle:ObjektHistory oh
INNER JOIN MBSAllgemeinBundle:Objekt o ON o.objektnummer = oh.objektnummer AND o.idSubunternehmer = oh.idSubunternehmer
INNER JOIN MBSAllgemeinBundle:Subunternehmer s ON s.subunternehmernummer = o.id_subunternehmer
INNER JOIN MBSAllgemeinBundle:SubunternehmerUser su ON su.id_subunternehmer = s.subunternehmernummer
WHERE su.idUser = 1"
);
Try this syntax in your repository :
return $this->createQueryBuilder('oh')
->select('partial oh.{id, objektnummer, idSubunternehmer, datum, typ, outbox}')
->innerJoin('MBSAllgemeinBundle:Objekt', 'o', 'ON', 'o.objektnummer = oh.objektnummer AND o.idSubunternehmer = oh.idSubunternehmer')
->addSelect('o')
// other INNER JOIN
// other INNER JOIN
->where('su.idUser = :id')
->setParameter('id', 1)
->getQuery()
->getResult()
;